index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const expect = require('chai').expect;
  2. const exec = require('child_process').exec;
  3. const reorder = require('../lib/reorder');
  4. const flaggedRespawn = require('../');
  5. describe('flaggedRespawn', function () {
  6. var flags = ['--harmony', '--use_strict', '--stack_size']
  7. describe('reorder', function () {
  8. it('should re-order args, placing special flags first', function () {
  9. var needsRespawn = ['node', 'file.js', '--flag', '--harmony', 'command'];
  10. var noRespawnNeeded = ['node', 'bin/flagged-respawn', 'thing'];
  11. expect(reorder(flags, needsRespawn))
  12. .to.deep.equal(['node', '--harmony', 'file.js', '--flag', 'command']);
  13. expect(reorder(flags, noRespawnNeeded))
  14. .to.deep.equal(noRespawnNeeded);
  15. });
  16. it('should keep flags values when not placed first', function () {
  17. var args = ['node', 'file.js', '--stack_size=2048'];
  18. var expected = ['node', '--stack_size=2048', 'file.js'];
  19. expect(reorder(flags, args)).to.deep.equal(expected);
  20. });
  21. it('should ignore special flags when they are in the correct position', function () {
  22. var args = ['node', '--harmony', 'file.js', '--flag'];
  23. expect(reorder(flags, reorder(flags, args))).to.deep.equal(args);
  24. });
  25. });
  26. describe('execute', function () {
  27. it('should throw if no flags are specified', function () {
  28. expect(function () { flaggedRespawn.execute(); }).to.throw;
  29. });
  30. it('should throw if no argv is specified', function () {
  31. expect(function () { flaggedRespawn.execute(flags); }).to.throw;
  32. });
  33. it('should respawn and pipe stderr/stdout to parent', function (done) {
  34. exec('node ./test/bin/respawner.js --harmony', function (err, stdout, stderr) {
  35. expect(stdout.replace(/[0-9]/g, '')).to.equal('Special flags found, respawning.\nRespawned to PID: \nRunning!\n');
  36. done();
  37. });
  38. });
  39. it('should respawn and pass exit code from child to parent', function (done) {
  40. exec('node ./test/bin/exit_code.js --harmony', function (err, stdout, stderr) {
  41. expect(err.code).to.equal(100);
  42. done();
  43. });
  44. });
  45. it.skip('should respawn; if child is killed, parent should exit with same signal', function (done) {
  46. // TODO: figure out why travis hates this
  47. exec('node ./test/bin/signal.js --harmony', function (err, stdout, stderr) {
  48. console.log('err', err);
  49. console.log('stdout', stdout);
  50. console.log('stderr', stderr);
  51. expect(err.signal).to.equal('SIGHUP');
  52. done();
  53. });
  54. });
  55. it('should call back with ready as true when respawn is not needed', function () {
  56. var argv = ['node', './test/bin/respawner'];
  57. flaggedRespawn(flags, argv, function (ready) {
  58. expect(ready).to.be.true;
  59. });
  60. });
  61. it('should call back with ready as false when respawn is needed', function () {
  62. var argv = ['node', './test/bin/respawner', '--harmony'];
  63. flaggedRespawn(flags, argv, function (ready) {
  64. expect(ready).to.be.false;
  65. });
  66. });
  67. it('should call back with the child process when ready', function () {
  68. var argv = ['node', './test/bin/respawner', '--harmony'];
  69. flaggedRespawn(flags, argv, function (ready, child) {
  70. expect(child.pid).to.not.equal(process.pid);
  71. });
  72. });
  73. it('should call back with own process when respawn not needed', function () {
  74. var argv = ['node', './test/bin/respawner'];
  75. flaggedRespawn(flags, argv, function (ready, child) {
  76. expect(child.pid).to.equal(process.pid);
  77. });
  78. });
  79. });
  80. });