_.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var spawn = require('child_process').spawn,
  2. should = require('chai').should();
  3. describe('bin script', function () {
  4. it('should run as a shell script with no arguments', function (done) {
  5. testCmd('./bin.js', [], done);
  6. });
  7. it('should run as a shell script with arguments', function (done) {
  8. testCmd('./bin.js', [ 'a', 'b', 'c' ], done);
  9. });
  10. it('should run as a node script with no arguments', function (done) {
  11. testCmd('node bin.js', [], done);
  12. });
  13. it('should run as a node script with arguments', function (done) {
  14. testCmd('node bin.js', [ 'x', 'y', 'z' ], done);
  15. });
  16. describe('path returned by "which"', function () {
  17. beforeEach(function () {
  18. this.which = spawn('which', ['node']);
  19. });
  20. it('should match the actual path to the script file', function (done) {
  21. this.which.stdout.on('data', function (buf) {
  22. testCmd(buf.toString().trim() + ' bin.js', [], done);
  23. });
  24. this.which.stderr.on('data', done);
  25. });
  26. it('should match the actual path to the script file, with arguments', function (done) {
  27. this.which.stdout.on('data', function (buf) {
  28. testCmd(buf.toString().trim() + ' bin.js', [ 'q', 'r' ], done);
  29. });
  30. this.which.stderr.on('data', done);
  31. });
  32. });
  33. });
  34. function testCmd(cmd, args, done) {
  35. var oldDir = process.cwd();
  36. process.chdir(__dirname + '/_');
  37. var cmds = cmd.split(' ');
  38. var bin = spawn(cmds[0], cmds.slice(1).concat(args.map(String)));
  39. process.chdir(oldDir);
  40. bin.stderr.on('data', done);
  41. bin.stdout.on('data', function (buf) {
  42. var _ = JSON.parse(buf.toString());
  43. _.map(String).should.deep.equal(args.map(String));
  44. done();
  45. });
  46. }