Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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('writes rendered template into the file specified by the third argument', function(done) {
  65. var outputFile = 'test/_files/cli_output.txt';
  66. exec('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile, function(err, stdout, stderr) {
  67. assert.equal(err, null);
  68. assert.equal(stderr, '');
  69. assert.equal(stdout, '');
  70. assert.equal(fs.readFileSync(outputFile), expectedOutput);
  71. fs.unlink('test/_files/cli_output.txt');
  72. done();
  73. });
  74. });
  75. it('reads view data from stdin when first argument equals "-"', function(done){
  76. exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
  77. assert.equal(err, null);
  78. assert.equal(stderr, '');
  79. assert.equal(stdout, expectedOutput);
  80. done();
  81. });
  82. });
  83. it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
  84. exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) {
  85. assert.isOk(/Could not find file: .+non-existing-template\.mustache/.test(stderr));
  86. done();
  87. });
  88. });
  89. it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
  90. exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
  91. assert.isOk(/Could not find file: .+non-existing-view\.json/.test(stderr));
  92. done();
  93. });
  94. });
  95. });
  96. describe("with partials", function(){
  97. before(function(done) {
  98. fs.readFile(cliPartialsTxt, function onFsEnd(err, data) {
  99. if (err) return done(err);
  100. expectedOutput = data.toString();
  101. done();
  102. });
  103. });
  104. it('writes rendered template with partials into stdout', function(done) {
  105. 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) {
  106. assert.equal(err, null);
  107. assert.equal(stderr, '');
  108. assert.equal(stdout, expectedOutput);
  109. done();
  110. });
  111. });
  112. it('writes rendered template with partials when partials args before required args', function(done) {
  113. 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) {
  114. assert.equal(err, null);
  115. assert.equal(stderr, '');
  116. assert.equal(stdout, expectedOutput);
  117. done();
  118. });
  119. });
  120. })
  121. });