You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.1KB

  1. require('./helper');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var _files = path.join(__dirname, '_files');
  5. var cliTxt = path.resolve(_files, 'cli.txt');
  6. var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt');
  7. var moduleVersion = require('../package').version;
  8. var exec = require('child_process').exec;
  9. function changeForOS(command) {
  10. if(process.platform === 'win32') {
  11. return command.
  12. replace(/bin\/mustache/g, 'node bin\\mustache').
  13. replace(/cat /g, 'type ').
  14. replace(/\//g, '\\');
  15. }
  16. return command;
  17. }
  18. describe('Mustache CLI', function () {
  19. var expectedOutput;
  20. it('writes syntax hints into stderr when runned with wrong number of arguments', function(done) {
  21. exec(changeForOS('bin/mustache'), function(err, stdout, stderr) {
  22. assert.notEqual(stderr.indexOf('Syntax'), -1);
  23. done();
  24. });
  25. });
  26. it('writes hints about JSON parsing errors when given invalid JSON', function(done) {
  27. exec(changeForOS('echo {name:"lebron"} | bin/mustache - test/_files/cli.mustache'), function(err, stdout, stderr) {
  28. assert.notEqual(stderr.indexOf('Shooot, could not parse view as JSON'), -1);
  29. done();
  30. });
  31. });
  32. it('writes module version into stdout when runned with --version', function(done){
  33. exec(changeForOS('bin/mustache --version'), function(err, stdout, stderr) {
  34. assert.notEqual(stdout.indexOf(moduleVersion), -1);
  35. done();
  36. });
  37. });
  38. it('writes module version into stdout when runned with -v', function(done){
  39. exec(changeForOS('bin/mustache -v'), function(err, stdout, stderr) {
  40. assert.notEqual(stdout.indexOf(moduleVersion), -1);
  41. done();
  42. });
  43. });
  44. describe("without partials", function(){
  45. before(function(done) {
  46. fs.readFile(cliTxt, function onFsEnd(err, data) {
  47. if (err) return done(err);
  48. expectedOutput = data.toString();
  49. done();
  50. });
  51. });
  52. it('writes rendered template into stdout when successfull', function(done) {
  53. exec(changeForOS('bin/mustache test/_files/cli.json test/_files/cli.mustache'), function(err, stdout, stderr) {
  54. assert.equal(err, null);
  55. assert.equal(stderr, '');
  56. assert.equal(stdout, expectedOutput);
  57. done();
  58. });
  59. });
  60. it('writes rendered template into the file specified by the third argument', function(done) {
  61. var outputFile = 'test/_files/cli_output.txt';
  62. exec(changeForOS('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile), function(err, stdout, stderr) {
  63. assert.equal(err, null);
  64. assert.equal(stderr, '');
  65. assert.equal(stdout, '');
  66. assert.equal(fs.readFileSync(outputFile), expectedOutput);
  67. fs.unlink('test/_files/cli_output.txt');
  68. done();
  69. });
  70. });
  71. it('reads view data from stdin when first argument equals "-"', function(done){
  72. exec(changeForOS('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache'), function(err, stdout, stderr) {
  73. assert.equal(err, null);
  74. assert.equal(stderr, '');
  75. assert.equal(stdout, expectedOutput);
  76. done();
  77. });
  78. });
  79. it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
  80. exec(changeForOS('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache'), function(err, stdout, stderr) {
  81. console.log(stderr);
  82. assert.notEqual(stderr.indexOf('Could not find file:'), -1);
  83. assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-template.mustache')), -1);
  84. done();
  85. });
  86. });
  87. it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
  88. exec(changeForOS('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache'), function(err, stdout, stderr) {
  89. assert.notEqual(stderr.indexOf('Could not find file:'), -1);
  90. assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-view.json')), -1);
  91. done();
  92. });
  93. });
  94. });
  95. describe("with partials", function(){
  96. before(function(done) {
  97. fs.readFile(cliPartialsTxt, function onFsEnd(err, data) {
  98. if (err) return done(err);
  99. expectedOutput = data.toString();
  100. done();
  101. });
  102. });
  103. it('writes rendered template with partials into stdout', function(done) {
  104. 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) {
  105. assert.equal(err, null);
  106. assert.equal(stderr, '');
  107. assert.equal(stdout, expectedOutput);
  108. done();
  109. });
  110. });
  111. it('writes rendered template with partials when partials args before required args', function(done) {
  112. 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) {
  113. assert.equal(err, null);
  114. assert.equal(stderr, '');
  115. assert.equal(stdout, expectedOutput);
  116. done();
  117. });
  118. });
  119. })
  120. });