Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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