Skip to content

Commit

Permalink
test/style: refactor test-task to make it more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Dec 14, 2019
1 parent dc54952 commit dde108e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 109 deletions.
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion tasks/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]
}
};
2 changes: 1 addition & 1 deletion tasks/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
55 changes: 55 additions & 0 deletions tasks/test-bin.js
Original file line number Diff line number Diff line change
@@ -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 changes: 66 additions & 0 deletions tasks/test-mocha.js
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 107 deletions tasks/test.js

This file was deleted.

0 comments on commit dde108e

Please sign in to comment.