Skip to content

Commit 277ec38

Browse files
author
Gromych
committed
Moves stream on data callback with stream writing outside the test cases
1 parent d3fa6ec commit 277ec38

File tree

1 file changed

+51
-83
lines changed

1 file changed

+51
-83
lines changed

test/main.js

+51-83
Original file line numberDiff line numberDiff line change
@@ -20,86 +20,78 @@ describe('gulp-replace', function() {
2020
});
2121

2222
describe('buffered input', function () {
23-
var file;
23+
var file, check;
2424

2525
beforeEach(function () {
2626
file = new File({
2727
path: 'test/fixtures/helloworld.txt',
2828
contents: fs.readFileSync('test/fixtures/helloworld.txt')
2929
});
30+
31+
check = function (stream, done, cb) {
32+
stream.on('data', function (newFile) {
33+
cb(newFile);
34+
done();
35+
});
36+
37+
stream.write(file);
38+
stream.end();
39+
};
3040
});
3141

3242
it('should replace string on a buffer', function(done) {
3343
var stream = replacePlugin('world', 'person');
34-
stream.on('data', function(newFile) {
44+
45+
check(stream, done, function (newFile) {
3546
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
36-
done();
3747
});
38-
39-
stream.write(file);
40-
stream.end();
4148
});
4249

4350
it('should replace regex on a buffer', function(done) {
4451
var stream = replacePlugin(/world/g, 'person');
45-
stream.on('data', function(newFile) {
52+
53+
check(stream, done, function (newFile) {
4654
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
47-
done();
4855
});
49-
50-
stream.write(file);
51-
stream.end();
5256
});
5357

5458
it('should replace regex on a buffer with a function', function(done) {
5559
var stream = replacePlugin(/world/g, function() { return 'person'; });
56-
stream.on('data', function(newFile) {
60+
61+
check(stream, done, function (newFile) {
5762
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
58-
done();
5963
});
60-
61-
stream.write(file);
62-
stream.end();
6364
});
6465

6566
it('should replace string on a buffer with a function', function(done) {
6667
var stream = replacePlugin('world', function() { return 'person'; });
67-
stream.on('data', function(newFile) {
68+
69+
check(stream, done, function (newFile) {
6870
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
69-
done();
7071
});
71-
72-
stream.write(file);
73-
stream.end();
7472
});
7573

7674

7775
it('should call function once for each replacement when replacing a string on a buffer', function(done) {
7876
var stream = replacePlugin('world', function() { return replacements.shift(); });
79-
stream.on('data', function(newFile) {
77+
check(stream, done, function (newFile) {
78+
8079
String(newFile.contents).should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
81-
done();
8280
});
83-
84-
stream.write(file);
85-
stream.end();
8681
});
8782

8883

8984
it('should call function once for each replacement when replacing a regex on a buffer', function(done) {
9085
var stream = replacePlugin(/world/g, function() { return replacements.shift(); });
91-
stream.on('data', function(newFile) {
86+
87+
check(stream, done, function (newFile) {
9288
String(newFile.contents).should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
93-
done();
9489
});
95-
96-
stream.write(file);
97-
stream.end();
9890
});
9991

10092
it('should trigger events on a buffer', function(done) {
10193
var stream = replacePlugin('world', 'elephant')
102-
.on('finish', function() {
94+
stream.on('finish', function() {
10395
// No assertion required, we should end up here, if we don't the test will time out
10496
done();
10597
});
@@ -110,91 +102,67 @@ describe('gulp-replace', function() {
110102
});
111103

112104
describe('streamed input', function () {
113-
var file;
105+
var file, check;
114106

115107
beforeEach(function () {
116108
file = new File({
117-
path: 'test/fixtures/helloworld.txt',
118-
contents: fs.createReadStream('test/fixtures/helloworld.txt')
109+
path: 'test/fixtures/helloworld.txt',
110+
contents: fs.createReadStream('test/fixtures/helloworld.txt')
111+
});
112+
113+
check = function (stream, done, cb) {
114+
stream.on('data', function(newFile) {
115+
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
116+
cb(data);
117+
done();
118+
}));
119119
});
120+
121+
stream.write(file);
122+
stream.end();
123+
};
120124
});
121125

122126
it('should replace string on a stream', function(done) {
123127
var stream = replacePlugin('world', 'person');
124-
stream.on('data', function(newFile) {
125-
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
126-
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
127-
done();
128-
}));
128+
check(stream, done, function (data) {
129+
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
129130
});
130-
131-
stream.write(file);
132-
stream.end();
133131
});
134132

135133
it('should replace regex on a stream', function(done) {
136134
var stream = replacePlugin(/world/g, 'person');
137-
stream.on('data', function(newFile) {
138-
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
139-
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
140-
done();
141-
}));
135+
check(stream, done, function (data) {
136+
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
142137
});
143-
144-
stream.write(file);
145-
stream.end();
146138
});
147139

148140
it('should replace regex on a stream with a function', function(done) {
149141
var stream = replacePlugin(/world/g, function() { return 'person'; });
150-
stream.on('data', function(newFile) {
151-
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
152-
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
153-
done();
154-
}));
142+
check(stream, done, function (data) {
143+
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
155144
});
156-
157-
stream.write(file);
158-
stream.end();
159145
});
160146

161147
it('should replace string on a stream with a function', function(done) {
162148
var stream = replacePlugin('world', function() { return 'person'; });
163-
stream.on('data', function(newFile) {
164-
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
165-
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
166-
done();
167-
}));
149+
check(stream, done, function (data) {
150+
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
168151
});
169-
170-
stream.write(file);
171-
stream.end();
172152
});
173153

174154
it('should call function once for each replacement when replacing a string on a stream', function(done) {
175155
var stream = replacePlugin('world', function() { return replacements.shift(); });
176-
stream.on('data', function(newFile) {
177-
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
178-
data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
179-
done();
180-
}));
156+
check(stream, done, function (data) {
157+
data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
181158
});
182-
183-
stream.write(file);
184-
stream.end();
185159
});
186160

187161
it('should call function once for each replacement when replacing a regex on a stream', function(done) {
188162
var stream = replacePlugin(/world/g, function() { return replacements.shift(); });
189-
stream.on('data', function(newFile) {
190-
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
191-
data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
192-
done();
193-
}));
163+
check(stream, done, function (data) {
164+
data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
194165
});
195-
196-
stream.write(file);
197-
stream.end();
198166
});
199167
});
200168

0 commit comments

Comments
 (0)