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

150 строки
5.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 child_process = require('child_process');
  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. console.log(stderr);
  86. assert.notEqual(stderr.indexOf('Could not find file:'), -1);
  87. assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-template.mustache')), -1);
  88. done();
  89. });
  90. });
  91. it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
  92. exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
  93. assert.notEqual(stderr.indexOf('Could not find file:'), -1);
  94. assert.notEqual(stderr.indexOf(changeForOS('test/_files/non-existing-view.json')), -1);
  95. done();
  96. });
  97. });
  98. });
  99. describe("with partials", function(){
  100. before(function(done) {
  101. fs.readFile(cliPartialsTxt, function onFsEnd(err, data) {
  102. if (err) return done(err);
  103. expectedOutput = data.toString();
  104. done();
  105. });
  106. });
  107. it('writes rendered template with partials into stdout', function(done) {
  108. 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) {
  109. assert.equal(err, null);
  110. assert.equal(stderr, '');
  111. assert.equal(stdout, expectedOutput);
  112. done();
  113. });
  114. });
  115. it('writes rendered template with partials when partials args before required args', function(done) {
  116. 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) {
  117. assert.equal(err, null);
  118. assert.equal(stderr, '');
  119. assert.equal(stdout, expectedOutput);
  120. done();
  121. });
  122. });
  123. })
  124. });