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.

165 lines
5.8KB

  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 cliRelativePartialsTxt = path.resolve(_files, 'cli_with_relative_partials.txt');
  9. var moduleVersion = require('../package').version;
  10. function changeForOS(command) {
  11. if(process.platform === 'win32') {
  12. return command
  13. .replace(/bin\/mustache/g, 'node bin\\mustache')
  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('writes rendered template into the file specified by the third argument', function(done) {
  66. var outputFile = 'test/_files/cli_output.txt';
  67. exec('bin/mustache test/_files/cli.json test/_files/cli.mustache ' + outputFile, function(err, stdout, stderr) {
  68. assert.equal(err, null);
  69. assert.equal(stderr, '');
  70. assert.equal(stdout, '');
  71. assert.equal(fs.readFileSync(outputFile), expectedOutput);
  72. fs.unlinkSync('test/_files/cli_output.txt');
  73. done();
  74. });
  75. });
  76. it('reads view data from stdin when first argument equals "-"', function(done){
  77. exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
  78. assert.equal(err, null);
  79. assert.equal(stderr, '');
  80. assert.equal(stdout, expectedOutput);
  81. done();
  82. });
  83. });
  84. it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
  85. exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) {
  86. assert.isOk(/Could not find file: .+non-existing-template\.mustache/.test(stderr));
  87. done();
  88. });
  89. });
  90. it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
  91. exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
  92. assert.isOk(/Could not find file: .+non-existing-view\.json/.test(stderr));
  93. done();
  94. });
  95. });
  96. });
  97. describe("with partials", function(){
  98. before(function(done) {
  99. fs.readFile(cliPartialsTxt, function onFsEnd(err, data) {
  100. if (err) return done(err);
  101. expectedOutput = data.toString();
  102. done();
  103. });
  104. });
  105. it('writes rendered template with partials into stdout', function(done) {
  106. 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) {
  107. assert.equal(err, null);
  108. assert.equal(stderr, '');
  109. assert.equal(stdout, expectedOutput);
  110. done();
  111. });
  112. });
  113. it('writes rendered template with partials when partials args before required args', function(done) {
  114. 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) {
  115. assert.equal(err, null);
  116. assert.equal(stderr, '');
  117. assert.equal(stdout, expectedOutput);
  118. done();
  119. });
  120. });
  121. });
  122. describe('with relative partials', function () {
  123. before(function(done) {
  124. fs.readFile(cliRelativePartialsTxt, function onFsEnd(err, data) {
  125. if (err) return done(err);
  126. expectedOutput = data.toString();
  127. done();
  128. });
  129. });
  130. it('selects the file described with the relative path', function (done) {
  131. exec(changeForOS('bin/mustache -p test/_files/alt/partial_with_relative_path.mustache -p test/_files/partial_with_relative_path.mustache test/_files/cli_with_relative_partials.json test/_files/cli_with_relative_partials.mustache'), function (err, stdout, stderr) {
  132. assert.equal(err, null);
  133. assert.equal(stderr, '');
  134. assert.equal(stdout, expectedOutput);
  135. done();
  136. });
  137. });
  138. });
  139. });