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