cli-test.js 5.7 KB

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