Skip to content

Commit dde108e

Browse files
committedDec 14, 2019
test/style: refactor test-task to make it more readable
1 parent dc54952 commit dde108e

File tree

6 files changed

+127
-109
lines changed

6 files changed

+127
-109
lines changed
 

‎Gruntfile.js

+2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ module.exports = function(grunt) {
263263

264264
this.registerTask('amd', ['babel:amd', 'requirejs']);
265265

266+
this.registerTask('test', ['test:bin', 'test:cov', 'test:check-cov']);
267+
266268
grunt.registerTask('bench', ['metrics']);
267269

268270
if (process.env.SAUCE_USERNAME) {

‎tasks/.eslintrc.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module.exports = {
77
},
88
rules: {
99
'no-process-env': 'off',
10-
'prefer-const': 'warn'
10+
'prefer-const': 'warn',
11+
'compat/compat': 'off',
12+
'dot-notation': ['error', { allowKeywords: true }]
1113
}
1214
};

‎tasks/parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = function(grunt) {
2929
]
3030
.map(grunt.file.read)
3131
.join('');
32-
grunt.file['delete']('handlebars.js');
32+
grunt.file.delete('handlebars.js');
3333

3434
grunt.file.write('lib/handlebars/compiler/parser.js', src);
3535
grunt.log.writeln('Parser "lib/handlebars/compiler/parser.js" created.');

‎tasks/test-bin.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const childProcess = require('child_process'),
2+
fs = require('fs'),
3+
os = require('os'),
4+
expect = require('chai').expect,
5+
util = require('util');
6+
7+
const readFile = util.promisify(fs.readFile);
8+
const execFile = util.promisify(childProcess.execFile);
9+
10+
module.exports = function(grunt) {
11+
grunt.registerTask(
12+
'test:bin',
13+
wrapAsync(async function() {
14+
const { stdout } = await execFileWithWin32Fallback('./bin/handlebars', [
15+
'-a',
16+
'spec/artifacts/empty.handlebars'
17+
]);
18+
19+
const expectedOutput = await readFile(
20+
'./spec/expected/empty.amd.js',
21+
'utf-8'
22+
);
23+
24+
const normalizedOutput = normalizeCrlf(stdout);
25+
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
26+
expect(normalizedOutput).to.equal(normalizedExpectedOutput);
27+
})
28+
);
29+
30+
async function execFileWithWin32Fallback(command, args) {
31+
// On Windows, the executable handlebars.js file cannot be run directly
32+
if (os.platform() === 'win32') {
33+
args.unshift(command);
34+
command = process.argv[0];
35+
}
36+
return execFile(command, args, { encoding: 'utf-8' });
37+
}
38+
39+
function normalizeCrlf(string) {
40+
if (string != null) {
41+
return string.replace(/\r\n/g, '\n');
42+
}
43+
return string;
44+
}
45+
46+
function wrapAsync(asyncFunction) {
47+
return function() {
48+
asyncFunction()
49+
.catch(error => {
50+
grunt.fatal(error);
51+
})
52+
.finally(this.async());
53+
};
54+
}
55+
};

‎tasks/test-mocha.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const childProcess = require('child_process');
2+
3+
module.exports = function(grunt) {
4+
grunt.registerTask(
5+
'test:mocha',
6+
promiseBasedTask(async () => forkAndWait('./spec/env/runner'))
7+
);
8+
9+
grunt.registerTask(
10+
'test:cov',
11+
promiseBasedTask(async () =>
12+
forkAndWait(
13+
'node_modules/istanbul/lib/cli.js',
14+
'cover',
15+
'--source-map',
16+
'--',
17+
'./spec/env/runner.js'
18+
)
19+
)
20+
);
21+
22+
grunt.registerTask(
23+
'test:min',
24+
promiseBasedTask(async () => forkAndWait('./spec/env/runner', '--min'))
25+
);
26+
27+
grunt.registerTask(
28+
'test:check-cov',
29+
promiseBasedTask(() =>
30+
forkAndWait(
31+
'node_modules/istanbul/lib/cli.js',
32+
'check-coverage',
33+
'--statements',
34+
'100',
35+
'--functions',
36+
'100',
37+
'--branches',
38+
'100',
39+
'--lines 100'
40+
)
41+
)
42+
);
43+
44+
function promiseBasedTask(asyncFunction) {
45+
return function() {
46+
asyncFunction()
47+
.catch(error => {
48+
grunt.fatal(error);
49+
})
50+
.finally(this.async());
51+
};
52+
}
53+
54+
async function forkAndWait(command, ...args) {
55+
return new Promise((resolve, reject) => {
56+
const child = childProcess.fork(command, args, { stdio: 'inherit' });
57+
child.on('close', code => {
58+
if (code !== 0) {
59+
reject(new Error(`Child process failed with exit-code ${code}`));
60+
}
61+
});
62+
});
63+
}
64+
65+
grunt.registerTask('test', ['test:bin', 'test:cov', 'test:check-cov']);
66+
};

‎tasks/test.js

-107
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.