@@ -11,168 +11,119 @@ require('mocha');
1111
1212var outpath = path . join ( __dirname , './out-fixtures' ) ;
1313
14+ var tempFileContent = 'A test generated this file and it is safe to delete' ;
15+
16+ function createTempFile ( path ) {
17+ fs . writeFileSync ( path , tempFileContent ) ;
18+ }
19+
20+ function updateTempFile ( path ) {
21+ var gazeTimeout = 125 ;
22+ setTimeout ( function ( ) {
23+ fs . appendFileSync ( path , ' changed' ) ;
24+ } , gazeTimeout ) ;
25+ }
26+
1427describe ( 'gulp' , function ( ) {
1528 describe ( 'watch()' , function ( ) {
1629 beforeEach ( rimraf . bind ( null , outpath ) ) ;
1730 beforeEach ( mkdirp . bind ( null , outpath ) ) ;
1831 afterEach ( rimraf . bind ( null , outpath ) ) ;
1932
20- var tempFileContent = 'A test generated this file and it is safe to delete' ;
21-
22- var writeTimeout = 125 ; // Wait for it to get to the filesystem
23- var writeFileWait = function ( name , content , cb ) {
24- if ( ! cb ) {
25- cb = function ( ) { } ;
26- }
27- setTimeout ( function ( ) {
28- fs . writeFile ( name , content , cb ) ;
29- } , writeTimeout ) ;
30- } ;
31-
3233 it ( 'should call the function when file changes: no options' , function ( done ) {
33-
34- // Arrange
3534 var tempFile = path . join ( outpath , 'watch-func.txt' ) ;
36- fs . writeFile ( tempFile , tempFileContent , function ( ) {
3735
38- // Assert: it works if it calls done
39- var watcher = gulp . watch ( tempFile , function ( evt ) {
40- should . exist ( evt ) ;
41- should . exist ( evt . path ) ;
42- should . exist ( evt . type ) ;
43- evt . type . should . equal ( 'changed' ) ;
44- evt . path . should . equal ( path . resolve ( tempFile ) ) ;
45- watcher . end ( ) ;
46- done ( ) ;
47- } ) ;
36+ createTempFile ( tempFile ) ;
4837
49- // Act: change file
50- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
38+ var watcher = gulp . watch ( tempFile , function ( cb ) {
39+ watcher . end ( ) ;
40+ cb ( ) ;
41+ done ( ) ;
5142 } ) ;
43+
44+ updateTempFile ( tempFile ) ;
5245 } ) ;
5346
5447 it ( 'should call the function when file changes: w/ options' , function ( done ) {
55-
56- // Arrange
5748 var tempFile = path . join ( outpath , 'watch-func-options.txt' ) ;
58- fs . writeFile ( tempFile , tempFileContent , function ( ) {
5949
60- // Assert: it works if it calls done
61- var watcher = gulp . watch ( tempFile , { debounceDelay : 5 } , function ( evt ) {
62- should . exist ( evt ) ;
63- should . exist ( evt . path ) ;
64- should . exist ( evt . type ) ;
65- evt . type . should . equal ( 'changed' ) ;
66- evt . path . should . equal ( path . resolve ( tempFile ) ) ;
67- watcher . end ( ) ;
68- done ( ) ;
69- } ) ;
50+ createTempFile ( tempFile ) ;
7051
71- // Act: change file
72- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
52+ var watcher = gulp . watch ( tempFile , { debounceDelay : 5 } , function ( cb ) {
53+ watcher . end ( ) ;
54+ cb ( ) ;
55+ done ( ) ;
7356 } ) ;
57+
58+ updateTempFile ( tempFile ) ;
7459 } ) ;
7560
7661 it ( 'should not drop options when no callback specified' , function ( done ) {
77- // Arrange
7862 var tempFile = path . join ( outpath , 'watch-func-nodrop-options.txt' ) ;
7963 // By passing a cwd option, ensure options are not lost to gaze
8064 var relFile = '../watch-func-nodrop-options.txt' ;
8165 var cwd = outpath + '/subdir' ;
82- fs . writeFile ( tempFile , tempFileContent , function ( ) {
83-
84- // Assert: it works if it calls done
85- var watcher = gulp . watch ( relFile , { debounceDelay : 5 , cwd : cwd } )
86- . on ( 'change' , function ( evt ) {
87- should . exist ( evt ) ;
88- should . exist ( evt . path ) ;
89- should . exist ( evt . type ) ;
90- evt . type . should . equal ( 'changed' ) ;
91- evt . path . should . equal ( path . resolve ( tempFile ) ) ;
92- watcher . end ( ) ;
93- done ( ) ;
94- } ) ;
95-
96- // Act: change file
97- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
98- } ) ;
66+
67+ createTempFile ( tempFile ) ;
68+
69+ var watcher = gulp . watch ( relFile , { debounceDelay : 5 , cwd : cwd } )
70+ . on ( 'change' , function ( evt ) {
71+ should . exist ( evt ) ;
72+ should . exist ( evt . path ) ;
73+ should . exist ( evt . type ) ;
74+ evt . type . should . equal ( 'changed' ) ;
75+ evt . path . should . equal ( path . resolve ( tempFile ) ) ;
76+ watcher . end ( ) ;
77+ done ( ) ;
78+ } ) ;
79+
80+ updateTempFile ( tempFile ) ;
9981 } ) ;
10082
10183 it ( 'should run many tasks: w/ options' , function ( done ) {
102- // Arrange
10384 var tempFile = path . join ( outpath , 'watch-task-options.txt' ) ;
104- var task1 = 'task1' ;
105- var task2 = 'task2' ;
106- var task3 = 'task3' ;
10785 var a = 0 ;
108- var timeout = writeTimeout * 2.5 ;
10986
110- fs . writeFile ( tempFile , tempFileContent , function ( ) {
87+ createTempFile ( tempFile ) ;
11188
112- gulp . task ( task1 , function ( ) {
113- a ++ ;
114- } ) ;
115- gulp . task ( task2 , function ( ) {
116- a += 10 ;
117- } ) ;
118- gulp . task ( task3 , function ( ) {
119- throw new Error ( 'task3 called!' ) ;
120- } ) ;
121-
122- // It works if it calls the task
123- var config = { debounceDelay : timeout / 2 } ;
124- var watcher = gulp . watch ( tempFile , config , [ task1 , task2 ] ) ;
125-
126- // Assert
127- setTimeout ( function ( ) {
128- a . should . equal ( 11 ) ; // Task1 and task2
89+ gulp . task ( 'task1' , function ( cb ) {
90+ a ++ ;
91+ cb ( ) ;
92+ } ) ;
93+ gulp . task ( 'task2' , function ( cb ) {
94+ a += 10 ;
95+ a . should . equal ( 11 ) ;
96+ watcher . end ( ) ;
97+ cb ( ) ;
98+ done ( ) ;
99+ } ) ;
129100
130- gulp . reset ( ) ;
131- watcher . end ( ) ;
132- done ( ) ;
133- } , timeout ) ;
101+ var watcher = gulp . watch ( tempFile , { debounceDelay : 25 } , gulp . series ( 'task1' , 'task2' ) ) ;
134102
135- // Act: change file
136- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
137- } ) ;
103+ updateTempFile ( tempFile ) ;
138104 } ) ;
139105
140106 it ( 'should run many tasks: no options' , function ( done ) {
141- // Arrange
142107 var tempFile = path . join ( outpath , 'watch-many-tasks-no-options.txt' ) ;
143- var task1 = 'task1' ;
144- var task2 = 'task2' ;
145- var task3 = 'task3' ;
146108 var a = 0 ;
147- var timeout = writeTimeout * 2.5 ;
148-
149- fs . writeFile ( tempFile , tempFileContent , function ( ) {
150-
151- gulp . task ( task1 , function ( ) {
152- a ++ ;
153- } ) ;
154- gulp . task ( task2 , function ( ) {
155- a += 10 ;
156- } ) ;
157- gulp . task ( task3 , function ( ) {
158- throw new Error ( 'task3 called!' ) ;
159- } ) ;
160109
161- // It works if it calls the task
162- var watcher = gulp . watch ( tempFile , [ task1 , task2 ] ) ;
110+ createTempFile ( tempFile ) ;
163111
164- // Assert
165- setTimeout ( function ( ) {
166- a . should . equal ( 11 ) ; // Task1 and task2
112+ gulp . task ( 'task1' , function ( cb ) {
113+ a ++ ;
114+ cb ( ) ;
115+ } ) ;
116+ gulp . task ( 'task2' , function ( cb ) {
117+ a += 10 ;
118+ a . should . equal ( 11 ) ;
119+ watcher . end ( ) ;
120+ cb ( ) ;
121+ done ( ) ;
122+ } ) ;
167123
168- gulp . reset ( ) ;
169- watcher . end ( ) ;
170- done ( ) ;
171- } , timeout ) ;
124+ var watcher = gulp . watch ( tempFile , gulp . series ( 'task1' , 'task2' ) ) ;
172125
173- // Act: change file
174- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
175- } ) ;
126+ updateTempFile ( tempFile ) ;
176127 } ) ;
177128
178129 } ) ;
0 commit comments