| @@ -7,48 +7,53 @@ var cliTxt = path.resolve(_files, 'cli.txt'); | |||||
| var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt'); | var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt'); | ||||
| var moduleVersion = require('../package').version; | var moduleVersion = require('../package').version; | ||||
| var exec = require('child_process').exec; | |||||
| var child_process = require('child_process'); | |||||
| function changeForOS(command) { | function changeForOS(command) { | ||||
| if(process.platform === 'win32') { | if(process.platform === 'win32') { | ||||
| return command. | return command. | ||||
| replace(/bin\/mustache/g, 'node bin\\mustache'). | replace(/bin\/mustache/g, 'node bin\\mustache'). | ||||
| replace(/cat /g, 'type '). | |||||
| replace(/\bcat\b/g, 'type'). | |||||
| replace(/\//g, '\\'); | replace(/\//g, '\\'); | ||||
| } | } | ||||
| return command; | return command; | ||||
| } | } | ||||
| function exec() { | |||||
| arguments[0] = changeForOS(arguments[0]); | |||||
| return child_process.exec.apply(child_process, arguments); | |||||
| } | |||||
| describe('Mustache CLI', function () { | describe('Mustache CLI', function () { | ||||
| var expectedOutput; | var expectedOutput; | ||||
| it('writes syntax hints into stderr when runned with wrong number of arguments', function(done) { | it('writes syntax hints into stderr when runned with wrong number of arguments', function(done) { | ||||
| exec(changeForOS('bin/mustache'), function(err, stdout, stderr) { | |||||
| exec('bin/mustache', function(err, stdout, stderr) { | |||||
| assert.notEqual(stderr.indexOf('Syntax'), -1); | assert.notEqual(stderr.indexOf('Syntax'), -1); | ||||
| done(); | done(); | ||||
| }); | }); | ||||
| }); | }); | ||||
| it('writes hints about JSON parsing errors when given invalid JSON', function(done) { | it('writes hints about JSON parsing errors when given invalid JSON', function(done) { | ||||
| exec(changeForOS('echo {name:"lebron"} | bin/mustache - test/_files/cli.mustache'), function(err, stdout, stderr) { | |||||
| 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); | assert.notEqual(stderr.indexOf('Shooot, could not parse view as JSON'), -1); | ||||
| done(); | done(); | ||||
| }); | }); | ||||
| }); | }); | ||||
| it('writes module version into stdout when runned with --version', function(done){ | it('writes module version into stdout when runned with --version', function(done){ | ||||
| exec(changeForOS('bin/mustache --version'), function(err, stdout, stderr) { | |||||
| exec('bin/mustache --version', function(err, stdout, stderr) { | |||||
| assert.notEqual(stdout.indexOf(moduleVersion), -1); | assert.notEqual(stdout.indexOf(moduleVersion), -1); | ||||
| done(); | done(); | ||||
| }); | }); | ||||
| }); | }); | ||||
| it('writes module version into stdout when runned with -v', function(done){ | it('writes module version into stdout when runned with -v', function(done){ | ||||
| exec(changeForOS('bin/mustache -v'), function(err, stdout, stderr) { | |||||
| exec('bin/mustache -v', function(err, stdout, stderr) { | |||||
| assert.notEqual(stdout.indexOf(moduleVersion), -1); | assert.notEqual(stdout.indexOf(moduleVersion), -1); | ||||
| done(); | done(); | ||||
| }); | }); | ||||
| @@ -65,7 +70,7 @@ describe('Mustache CLI', function () { | |||||
| }); | }); | ||||
| it('writes rendered template into stdout when successfull', function(done) { | it('writes rendered template into stdout when successfull', function(done) { | ||||
| exec(changeForOS('bin/mustache test/_files/cli.json test/_files/cli.mustache'), function(err, stdout, stderr) { | |||||
| exec('bin/mustache test/_files/cli.json test/_files/cli.mustache', function(err, stdout, stderr) { | |||||
| assert.equal(err, null); | assert.equal(err, null); | ||||
| assert.equal(stderr, ''); | assert.equal(stderr, ''); | ||||
| assert.equal(stdout, expectedOutput); | assert.equal(stdout, expectedOutput); | ||||
| @@ -75,7 +80,7 @@ describe('Mustache CLI', function () { | |||||
| it('writes rendered template into the file specified by the third argument', function(done) { | it('writes rendered template into the file specified by the third argument', function(done) { | ||||
| var outputFile = 'test/_files/cli_output.txt'; | var outputFile = 'test/_files/cli_output.txt'; | ||||
| exec(changeForOS('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile), function(err, stdout, stderr) { | |||||
| exec('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile, function(err, stdout, stderr) { | |||||
| assert.equal(err, null); | assert.equal(err, null); | ||||
| assert.equal(stderr, ''); | assert.equal(stderr, ''); | ||||
| assert.equal(stdout, ''); | assert.equal(stdout, ''); | ||||
| @@ -86,7 +91,7 @@ describe('Mustache CLI', function () { | |||||
| }); | }); | ||||
| it('reads view data from stdin when first argument equals "-"', function(done){ | it('reads view data from stdin when first argument equals "-"', function(done){ | ||||
| exec(changeForOS('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache'), function(err, stdout, stderr) { | |||||
| exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) { | |||||
| assert.equal(err, null); | assert.equal(err, null); | ||||
| assert.equal(stderr, ''); | assert.equal(stderr, ''); | ||||
| assert.equal(stdout, expectedOutput); | assert.equal(stdout, expectedOutput); | ||||
| @@ -95,7 +100,7 @@ describe('Mustache CLI', function () { | |||||
| }); | }); | ||||
| it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) { | it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) { | ||||
| exec(changeForOS('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache'), function(err, stdout, stderr) { | |||||
| exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) { | |||||
| console.log(stderr); | console.log(stderr); | ||||
| assert.notEqual(stderr.indexOf('Could not find file:'), -1); | assert.notEqual(stderr.indexOf('Could not find file:'), -1); | ||||
| assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-template.mustache')), -1); | assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-template.mustache')), -1); | ||||
| @@ -104,7 +109,7 @@ describe('Mustache CLI', function () { | |||||
| }); | }); | ||||
| it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) { | it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) { | ||||
| exec(changeForOS('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache'), function(err, stdout, stderr) { | |||||
| exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) { | |||||
| assert.notEqual(stderr.indexOf('Could not find file:'), -1); | assert.notEqual(stderr.indexOf('Could not find file:'), -1); | ||||
| assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-view.json')), -1); | assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-view.json')), -1); | ||||
| done(); | done(); | ||||
| @@ -124,7 +129,7 @@ describe('Mustache CLI', function () { | |||||
| }); | }); | ||||
| it('writes rendered template with partials into stdout', function(done) { | it('writes rendered template with partials into stdout', function(done) { | ||||
| exec(changeForOS('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) { | |||||
| 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(err, null); | ||||
| assert.equal(stderr, ''); | assert.equal(stderr, ''); | ||||
| assert.equal(stdout, expectedOutput); | assert.equal(stdout, expectedOutput); | ||||
| @@ -133,7 +138,7 @@ describe('Mustache CLI', function () { | |||||
| }); | }); | ||||
| it('writes rendered template with partials when partials args before required args', function(done) { | it('writes rendered template with partials when partials args before required args', function(done) { | ||||
| exec(changeForOS('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) { | |||||
| 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(err, null); | ||||
| assert.equal(stderr, ''); | assert.equal(stderr, ''); | ||||
| assert.equal(stdout, expectedOutput); | assert.equal(stdout, expectedOutput); | ||||