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.

cli-test.js 4.6KB

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