forked from janl/mustache.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli-test.js
144 lines (121 loc) · 4.93 KB
/
cli-test.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
require('./helper');
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var _files = path.join(__dirname, '_files');
var cliTxt = path.resolve(_files, 'cli.txt');
var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt');
var moduleVersion = require('../package').version;
function changeForOS(command) {
if(process.platform === 'win32') {
return command
.replace(/bin\/mustache/g, 'node bin\\mustache')
.replace(/\bcat\b/g, 'type')
.replace(/\//g, '\\');
}
return command;
}
function exec() {
arguments[0] = changeForOS(arguments[0]);
return child_process.exec.apply(child_process, arguments);
}
describe('Mustache CLI', function () {
var expectedOutput;
it('writes syntax hints into stderr when runned with wrong number of arguments', function(done) {
exec('bin/mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Syntax'), -1);
done();
});
});
it('writes hints about JSON parsing errors when given invalid JSON', function(done) {
exec('echo {name:"lebron"} | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
assert.notEqual(stderr.indexOf('Shooot, could not parse view as JSON'), -1);
done();
});
});
it('writes module version into stdout when runned with --version', function(done){
exec('bin/mustache --version', function(err, stdout, stderr) {
assert.notEqual(stdout.indexOf(moduleVersion), -1);
done();
});
});
it('writes module version into stdout when runned with -v', function(done){
exec('bin/mustache -v', function(err, stdout, stderr) {
assert.notEqual(stdout.indexOf(moduleVersion), -1);
done();
});
});
describe("without partials", function(){
before(function(done) {
fs.readFile(cliTxt, function onFsEnd(err, data) {
if (err) return done(err);
expectedOutput = data.toString();
done();
});
});
it('writes rendered template into stdout when successfull', function(done) {
exec('bin/mustache test/_files/cli.json test/_files/cli.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});
it('writes rendered template into the file specified by the third argument', function(done) {
var outputFile = 'test/_files/cli_output.txt';
exec('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile, function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, '');
assert.equal(fs.readFileSync(outputFile), expectedOutput);
fs.unlink('test/_files/cli_output.txt');
done();
});
});
it('reads view data from stdin when first argument equals "-"', function(done){
exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});
it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) {
assert.isOk(/Could not find file: .+non-existing-template\.mustache/.test(stderr));
done();
});
});
it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
assert.isOk(/Could not find file: .+non-existing-view\.json/.test(stderr));
done();
});
});
});
describe("with partials", function(){
before(function(done) {
fs.readFile(cliPartialsTxt, function onFsEnd(err, data) {
if (err) return done(err);
expectedOutput = data.toString();
done();
});
});
it('writes rendered template with partials into stdout', function(done) {
exec('bin/mustache test/_files/cli_with_partials.json test/_files/cli_with_partials.mustache -p test/_files/cli.mustache -p test/_files/comments.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});
it('writes rendered template with partials when partials args before required args', function(done) {
exec('bin/mustache -p test/_files/cli.mustache -p test/_files/comments.mustache test/_files/cli_with_partials.json test/_files/cli_with_partials.mustache', function(err, stdout, stderr) {
assert.equal(err, null);
assert.equal(stderr, '');
assert.equal(stdout, expectedOutput);
done();
});
});
})
});