test/style: refactor test-task to make it more readable

This commit is contained in:
Nils Knappmeier
2019-12-12 01:00:57 +01:00
committed by Nils Knappmeier
parent dc5495216d
commit dde108e283
6 changed files with 127 additions and 109 deletions
+2
View File
@@ -263,6 +263,8 @@ module.exports = function(grunt) {
this.registerTask('amd', ['babel:amd', 'requirejs']);
this.registerTask('test', ['test:bin', 'test:cov', 'test:check-cov']);
grunt.registerTask('bench', ['metrics']);
if (process.env.SAUCE_USERNAME) {
+3 -1
View File
@@ -7,6 +7,8 @@ module.exports = {
},
rules: {
'no-process-env': 'off',
'prefer-const': 'warn'
'prefer-const': 'warn',
'compat/compat': 'off',
'dot-notation': ['error', { allowKeywords: true }]
}
};
+1 -1
View File
@@ -29,7 +29,7 @@ module.exports = function(grunt) {
]
.map(grunt.file.read)
.join('');
grunt.file['delete']('handlebars.js');
grunt.file.delete('handlebars.js');
grunt.file.write('lib/handlebars/compiler/parser.js', src);
grunt.log.writeln('Parser "lib/handlebars/compiler/parser.js" created.');
+55
View File
@@ -0,0 +1,55 @@
const childProcess = require('child_process'),
fs = require('fs'),
os = require('os'),
expect = require('chai').expect,
util = require('util');
const readFile = util.promisify(fs.readFile);
const execFile = util.promisify(childProcess.execFile);
module.exports = function(grunt) {
grunt.registerTask(
'test:bin',
wrapAsync(async function() {
const { stdout } = await execFileWithWin32Fallback('./bin/handlebars', [
'-a',
'spec/artifacts/empty.handlebars'
]);
const expectedOutput = await readFile(
'./spec/expected/empty.amd.js',
'utf-8'
);
const normalizedOutput = normalizeCrlf(stdout);
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
expect(normalizedOutput).to.equal(normalizedExpectedOutput);
})
);
async function execFileWithWin32Fallback(command, args) {
// On Windows, the executable handlebars.js file cannot be run directly
if (os.platform() === 'win32') {
args.unshift(command);
command = process.argv[0];
}
return execFile(command, args, { encoding: 'utf-8' });
}
function normalizeCrlf(string) {
if (string != null) {
return string.replace(/\r\n/g, '\n');
}
return string;
}
function wrapAsync(asyncFunction) {
return function() {
asyncFunction()
.catch(error => {
grunt.fatal(error);
})
.finally(this.async());
};
}
};
+66
View File
@@ -0,0 +1,66 @@
const childProcess = require('child_process');
module.exports = function(grunt) {
grunt.registerTask(
'test:mocha',
promiseBasedTask(async () => forkAndWait('./spec/env/runner'))
);
grunt.registerTask(
'test:cov',
promiseBasedTask(async () =>
forkAndWait(
'node_modules/istanbul/lib/cli.js',
'cover',
'--source-map',
'--',
'./spec/env/runner.js'
)
)
);
grunt.registerTask(
'test:min',
promiseBasedTask(async () => forkAndWait('./spec/env/runner', '--min'))
);
grunt.registerTask(
'test:check-cov',
promiseBasedTask(() =>
forkAndWait(
'node_modules/istanbul/lib/cli.js',
'check-coverage',
'--statements',
'100',
'--functions',
'100',
'--branches',
'100',
'--lines 100'
)
)
);
function promiseBasedTask(asyncFunction) {
return function() {
asyncFunction()
.catch(error => {
grunt.fatal(error);
})
.finally(this.async());
};
}
async function forkAndWait(command, ...args) {
return new Promise((resolve, reject) => {
const child = childProcess.fork(command, args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject(new Error(`Child process failed with exit-code ${code}`));
}
});
});
}
grunt.registerTask('test', ['test:bin', 'test:cov', 'test:check-cov']);
};
-107
View File
@@ -1,107 +0,0 @@
const childProcess = require('child_process'),
fs = require('fs'),
os = require('os');
module.exports = function(grunt) {
grunt.registerTask('test:bin', function() {
const done = this.async();
let cmd = './bin/handlebars';
const args = ['-a', 'spec/artifacts/empty.handlebars'];
// On Windows, the executable handlebars.js file cannot be run directly
if (os.platform() === 'win32') {
args.unshift(cmd);
cmd = process.argv[0];
}
childProcess.execFile(cmd, args, function(err, stdout) {
if (err) {
throw err;
}
const expected = fs
.readFileSync('./spec/expected/empty.amd.js')
.toString()
.replace(/\r\n/g, '\n');
if (stdout.toString() !== expected) {
throw new Error(
'Expected binary output differed:\n\n"' +
stdout +
'"\n\n"' +
expected +
'"'
);
}
done();
});
});
grunt.registerTask('test:mocha', function() {
const done = this.async();
const runner = childProcess.fork('./spec/env/runner', [], {
stdio: 'inherit'
});
runner.on('close', function(code) {
if (code !== 0) {
grunt.fatal(code + ' tests failed');
}
done();
});
});
grunt.registerTask('test:cov', function() {
const done = this.async();
const runner = childProcess.spawn(
'node_modules/istanbul/lib/cli.js',
['cover', '--source-map', '--', './spec/env/runner.js'],
{ stdio: 'inherit' }
);
runner.on('exit', function(code) {
if (code !== 0) {
grunt.fatal(code + ' tests failed');
}
done();
});
});
grunt.registerTask('test:min', function() {
const done = this.async();
const runner = childProcess.fork('./spec/env/runner', ['--min'], {
stdio: 'inherit'
});
runner.on('close', function(code) {
if (code !== 0) {
grunt.fatal(code + ' tests failed');
}
done();
});
});
grunt.registerTask('test:check-cov', function() {
const done = this.async();
const runner = childProcess.fork(
'node_modules/istanbul/lib/cli.js',
[
'check-coverage',
'--statements',
'100',
'--functions',
'100',
'--branches',
'100',
'--lines 100'
],
{ stdio: 'inherit' }
);
runner.on('close', function(code) {
if (code != 0) {
grunt.fatal('Coverage check failed: ' + code);
}
done();
});
});
grunt.registerTask('test', ['test:bin', 'test:cov', 'test:check-cov']);
};