@@ -8,59 +8,137 @@ var File = require('vinyl');
8
8
9
9
describe ( 'gulp-replace' , function ( ) {
10
10
describe ( 'replacePlugin()' , function ( ) {
11
- it ( 'should replace string on a stream' , function ( done ) {
12
- var file = new File ( {
13
- path : 'test/fixtures/helloworld.txt' ,
14
- cwd : 'test/' ,
15
- base : 'test/fixtures' ,
16
- contents : fs . createReadStream ( 'test/fixtures/helloworld.txt' )
11
+ describe ( 'buffered input' , function ( ) {
12
+ var file ;
13
+
14
+ beforeEach ( function ( ) {
15
+ file = new File ( {
16
+ path : 'test/fixtures/helloworld.txt' ,
17
+ cwd : 'test/' ,
18
+ base : 'test/fixtures' ,
19
+ contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
20
+ } ) ;
17
21
} ) ;
18
22
19
- var stream = replacePlugin ( 'world' , 'person' ) ;
20
- stream . on ( 'data' , function ( newFile ) {
21
- should . exist ( newFile ) ;
22
- should . exist ( newFile . contents ) ;
23
+ it ( 'should replace string on a buffer' , function ( done ) {
24
+ var stream = replacePlugin ( 'world' , 'person' ) ;
25
+ stream . on ( 'data' , function ( newFile ) {
26
+ should . exist ( newFile ) ;
27
+ should . exist ( newFile . contents ) ;
23
28
24
- newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
25
- data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
29
+ String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
26
30
done ( ) ;
27
- } ) ) ;
31
+ } ) ;
32
+
33
+ stream . write ( file ) ;
34
+ stream . end ( ) ;
28
35
} ) ;
29
36
30
- stream . write ( file ) ;
31
- stream . end ( ) ;
32
- } ) ;
37
+ it ( 'should replace regex on a buffer' , function ( done ) {
38
+ var stream = replacePlugin ( / w o r l d / g, 'person' ) ;
39
+ stream . on ( 'data' , function ( newFile ) {
40
+ should . exist ( newFile ) ;
41
+ should . exist ( newFile . contents ) ;
33
42
34
- it ( 'should replace string on a buffer' , function ( done ) {
35
- var file = new File ( {
36
- path : 'test/fixtures/helloworld.txt' ,
37
- cwd : 'test/' ,
38
- base : 'test/fixtures' ,
39
- contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
43
+ String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
44
+ done ( ) ;
45
+ } ) ;
46
+
47
+ stream . write ( file ) ;
48
+ stream . end ( ) ;
40
49
} ) ;
41
50
42
- var stream = replacePlugin ( 'world' , 'person' ) ;
43
- stream . on ( 'data' , function ( newFile ) {
44
- should . exist ( newFile ) ;
45
- should . exist ( newFile . contents ) ;
51
+ it ( 'should replace regex on a buffer with a function' , function ( done ) {
52
+ var stream = replacePlugin ( / w o r l d / g, function ( ) { return 'person' ; } ) ;
53
+ stream . on ( 'data' , function ( newFile ) {
54
+ should . exist ( newFile ) ;
55
+ should . exist ( newFile . contents ) ;
46
56
47
- String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
48
- done ( ) ;
57
+ String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
58
+ done ( ) ;
59
+ } ) ;
60
+
61
+ stream . write ( file ) ;
62
+ stream . end ( ) ;
49
63
} ) ;
50
64
51
- stream . write ( file ) ;
52
- stream . end ( ) ;
65
+ it ( 'should replace string on a buffer with a function' , function ( done ) {
66
+ var stream = replacePlugin ( 'world' , function ( ) { return 'person' ; } ) ;
67
+ stream . on ( 'data' , function ( newFile ) {
68
+ should . exist ( newFile ) ;
69
+ should . exist ( newFile . contents ) ;
70
+
71
+ String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
72
+ done ( ) ;
73
+ } ) ;
74
+
75
+ stream . write ( file ) ;
76
+ stream . end ( ) ;
77
+ } ) ;
78
+
79
+
80
+ it ( 'should call function once for each replacement when replacing a string on a buffer' , function ( done ) {
81
+ var replacements = [
82
+ 'cow' ,
83
+ 'chicken' ,
84
+ 'duck' ,
85
+ 'person'
86
+ ] ;
87
+ var stream = replacePlugin ( 'world' , function ( ) { return replacements . shift ( ) ; } ) ;
88
+ stream . on ( 'data' , function ( newFile ) {
89
+ should . exist ( newFile ) ;
90
+ should . exist ( newFile . contents ) ;
91
+
92
+ String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
93
+ done ( ) ;
94
+ } ) ;
95
+
96
+ stream . write ( file ) ;
97
+ stream . end ( ) ;
98
+ } ) ;
99
+
100
+
101
+ it ( 'should call function once for each replacement when replacing a regex on a buffer' , function ( done ) {
102
+ var replacements = [
103
+ 'cow' ,
104
+ 'chicken' ,
105
+ 'duck' ,
106
+ 'person'
107
+ ] ;
108
+ var stream = replacePlugin ( / w o r l d / g, function ( ) { return replacements . shift ( ) ; } ) ;
109
+ stream . on ( 'data' , function ( newFile ) {
110
+ should . exist ( newFile ) ;
111
+ should . exist ( newFile . contents ) ;
112
+
113
+ String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
114
+ done ( ) ;
115
+ } ) ;
116
+
117
+ stream . write ( file ) ;
118
+ stream . end ( ) ;
119
+ } ) ;
120
+
121
+ it ( 'should trigger events on a buffer' , function ( done ) {
122
+ var stream = replacePlugin ( 'world' , 'elephant' )
123
+ . on ( 'finish' , function ( ) {
124
+ // No assertion required, we should end up here, if we don't the test will time out
125
+ done ( ) ;
126
+ } ) ;
127
+
128
+ stream . write ( file ) ;
129
+ stream . end ( ) ;
130
+ } ) ;
53
131
} ) ;
54
132
55
- it ( 'should replace regex on a stream' , function ( done ) {
133
+ it ( 'should replace string on a stream' , function ( done ) {
56
134
var file = new File ( {
57
135
path : 'test/fixtures/helloworld.txt' ,
58
136
cwd : 'test/' ,
59
137
base : 'test/fixtures' ,
60
138
contents : fs . createReadStream ( 'test/fixtures/helloworld.txt' )
61
139
} ) ;
62
140
63
- var stream = replacePlugin ( / w o r l d / g , 'person' ) ;
141
+ var stream = replacePlugin ( ' world' , 'person' ) ;
64
142
stream . on ( 'data' , function ( newFile ) {
65
143
should . exist ( newFile ) ;
66
144
should . exist ( newFile . contents ) ;
@@ -75,27 +153,30 @@ describe('gulp-replace', function() {
75
153
stream . end ( ) ;
76
154
} ) ;
77
155
78
- it ( 'should replace regex on a buffer ' , function ( done ) {
156
+ it ( 'should replace regex on a stream ' , function ( done ) {
79
157
var file = new File ( {
80
158
path : 'test/fixtures/helloworld.txt' ,
81
159
cwd : 'test/' ,
82
160
base : 'test/fixtures' ,
83
- contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
161
+ contents : fs . createReadStream ( 'test/fixtures/helloworld.txt' )
84
162
} ) ;
85
163
86
164
var stream = replacePlugin ( / w o r l d / g, 'person' ) ;
87
165
stream . on ( 'data' , function ( newFile ) {
88
166
should . exist ( newFile ) ;
89
167
should . exist ( newFile . contents ) ;
90
168
91
- String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
92
- done ( ) ;
169
+ newFile . contents . pipe ( concatStream ( { encoding : 'string' } , function ( data ) {
170
+ data . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
171
+ done ( ) ;
172
+ } ) ) ;
93
173
} ) ;
94
174
95
175
stream . write ( file ) ;
96
176
stream . end ( ) ;
97
177
} ) ;
98
178
179
+
99
180
it ( 'should replace regex on a stream with a function' , function ( done ) {
100
181
var file = new File ( {
101
182
path : 'test/fixtures/helloworld.txt' ,
@@ -119,26 +200,7 @@ describe('gulp-replace', function() {
119
200
stream . end ( ) ;
120
201
} ) ;
121
202
122
- it ( 'should replace regex on a buffer with a function' , function ( done ) {
123
- var file = new File ( {
124
- path : 'test/fixtures/helloworld.txt' ,
125
- cwd : 'test/' ,
126
- base : 'test/fixtures' ,
127
- contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
128
- } ) ;
129
203
130
- var stream = replacePlugin ( / w o r l d / g, function ( ) { return 'person' ; } ) ;
131
- stream . on ( 'data' , function ( newFile ) {
132
- should . exist ( newFile ) ;
133
- should . exist ( newFile . contents ) ;
134
-
135
- String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
136
- done ( ) ;
137
- } ) ;
138
-
139
- stream . write ( file ) ;
140
- stream . end ( ) ;
141
- } ) ;
142
204
143
205
it ( 'should replace string on a stream with a function' , function ( done ) {
144
206
var file = new File ( {
@@ -163,26 +225,7 @@ describe('gulp-replace', function() {
163
225
stream . end ( ) ;
164
226
} ) ;
165
227
166
- it ( 'should replace string on a buffer with a function' , function ( done ) {
167
- var file = new File ( {
168
- path : 'test/fixtures/helloworld.txt' ,
169
- cwd : 'test/' ,
170
- base : 'test/fixtures' ,
171
- contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
172
- } ) ;
173
228
174
- var stream = replacePlugin ( 'world' , function ( ) { return 'person' ; } ) ;
175
- stream . on ( 'data' , function ( newFile ) {
176
- should . exist ( newFile ) ;
177
- should . exist ( newFile . contents ) ;
178
-
179
- String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/helloworld.txt' , 'utf8' ) ) ;
180
- done ( ) ;
181
- } ) ;
182
-
183
- stream . write ( file ) ;
184
- stream . end ( ) ;
185
- } ) ;
186
229
187
230
it ( 'should call function once for each replacement when replacing a string on a stream' , function ( done ) {
188
231
var file = new File ( {
@@ -242,59 +285,9 @@ describe('gulp-replace', function() {
242
285
stream . end ( ) ;
243
286
} ) ;
244
287
245
- it ( 'should call function once for each replacement when replacing a string on a buffer' , function ( done ) {
246
- var file = new File ( {
247
- path : 'test/fixtures/helloworld.txt' ,
248
- cwd : 'test/' ,
249
- base : 'test/fixtures' ,
250
- contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
251
- } ) ;
252
288
253
- var replacements = [
254
- 'cow' ,
255
- 'chicken' ,
256
- 'duck' ,
257
- 'person'
258
- ] ;
259
- var stream = replacePlugin ( 'world' , function ( ) { return replacements . shift ( ) ; } ) ;
260
- stream . on ( 'data' , function ( newFile ) {
261
- should . exist ( newFile ) ;
262
- should . exist ( newFile . contents ) ;
263
289
264
- String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
265
- done ( ) ;
266
- } ) ;
267
290
268
- stream . write ( file ) ;
269
- stream . end ( ) ;
270
- } ) ;
271
-
272
- it ( 'should call function once for each replacement when replacing a regex on a buffer' , function ( done ) {
273
- var file = new File ( {
274
- path : 'test/fixtures/helloworld.txt' ,
275
- cwd : 'test/' ,
276
- base : 'test/fixtures' ,
277
- contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
278
- } ) ;
279
-
280
- var replacements = [
281
- 'cow' ,
282
- 'chicken' ,
283
- 'duck' ,
284
- 'person'
285
- ] ;
286
- var stream = replacePlugin ( / w o r l d / g, function ( ) { return replacements . shift ( ) ; } ) ;
287
- stream . on ( 'data' , function ( newFile ) {
288
- should . exist ( newFile ) ;
289
- should . exist ( newFile . contents ) ;
290
-
291
- String ( newFile . contents ) . should . equal ( fs . readFileSync ( 'test/expected/hellofarm.txt' , 'utf8' ) ) ;
292
- done ( ) ;
293
- } ) ;
294
-
295
- stream . write ( file ) ;
296
- stream . end ( ) ;
297
- } ) ;
298
291
299
292
it ( 'should ignore binary files when skipBinary is enabled' , function ( done ) {
300
293
var file = new File ( {
@@ -340,22 +333,6 @@ describe('gulp-replace', function() {
340
333
stream . end ( ) ;
341
334
} ) ;
342
335
343
- it ( 'should trigger events on a buffer' , function ( done ) {
344
- var file = new File ( {
345
- path : 'test/fixtures/helloworld.txt' ,
346
- cwd : 'test/' ,
347
- base : 'test/fixtures' ,
348
- contents : fs . readFileSync ( 'test/fixtures/helloworld.txt' )
349
- } ) ;
350
-
351
- var stream = replacePlugin ( 'world' , 'elephant' )
352
- . on ( 'finish' , function ( ) {
353
- // No assertion required, we should end up here, if we don't the test will time out
354
- done ( ) ;
355
- } ) ;
356
336
357
- stream . write ( file ) ;
358
- stream . end ( ) ;
359
- } ) ;
360
337
} ) ;
361
338
} ) ;
0 commit comments