/
test-bin.js
146 lines (134 loc) · 3.76 KB
/
test-bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const childProcess = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const chai = require('chai');
chai.use(require('chai-diff'));
const expect = chai.expect;
const testCases = [
{
binInputParameters: ['-a', 'spec/artifacts/empty.handlebars'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.js'
},
{
binInputParameters: [
'-a',
'-f',
'TEST_OUTPUT',
'spec/artifacts/empty.handlebars'
],
outputLocation: 'TEST_OUTPUT',
expectedOutputSpec: './spec/expected/empty.amd.js'
},
{
binInputParameters: [
'-a',
'-n',
'CustomNamespace.templates',
'spec/artifacts/empty.handlebars'
],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
},
{
binInputParameters: [
'-a',
'--namespace',
'CustomNamespace.templates',
'spec/artifacts/empty.handlebars'
],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
},
{
binInputParameters: ['-a', '-s', 'spec/artifacts/empty.handlebars'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.simple.js'
},
{
binInputParameters: ['-a', '-m', 'spec/artifacts/empty.handlebars'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/empty.amd.min.js'
},
{
binInputParameters: [
'spec/artifacts/known.helpers.handlebars',
'-a',
'-k',
'someHelper',
'-k',
'anotherHelper',
'-o'
],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/non.empty.amd.known.helper.js'
},
{
binInputParameters: ['--help'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/help.menu.txt'
},
{
binInputParameters: ['-v'],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/version.txt'
},
{
binInputParameters: [
'-a',
'-e',
'hbs',
'./spec/artifacts/non.default.extension.hbs'
],
outputLocation: 'stdout',
expectedOutputSpec: './spec/expected/non.default.extension.amd.js'
}
];
module.exports = function(grunt) {
grunt.registerTask('test:bin', function() {
testCases.forEach(
({ binInputParameters, outputLocation, expectedOutputSpec }) => {
const stdout = executeBinHandlebars(...binInputParameters);
const expectedOutput = fs.readFileSync(expectedOutputSpec, 'utf-8');
const useStdout = outputLocation === 'stdout';
const normalizedOutput = normalizeCrlf(
useStdout ? stdout : fs.readFileSync(outputLocation, 'utf-8')
);
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
if (!useStdout) {
fs.unlinkSync(outputLocation);
}
expect(normalizedOutput).not.to.be.differentFrom(
normalizedExpectedOutput,
{
relaxedSpace: true
}
);
}
);
});
};
// helper functions
function executeBinHandlebars(...args) {
if (os.platform() === 'win32') {
// On Windows, the executable handlebars.js file cannot be run directly
const nodeJs = process.argv[0];
return execFilesSyncUtf8(nodeJs, ['./bin/handlebars'].concat(args));
}
return execFilesSyncUtf8('./bin/handlebars', args);
}
function execFilesSyncUtf8(command, args) {
const env = process.env;
env.PATH = addPathToNodeJs(env.PATH);
return childProcess.execFileSync(command, args, { encoding: 'utf-8', env });
}
function addPathToNodeJs(pathEnvironment) {
return path.dirname(process.argv0) + path.delimiter + pathEnvironment;
}
function normalizeCrlf(string) {
if (typeof string === 'string') {
return string.replace(/\r\n/g, '\n');
}
return string;
}