25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

85 satır
2.8KB

  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 moduleVersion = require('../package').version;
  7. var exec = require('child_process').exec;
  8. describe('Mustache CLI', function () {
  9. var expectedOutput;
  10. before(function(done) {
  11. fs.readFile(cliTxt, function onFsEnd(err, data) {
  12. if (err) return done(err);
  13. expectedOutput = data.toString();
  14. done();
  15. });
  16. });
  17. it('writes syntax hints into stderr when runned with wrong number of arguments', function(done) {
  18. exec('bin/mustache', function(err, stdout, stderr) {
  19. assert.notEqual(stderr.indexOf('Syntax'), -1);
  20. done();
  21. });
  22. });
  23. it('writes hints about JSON parsing errors when given invalid JSON', function(done) {
  24. exec('echo {name:"lebron"} | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
  25. assert.notEqual(stderr.indexOf('Shooot, could not parse view as JSON'), -1);
  26. done();
  27. });
  28. });
  29. it('writes module version into stdout when runned with --version', function(done){
  30. exec('bin/mustache --version', function(err, stdout, stderr) {
  31. assert.notEqual(stdout.indexOf(moduleVersion), -1);
  32. done();
  33. });
  34. });
  35. it('writes module version into stdout when runned with -v', function(done){
  36. exec('bin/mustache -v', function(err, stdout, stderr) {
  37. assert.notEqual(stdout.indexOf(moduleVersion), -1);
  38. done();
  39. });
  40. });
  41. it('writes rendered template into stdout when successfull', function(done) {
  42. exec('bin/mustache test/_files/cli.json test/_files/cli.mustache', function(err, stdout, stderr) {
  43. assert.equal(err, null);
  44. assert.equal(stderr, '');
  45. assert.equal(stdout, expectedOutput);
  46. done();
  47. });
  48. });
  49. it('reads view data from stdin when first argument equals "-"', function(done){
  50. exec('cat test/_files/cli.json | bin/mustache - test/_files/cli.mustache', function(err, stdout, stderr) {
  51. assert.equal(err, null);
  52. assert.equal(stderr, '');
  53. assert.equal(stdout, expectedOutput);
  54. done();
  55. });
  56. });
  57. it('writes it couldnt find template into stderr when second argument doesnt resolve to a file', function(done) {
  58. exec('bin/mustache test/_files/cli.json test/_files/non-existing-template.mustache', function(err, stdout, stderr) {
  59. assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-template.mustache'), -1);
  60. done();
  61. });
  62. });
  63. it('writes it couldnt find view into stderr when first argument doesnt resolve to a file', function(done) {
  64. exec('bin/mustache test/_files/non-existing-view.json test/_files/cli.mustache', function(err, stdout, stderr) {
  65. assert.notEqual(stderr.indexOf('Could not find file: test/_files/non-existing-view.json'), -1);
  66. done();
  67. });
  68. });
  69. });