@@ -11,168 +11,119 @@ require('mocha');
11
11
12
12
var outpath = path . join ( __dirname , './out-fixtures' ) ;
13
13
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
+
14
27
describe ( 'gulp' , function ( ) {
15
28
describe ( 'watch()' , function ( ) {
16
29
beforeEach ( rimraf . bind ( null , outpath ) ) ;
17
30
beforeEach ( mkdirp . bind ( null , outpath ) ) ;
18
31
afterEach ( rimraf . bind ( null , outpath ) ) ;
19
32
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
-
32
33
it ( 'should call the function when file changes: no options' , function ( done ) {
33
-
34
- // Arrange
35
34
var tempFile = path . join ( outpath , 'watch-func.txt' ) ;
36
- fs . writeFile ( tempFile , tempFileContent , function ( ) {
37
35
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 ) ;
48
37
49
- // Act: change file
50
- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
38
+ var watcher = gulp . watch ( tempFile , function ( cb ) {
39
+ watcher . end ( ) ;
40
+ cb ( ) ;
41
+ done ( ) ;
51
42
} ) ;
43
+
44
+ updateTempFile ( tempFile ) ;
52
45
} ) ;
53
46
54
47
it ( 'should call the function when file changes: w/ options' , function ( done ) {
55
-
56
- // Arrange
57
48
var tempFile = path . join ( outpath , 'watch-func-options.txt' ) ;
58
- fs . writeFile ( tempFile , tempFileContent , function ( ) {
59
49
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 ) ;
70
51
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 ( ) ;
73
56
} ) ;
57
+
58
+ updateTempFile ( tempFile ) ;
74
59
} ) ;
75
60
76
61
it ( 'should not drop options when no callback specified' , function ( done ) {
77
- // Arrange
78
62
var tempFile = path . join ( outpath , 'watch-func-nodrop-options.txt' ) ;
79
63
// By passing a cwd option, ensure options are not lost to gaze
80
64
var relFile = '../watch-func-nodrop-options.txt' ;
81
65
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 ) ;
99
81
} ) ;
100
82
101
83
it ( 'should run many tasks: w/ options' , function ( done ) {
102
- // Arrange
103
84
var tempFile = path . join ( outpath , 'watch-task-options.txt' ) ;
104
- var task1 = 'task1' ;
105
- var task2 = 'task2' ;
106
- var task3 = 'task3' ;
107
85
var a = 0 ;
108
- var timeout = writeTimeout * 2.5 ;
109
86
110
- fs . writeFile ( tempFile , tempFileContent , function ( ) {
87
+ createTempFile ( tempFile ) ;
111
88
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
+ } ) ;
129
100
130
- gulp . reset ( ) ;
131
- watcher . end ( ) ;
132
- done ( ) ;
133
- } , timeout ) ;
101
+ var watcher = gulp . watch ( tempFile , { debounceDelay : 25 } , gulp . series ( 'task1' , 'task2' ) ) ;
134
102
135
- // Act: change file
136
- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
137
- } ) ;
103
+ updateTempFile ( tempFile ) ;
138
104
} ) ;
139
105
140
106
it ( 'should run many tasks: no options' , function ( done ) {
141
- // Arrange
142
107
var tempFile = path . join ( outpath , 'watch-many-tasks-no-options.txt' ) ;
143
- var task1 = 'task1' ;
144
- var task2 = 'task2' ;
145
- var task3 = 'task3' ;
146
108
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
- } ) ;
160
109
161
- // It works if it calls the task
162
- var watcher = gulp . watch ( tempFile , [ task1 , task2 ] ) ;
110
+ createTempFile ( tempFile ) ;
163
111
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
+ } ) ;
167
123
168
- gulp . reset ( ) ;
169
- watcher . end ( ) ;
170
- done ( ) ;
171
- } , timeout ) ;
124
+ var watcher = gulp . watch ( tempFile , gulp . series ( 'task1' , 'task2' ) ) ;
172
125
173
- // Act: change file
174
- writeFileWait ( tempFile , tempFileContent + ' changed' ) ;
175
- } ) ;
126
+ updateTempFile ( tempFile ) ;
176
127
} ) ;
177
128
178
129
} ) ;
0 commit comments