Skip to content

Commit 80c4516

Browse files
aorinevofabb
authored andcommittedMar 31, 2020
chore: add unit tests for cli options (#1666)
- adds full unit tests for all cli options - demonstrates that nothing changes between minimist and yargs except a minor order change in the --help text - proves b9c4b25 works the same as before Co-authored-by: fabb <fabb@users.noreply.github.com>
1 parent d79212a commit 80c4516

22 files changed

+335
-13
lines changed
 

‎bin/handlebars

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ delete argv._;
111111

112112
const Precompiler = require('../dist/cjs/precompiler');
113113
Precompiler.loadTemplates(argv, function(err, opts) {
114+
114115
if (err) {
115116
throw err;
116117
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{#someHelper true}}
2+
<div>Some known helper</div>
3+
{{#anotherHelper true}}
4+
<div>Another known helper</div>
5+
{{/anotherHelper}}
6+
{{/someHelper}}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>This is a test</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>Test Partial</div>

‎spec/artifacts/source.map.amd.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":3,"sources":["test"],"names":["compiler","main","container","depth0","helpers","partials","data","useData"],"mappings":"kHAAA,CAAAA,SAAA,CAAA,EAAA,YAAAC,KAAA,SAAAC,EAAAC,EAAAC,EAAAC,EAAAC,GAAA,MAAA,gBAAAC,SAAA"}

‎spec/expected/bom.amd.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
return templates['bom'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "a";
5+
},"useData":true});
6+
});

‎spec/expected/compiled.string.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
2+
return "<div>Test String</div>";
3+
},"useData":true}

‎spec/expected/empty.amd.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎spec/expected/empty.amd.namespace.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = CustomNamespace.templates = CustomNamespace.templates || {};
3+
return templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "";
5+
},"useData":true});
6+
});

‎spec/expected/empty.amd.simple.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
2+
return "";
3+
},"useData":true}

‎spec/expected/empty.common.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(function() {
2+
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "";
5+
},"useData":true});
6+
})();

‎spec/expected/empty.name.amd.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
templates['firstTemplate'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "<div>1</div>";
5+
},"useData":true});
6+
templates['secondTemplate'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
7+
return "<div>2</div>";
8+
},"useData":true});
9+
return templates;
10+
});

‎spec/expected/empty.root.amd.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
return templates['artifacts/partial.template'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "<div>Test Partial</div>";
5+
},"useData":true});
6+
});

‎spec/expected/handlebar.path.amd.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['some-path/handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
return templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "";
5+
},"useData":true});
6+
});

‎spec/expected/help.menu.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Precompile handlebar templates.
2+
Usage: handlebars [template|directory]...
3+
4+
Options:
5+
--help Outputs this message [boolean]
6+
-f, --output Output File [string]
7+
--map Source Map File [string]
8+
-a, --amd Exports amd style (require.js) [boolean]
9+
-c, --commonjs Exports CommonJS style, path to Handlebars module [string] [default: null]
10+
-h, --handlebarPath Path to handlebar.js (only valid for amd-style) [string] [default: ""]
11+
-k, --known Known helpers [string]
12+
-o, --knownOnly Known helpers only [boolean]
13+
-m, --min Minimize output [boolean]
14+
-n, --namespace Template namespace [string] [default: "Handlebars.templates"]
15+
-s, --simple Output template function only. [boolean]
16+
-N, --name Name of passed string templates. Optional if running in a simple mode. Required when operating on
17+
multiple templates. [string]
18+
-i, --string Generates a template from the passed CLI argument.
19+
"-" is treated as a special value and causes stdin to be read for the template value. [string]
20+
-r, --root Template root. Base value that will be stripped from template names. [string]
21+
-p, --partial Compiling a partial template [boolean]
22+
-d, --data Include data when compiling [boolean]
23+
-e, --extension Template extension. [string] [default: "handlebars"]
24+
-b, --bom Removes the BOM (Byte Order Mark) from the beginning of the templates. [boolean]
25+
-v, --version Show version number [boolean]

‎spec/expected/namespace.amd.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = someNameSpace = someNameSpace || {};
3+
templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "";
5+
},"useData":true});
6+
templates['empty'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
7+
return "";
8+
},"useData":true});
9+
return templates;
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
return templates['non.default.extension'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "<div>This is a test</div>";
5+
},"useData":true});
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
return templates['known.helpers'] = template({"1":function(container,depth0,helpers,partials,data) {
4+
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
5+
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
6+
return parent[propertyName];
7+
}
8+
return undefined
9+
};
10+
return " <div>Some known helper</div>\n"
11+
+ ((stack1 = lookupProperty(helpers,"anotherHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"anotherHelper","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":3,"column":4},"end":{"line":5,"column":22}}})) != null ? stack1 : "");
12+
},"2":function(container,depth0,helpers,partials,data) {
13+
return " <div>Another known helper</div>\n";
14+
},"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
15+
var stack1, lookupProperty = container.lookupProperty || function(parent, propertyName) {
16+
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
17+
return parent[propertyName];
18+
}
19+
return undefined
20+
};
21+
return ((stack1 = lookupProperty(helpers,"someHelper").call(depth0 != null ? depth0 : (container.nullContext || {}),true,{"name":"someHelper","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":15}}})) != null ? stack1 : "");
22+
},"useData":true});
23+
});
24+

‎spec/expected/partial.template.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
define(['handlebars.runtime'], function(Handlebars) {
2+
Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
return Handlebars.partials['partial.template'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
4+
return "<div>Test Partial</div>";
5+
},"useData":true});
6+
});

‎spec/expected/source.map.amd.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎spec/precompiler.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ describe('precompiler', function() {
307307
Precompiler.loadTemplates(
308308
{ files: [__dirname + '/artifacts'], extension: 'hbs' },
309309
function(err, opts) {
310-
equal(opts.templates.length, 1);
310+
equal(opts.templates.length, 2);
311311
equal(opts.templates[0].name, 'example_2');
312+
312313
done(err);
313314
}
314315
);
@@ -317,7 +318,7 @@ describe('precompiler', function() {
317318
Precompiler.loadTemplates(
318319
{ files: [__dirname + '/artifacts'], extension: 'handlebars' },
319320
function(err, opts) {
320-
equal(opts.templates.length, 3);
321+
equal(opts.templates.length, 5);
321322
equal(opts.templates[0].name, 'bom');
322323
equal(opts.templates[1].name, 'empty');
323324
equal(opts.templates[2].name, 'example_1');

‎tasks/test-bin.js

+202-11
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,213 @@ const chai = require('chai');
77
chai.use(require('chai-diff'));
88
const expect = chai.expect;
99

10-
module.exports = function(grunt) {
11-
grunt.registerTask('test:bin', function() {
12-
const stdout = executeBinHandlebars(
10+
const testCases = [
11+
{
12+
binInputParameters: ['-a', 'spec/artifacts/empty.handlebars'],
13+
outputLocation: 'stdout',
14+
expectedOutputSpec: './spec/expected/empty.amd.js'
15+
},
16+
{
17+
binInputParameters: [
1318
'-a',
19+
'-f',
20+
'TEST_OUTPUT',
1421
'spec/artifacts/empty.handlebars'
15-
);
22+
],
23+
outputLocation: 'TEST_OUTPUT',
24+
expectedOutputSpec: './spec/expected/empty.amd.js'
25+
},
26+
{
27+
binInputParameters: [
28+
'-a',
29+
'-n',
30+
'CustomNamespace.templates',
31+
'spec/artifacts/empty.handlebars'
32+
],
33+
outputLocation: 'stdout',
34+
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
35+
},
36+
{
37+
binInputParameters: [
38+
'-a',
39+
'--namespace',
40+
'CustomNamespace.templates',
41+
'spec/artifacts/empty.handlebars'
42+
],
43+
outputLocation: 'stdout',
44+
expectedOutputSpec: './spec/expected/empty.amd.namespace.js'
45+
},
46+
{
47+
binInputParameters: ['-a', '-s', 'spec/artifacts/empty.handlebars'],
48+
outputLocation: 'stdout',
49+
expectedOutputSpec: './spec/expected/empty.amd.simple.js'
50+
},
51+
{
52+
binInputParameters: ['-a', '-m', 'spec/artifacts/empty.handlebars'],
53+
outputLocation: 'stdout',
54+
expectedOutputSpec: './spec/expected/empty.amd.min.js'
55+
},
56+
{
57+
binInputParameters: [
58+
'spec/artifacts/known.helpers.handlebars',
59+
'-a',
60+
'-k',
61+
'someHelper',
62+
'-k',
63+
'anotherHelper',
64+
'-o'
65+
],
66+
outputLocation: 'stdout',
67+
expectedOutputSpec: './spec/expected/non.empty.amd.known.helper.js'
68+
},
69+
{
70+
binInputParameters: ['--help'],
71+
outputLocation: 'stdout',
72+
expectedOutputSpec: './spec/expected/help.menu.txt'
73+
},
74+
{
75+
binInputParameters: ['-v'],
76+
outputLocation: 'stdout',
77+
expectedOutput: require('../package.json').version
78+
},
79+
{
80+
binInputParameters: [
81+
'-a',
82+
'-e',
83+
'hbs',
84+
'./spec/artifacts/non.default.extension.hbs'
85+
],
86+
outputLocation: 'stdout',
87+
expectedOutputSpec: './spec/expected/non.default.extension.amd.js'
88+
},
89+
{
90+
binInputParameters: [
91+
'-a',
92+
'-p',
93+
'./spec/artifacts/partial.template.handlebars'
94+
],
95+
outputLocation: 'stdout',
96+
expectedOutputSpec: './spec/expected/partial.template.js'
97+
},
98+
{
99+
binInputParameters: ['spec/artifacts/empty.handlebars', '-c'],
100+
outputLocation: 'stdout',
101+
expectedOutputSpec: './spec/expected/empty.common.js'
102+
},
103+
{
104+
binInputParameters: [
105+
'spec/artifacts/empty.handlebars',
106+
'spec/artifacts/empty.handlebars',
107+
'-a',
108+
'-n',
109+
'someNameSpace'
110+
],
111+
outputLocation: 'stdout',
112+
expectedOutputSpec: './spec/expected/namespace.amd.js'
113+
},
114+
{
115+
binInputParameters: [
116+
'spec/artifacts/empty.handlebars',
117+
'-h',
118+
'some-path/',
119+
'-a'
120+
],
121+
outputLocation: 'stdout',
122+
expectedOutputSpec: './spec/expected/handlebar.path.amd.js'
123+
},
124+
{
125+
binInputParameters: [
126+
'spec/artifacts/partial.template.handlebars',
127+
'-r',
128+
'spec',
129+
'-a'
130+
],
131+
outputLocation: 'stdout',
132+
expectedOutputSpec: './spec/expected/empty.root.amd.js'
133+
},
134+
{
135+
binInputParameters: [
136+
'-i',
137+
'<div>1</div>',
138+
'-i',
139+
'<div>2</div>',
140+
'-N',
141+
'firstTemplate',
142+
'-N',
143+
'secondTemplate',
144+
'-a'
145+
],
146+
outputLocation: 'stdout',
147+
expectedOutputSpec: './spec/expected/empty.name.amd.js'
148+
},
149+
{
150+
binInputParameters: [
151+
'-i',
152+
'<div>1</div>',
153+
'-a',
154+
'-m',
155+
'-N',
156+
'test',
157+
'--map',
158+
'./spec/artifacts/source.map.amd.txt'
159+
],
160+
outputLocation: 'stdout',
161+
expectedOutputSpec: './spec/expected/source.map.amd.js'
162+
},
163+
{
164+
binInputParameters: [
165+
'-i',
166+
'<div>1</div>',
167+
'-a',
168+
'-m',
169+
'-N',
170+
'test',
171+
'--map',
172+
'./spec/artifacts/source.map.amd.txt'
173+
],
174+
outputLocation: 'stdout',
175+
expectedOutputSpec: './spec/expected/source.map.amd.js'
176+
},
177+
{
178+
binInputParameters: ['./spec/artifacts/bom.handlebars', '-b', '-a'],
179+
outputLocation: 'stdout',
180+
expectedOutputSpec: './spec/expected/bom.amd.js'
181+
}
182+
];
16183

17-
const expectedOutput = fs.readFileSync(
18-
'./spec/expected/empty.amd.js',
19-
'utf-8'
20-
);
184+
module.exports = function(grunt) {
185+
grunt.registerTask('test:bin', function() {
186+
testCases.forEach(
187+
({
188+
binInputParameters,
189+
outputLocation,
190+
expectedOutputSpec,
191+
expectedOutput
192+
}) => {
193+
const stdout = executeBinHandlebars(...binInputParameters);
194+
195+
if (!expectedOutput && expectedOutputSpec) {
196+
expectedOutput = fs.readFileSync(expectedOutputSpec, 'utf-8');
197+
}
21198

22-
const normalizedOutput = normalizeCrlf(stdout);
23-
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
199+
const useStdout = outputLocation === 'stdout';
200+
const normalizedOutput = normalizeCrlf(
201+
useStdout ? stdout : fs.readFileSync(outputLocation, 'utf-8')
202+
);
203+
const normalizedExpectedOutput = normalizeCrlf(expectedOutput);
24204

25-
expect(normalizedOutput).not.to.be.differentFrom(normalizedExpectedOutput);
205+
if (!useStdout) {
206+
fs.unlinkSync(outputLocation);
207+
}
208+
209+
expect(normalizedOutput).not.to.be.differentFrom(
210+
normalizedExpectedOutput,
211+
{
212+
relaxedSpace: true
213+
}
214+
);
215+
}
216+
);
26217
});
27218
};
28219

0 commit comments

Comments
 (0)
Please sign in to comment.