Skip to content

Commit 3f8c0a2

Browse files
author
Gromych
committed
Removes copy-paste of creating file in buffered input mode
1 parent 4775047 commit 3f8c0a2

File tree

1 file changed

+117
-140
lines changed

1 file changed

+117
-140
lines changed

test/main.js

+117-140
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,137 @@ var File = require('vinyl');
88

99
describe('gulp-replace', function() {
1010
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+
});
1721
});
1822

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);
2328

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'));
2630
done();
27-
}));
31+
});
32+
33+
stream.write(file);
34+
stream.end();
2835
});
2936

30-
stream.write(file);
31-
stream.end();
32-
});
37+
it('should replace regex on a buffer', function(done) {
38+
var stream = replacePlugin(/world/g, 'person');
39+
stream.on('data', function(newFile) {
40+
should.exist(newFile);
41+
should.exist(newFile.contents);
3342

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();
4049
});
4150

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(/world/g, function() { return 'person'; });
53+
stream.on('data', function(newFile) {
54+
should.exist(newFile);
55+
should.exist(newFile.contents);
4656

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();
4963
});
5064

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(/world/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+
});
53131
});
54132

55-
it('should replace regex on a stream', function(done) {
133+
it('should replace string on a stream', function(done) {
56134
var file = new File({
57135
path: 'test/fixtures/helloworld.txt',
58136
cwd: 'test/',
59137
base: 'test/fixtures',
60138
contents: fs.createReadStream('test/fixtures/helloworld.txt')
61139
});
62140

63-
var stream = replacePlugin(/world/g, 'person');
141+
var stream = replacePlugin('world', 'person');
64142
stream.on('data', function(newFile) {
65143
should.exist(newFile);
66144
should.exist(newFile.contents);
@@ -75,27 +153,30 @@ describe('gulp-replace', function() {
75153
stream.end();
76154
});
77155

78-
it('should replace regex on a buffer', function(done) {
156+
it('should replace regex on a stream', function(done) {
79157
var file = new File({
80158
path: 'test/fixtures/helloworld.txt',
81159
cwd: 'test/',
82160
base: 'test/fixtures',
83-
contents: fs.readFileSync('test/fixtures/helloworld.txt')
161+
contents: fs.createReadStream('test/fixtures/helloworld.txt')
84162
});
85163

86164
var stream = replacePlugin(/world/g, 'person');
87165
stream.on('data', function(newFile) {
88166
should.exist(newFile);
89167
should.exist(newFile.contents);
90168

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+
}));
93173
});
94174

95175
stream.write(file);
96176
stream.end();
97177
});
98178

179+
99180
it('should replace regex on a stream with a function', function(done) {
100181
var file = new File({
101182
path: 'test/fixtures/helloworld.txt',
@@ -119,26 +200,7 @@ describe('gulp-replace', function() {
119200
stream.end();
120201
});
121202

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-
});
129203

130-
var stream = replacePlugin(/world/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-
});
142204

143205
it('should replace string on a stream with a function', function(done) {
144206
var file = new File({
@@ -163,26 +225,7 @@ describe('gulp-replace', function() {
163225
stream.end();
164226
});
165227

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-
});
173228

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-
});
186229

187230
it('should call function once for each replacement when replacing a string on a stream', function(done) {
188231
var file = new File({
@@ -242,59 +285,9 @@ describe('gulp-replace', function() {
242285
stream.end();
243286
});
244287

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-
});
252288

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);
263289

264-
String(newFile.contents).should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
265-
done();
266-
});
267290

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(/world/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-
});
298291

299292
it('should ignore binary files when skipBinary is enabled', function(done) {
300293
var file = new File({
@@ -340,22 +333,6 @@ describe('gulp-replace', function() {
340333
stream.end();
341334
});
342335

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-
});
356336

357-
stream.write(file);
358-
stream.end();
359-
});
360337
});
361338
});

0 commit comments

Comments
 (0)