Skip to content

Commit 66c67e1

Browse files
sttkphated
authored andcommitted
Update: Use gulp-test-tools for testing (#98)
1 parent 1d27f81 commit 66c67e1

23 files changed

+312
-405
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"fs-extra": "^0.26.1",
6262
"github-changes": "^1.0.1",
6363
"gulp": "gulpjs/gulp#4.0",
64+
"gulp-test-tools": "^0.5.2",
6465
"jscs": "^2.3.5",
6566
"jscs-preset-gulp": "^1.0.0",
6667
"lab": "^6.2.0",

test/completion.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,33 @@
11
'use strict';
22

33
var lab = exports.lab = require('lab').script();
4-
var code = require('code');
5-
6-
var child = require('child_process');
7-
8-
var fs = require('fs-extra');
9-
var path = require('path');
10-
11-
var outfile = path.resolve(__dirname, 'output/completion.out');
4+
var expect = require('code').expect;
5+
var runner = require('gulp-test-tools').gulpRunner;
126

137
lab.experiment('flag: --completion', function() {
148

15-
lab.before(function(done) {
16-
fs.mkdirpSync(path.resolve(__dirname, 'output'));
17-
done();
18-
});
19-
20-
lab.after(function(done) {
21-
fs.remove(path.resolve(__dirname, 'output'));
22-
done();
23-
});
24-
259
['bash', 'fish', 'powershell', 'zsh'].forEach(function(type) {
2610
lab.test('returns completion script for ' + type, function(done) {
27-
child.exec('node ' + __dirname + '/../bin/gulp.js --completion=' + type + ' > ' + outfile, function(err) {
28-
var stdout = fs.readFileSync(outfile, { encoding: 'utf8' });
29-
code.expect(stdout).to.contain('gulp --completion=' + type);
11+
runner({ verbose: false })
12+
.gulp('--completion=' + type)
13+
.run(cb);
14+
15+
function cb(err, stdout) {
16+
expect(stdout).to.contain('gulp --completion=' + type);
3017
done(err);
31-
});
18+
}
3219
});
3320
});
3421

3522
lab.test('shows error message for unknown completion type', function(done) {
36-
child.exec('node ' + __dirname + '/../bin/gulp.js --completion=unknown > ' + outfile, function() {
37-
var stdout = fs.readFileSync(outfile, { encoding: 'utf8' });
38-
code.expect(stdout).to.contain('rules for \'unknown\' not found');
23+
runner({ verbose: false })
24+
.gulp('--completion=unknown')
25+
.run(cb);
26+
27+
function cb(err, stdout) {
28+
expect(stdout).to.contain('rules for \'unknown\' not found');
3929
done();
40-
});
30+
}
4131
});
4232

4333
});

test/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ var expect = require('code').expect;
55
var path = require('path');
66
var fs = require('fs');
77

8-
var skipLines = require('./tools/skip-lines');
9-
var eraseTime = require('./tools/erase-time');
10-
var runner = require('./tools/run-gulp');
8+
var skipLines = require('gulp-test-tools').skipLines;
9+
var eraseTime = require('gulp-test-tools').eraseTime;
10+
var runner = require('gulp-test-tools').gulpRunner;
1111

1212
var fixturesDir = path.join(__dirname, 'fixtures', 'config');
1313
var expectedDir = path.join(__dirname, 'expected', 'config');

test/exports-as-tasks.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ var lab = exports.lab = require('lab').script();
44
var expect = require('code').expect;
55
var fs = require('fs');
66
var path = require('path');
7-
var skipLines = require('./tools/skip-lines');
8-
var eraseTime = require('./tools/erase-time');
9-
var runner = require('./tools/run-gulp');
7+
var skipLines = require('gulp-test-tools').skipLines;
8+
var eraseTime = require('gulp-test-tools').eraseTime;
9+
var runner = require('gulp-test-tools').gulpRunner;
1010

1111
var expectedDir = path.join(__dirname, 'expected');
1212

@@ -16,7 +16,7 @@ lab.experiment('exports as tasks', { timeout: 0 }, function() {
1616
lab.test('prints the task list', function(done) {
1717
runner({ verbose: false })
1818
.gulp('--tasks',
19-
'--gulpfile ./fixtures/gulpfiles/gulpfile-exports.babel.js')
19+
'--gulpfile ./test/fixtures/gulpfiles/gulpfile-exports.babel.js')
2020
.run(cb);
2121

2222
function cb(err, stdout) {

test/fixtures/gulpfiles/gulpfile-2.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22
var gulp = require('gulp');
33

4-
function noop(cb) {
4+
function logGulpfilePath(cb) {
5+
console.log(__filename);
56
return cb();
67
}
78

8-
gulp.task('default', gulp.series(noop));
9+
gulp.task('default', gulp.series(logGulpfilePath));

test/fixtures/gulpfiles/gulpfile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ var gulp = require('gulp');
55
function noop(cb) {
66
return cb();
77
}
8-
function described() {}
8+
function described(cb) {
9+
cb();
10+
}
911
function errorFunction() {
1012
throw new Error('Error!');
1113
}

test/flags-continue.js

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,64 @@
11
'use strict';
22

33
var lab = exports.lab = require('lab').script();
4-
var code = require('code');
5-
6-
var child = require('child_process');
4+
var expect = require('code').expect;
5+
var runner = require('gulp-test-tools').gulpRunner;
6+
var eraseTime = require('gulp-test-tools').eraseTime;
7+
var eraseLapse = require('gulp-test-tools').eraseLapse;
8+
var skipLines = require('gulp-test-tools').skipLines;
9+
var headLines = require('gulp-test-tools').headLines;
710

811
lab.experiment('flag: --continue', function() {
912

1013
lab.test('continues execution when flag is set', function(done) {
11-
child.exec('node ' + __dirname + '/../bin/gulp.js test4 --continue --cwd ./test/fixtures/gulpfiles', function(err, stdout, stderr) {
12-
code.expect(stdout).to.contain('Starting \'errorFunction\'');
13-
code.expect(stderr).to.contain('\'errorFunction\' errored after');
14-
stdout = stdout.replace(/\\/g, '/').split('\n');
15-
code.expect(stdout[4]).to.contain('Starting \'anon\'');
16-
code.expect(stdout[5]).to.contain('Finished \'anon\'');
14+
runner({ verbose: false })
15+
.gulp('test4', '--continue', '--cwd ./test/fixtures/gulpfiles')
16+
.run(cb);
17+
18+
function cb(err, stdout, stderr) {
19+
expect(err).to.be.not.null();
20+
21+
stdout = eraseLapse(eraseTime(skipLines(stdout, 2)));
22+
expect(stdout).to.equal(
23+
'Starting \'test4\'...\n' +
24+
'Starting \'errorFunction\'...\n' +
25+
'Starting \'anon\'...\n' +
26+
'Finished \'anon\' after ?\n' +
27+
''
28+
);
29+
30+
stderr = eraseLapse(eraseTime(headLines(stderr, 2)));
31+
expect(stderr).to.equal(
32+
'\'errorFunction\' errored after ?\n' +
33+
'Error: Error!'
34+
);
1735
done();
18-
});
36+
}
1937
});
2038

2139
lab.test('stops execution when flag is not set', function(done) {
22-
child.exec('node ' + __dirname + '/../bin/gulp.js test4 --cwd ./test/fixtures/gulpfiles', function(err, stdout, stderr) {
23-
code.expect(stdout).to.contain('Starting \'errorFunction\'');
24-
code.expect(stderr).to.contain('\'errorFunction\' errored after');
25-
code.expect(stdout[4]).to.not.contain('Starting \'anon\'');
40+
runner({ verbose: false })
41+
.gulp('test4', '--cwd ./test/fixtures/gulpfiles')
42+
.run(cb);
43+
44+
function cb(err, stdout, stderr) {
45+
expect(err).to.be.not.null();
46+
47+
expect(stdout).to.not.contain('Starting \'anon\'');
48+
stdout = eraseLapse(eraseTime(skipLines(stdout, 2)));
49+
expect(stdout).to.equal(
50+
'Starting \'test4\'...\n' +
51+
'Starting \'errorFunction\'...\n' +
52+
''
53+
);
54+
55+
stderr = eraseLapse(eraseTime(headLines(stderr, 2)));
56+
expect(stderr).to.equal(
57+
'\'errorFunction\' errored after ?\n' +
58+
'Error: Error!'
59+
);
2660
done();
27-
});
61+
}
2862
});
2963

3064
});

test/flags-gulpfile.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
'use strict';
22

33
var lab = exports.lab = require('lab').script();
4-
var code = require('code');
5-
6-
var child = require('child_process');
4+
var expect = require('code').expect;
5+
var runner = require('gulp-test-tools').gulpRunner;
6+
var skipLines = require('gulp-test-tools').skipLines;
7+
var headLines = require('gulp-test-tools').headLines;
8+
var eraseTime = require('gulp-test-tools').eraseTime;
9+
var eraseLapse = require('gulp-test-tools').eraseLapse;
10+
var path = require('path');
711

812
lab.experiment('flag: --gulpfile', function() {
913

1014
lab.test('Manually set path of gulpfile', function(done) {
11-
child.exec('node ' + __dirname + '/../bin/gulp.js --gulpfile "./test/fixtures/gulpfiles/gulpfile-2.js"', function(err, stdout) {
12-
stdout = stdout.replace(/\\/g, '/').split('\n');
13-
code.expect(stdout[1]).to.contain('test/fixtures/gulpfiles/gulpfile-2.js');
14-
code.expect(stdout[5]).to.contain('Finished \'default\'');
15+
var gulpfilePath = 'test/fixtures/gulpfiles/gulpfile-2.js';
16+
17+
runner({ verbose: false })
18+
.gulp('--gulpfile', gulpfilePath)
19+
.run(cb);
20+
21+
function cb(err, stdout) {
22+
var chgWorkdirLog = headLines(stdout, 1);
23+
var workdir = path.dirname(gulpfilePath).replace(/\//g, path.sep);
24+
expect(chgWorkdirLog).to.contain('Working directory changed to ');
25+
expect(chgWorkdirLog).to.contain(workdir);
26+
27+
stdout = eraseLapse(eraseTime(skipLines(stdout, 2)));
28+
expect(stdout).to.equal(
29+
'Starting \'default\'...\n' +
30+
'Starting \'logGulpfilePath\'...\n' +
31+
path.resolve(gulpfilePath) + '\n' +
32+
'Finished \'logGulpfilePath\' after ?\n' +
33+
'Finished \'default\' after ?\n' +
34+
''
35+
);
1536
done(err);
16-
});
37+
}
1738
});
1839

1940
});

test/flags-help.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
'use strict';
22

33
var lab = exports.lab = require('lab').script();
4-
var code = require('code');
5-
6-
var fs = require('fs-extra');
7-
var child = require('child_process');
4+
var expect = require('code').expect;
5+
var runner = require('gulp-test-tools').gulpRunner;
86

97
var path = require('path');
10-
var outfile = path.resolve(__dirname, 'output/flags-help.out');
11-
12-
var output = fs.readFileSync(__dirname + '/expected/flags-help.txt', 'utf8').replace(/(\r\n|\n|\r)\s?/gm,'\n');
8+
var fs = require('fs');
139

14-
lab.experiment('flag: --help', function() {
10+
// Erases a first space inserted by `chalk`.
11+
function eraseFirstSpace(s) {
12+
return s.replace(/^(\r\n|\n|\r)\s?/g, '\n');
13+
}
1514

16-
lab.before(function(done) {
17-
fs.mkdirpSync(path.resolve(__dirname, 'output'));
18-
done();
19-
});
15+
var outputFile = path.join(__dirname, 'expected/flags-help.txt');
16+
var outputText = fs.readFileSync(outputFile, 'utf8');
2017

21-
lab.after(function(done) {
22-
fs.removeSync(path.resolve(__dirname, 'output'));
23-
done();
24-
});
18+
lab.experiment('flag: --help', function() {
2519

2620
lab.test('shows help using --help', function(done) {
27-
child.exec('node ' + __dirname + '/../bin/gulp.js --help --cwd ./test/fixtures/gulpfiles > ' + outfile, function(err) {
28-
var stdout = fs.readFileSync(outfile, { encoding: 'utf8' });
29-
code.expect(stdout.replace(/(\r\n|\n|\r)\s?/gm,'\n')).to.equals(output);
21+
runner({ verbose: false })
22+
.gulp('--help', '--cwd ./test/fixtures/gulpfiles')
23+
.run(cb);
24+
25+
function cb(err, stdout) {
26+
stdout = eraseFirstSpace(stdout);
27+
expect(stdout).to.equal(outputText);
3028
done(err);
31-
});
29+
}
3230
});
3331

3432
lab.test('shows help using short --h', function(done) {
35-
child.exec('node ' + __dirname + '/../bin/gulp.js --h --cwd ./test/fixtures/gulpfiles > ' + outfile, function(err) {
36-
var stdout = fs.readFileSync(outfile, { encoding: 'utf8' });
37-
code.expect(stdout.replace(/(\r\n|\n|\r)\s?/gm,'\n')).to.equals(output);
33+
runner({ verbose: false })
34+
.gulp('--h', '--cwd ./test/fixtures/gulpfiles')
35+
.run(cb);
36+
37+
function cb(err, stdout) {
38+
stdout = eraseFirstSpace(stdout);
39+
expect(stdout).to.equal(outputText);
3840
done(err);
39-
});
41+
}
4042
});
4143

4244
});

0 commit comments

Comments
 (0)