Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

117 строки
4.1KB

  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('reads view data from stdin when first argument equals "-"', function(done){
  52. exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
  53. assert.equal(err, null);
  54. assert.equal(stderr, '');
  55. assert.equal(stdout, expectedOutput);
  56. done();
  57. });
  58. });
  59. it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
  60. exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) {
  61. assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-template.mustache'), -1);
  62. done();
  63. });
  64. });
  65. it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
  66. exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
  67. assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-view.json'), -1);
  68. done();
  69. });
  70. });
  71. });
  72. describe("with partials", function(){
  73. before(function(done) {
  74. fs.readFile(cliPartialsTxt, function onFsEnd(err, data) {
  75. if (err) return done(err);
  76. expectedOutput = data.toString();
  77. done();
  78. });
  79. });
  80. it('writes rendered template with partials into stdout', function(done) {
  81. 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) {
  82. assert.equal(err, null);
  83. assert.equal(stderr, '');
  84. assert.equal(stdout, expectedOutput);
  85. done();
  86. });
  87. });
  88. it('writes rendered template with partials when partials args before required args', function(done) {
  89. 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) {
  90. assert.equal(err, null);
  91. assert.equal(stderr, '');
  92. assert.equal(stdout, expectedOutput);
  93. done();
  94. });
  95. });
  96. })
  97. });