Skip to content

Commit 6b1a08d

Browse files
mmaleckiindexzero
authored andcommitted
[test] Add test for option parsing
1 parent a52ee8a commit 6b1a08d

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

test/cli-test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* cli-test.js: Tests for forever CLI
3+
*
4+
* (C) 2011 Nodejitsu Inc.
5+
* MIT LICENCE
6+
*
7+
*/
8+
9+
var fs = require('fs'),
10+
path = require('path'),
11+
assert = require('assert'),
12+
vows = require('vows'),
13+
helpers = require('./helpers'),
14+
forever = require('../lib/forever');
15+
16+
var script = path.join(__dirname, '..', 'examples', 'log-on-interval.js'),
17+
options = ['--uid', 'itShouldNotGoToUIDField'];
18+
19+
vows.describe('forever/cli').addBatch({
20+
'When using forever CLI': {
21+
'and starting script using `forever start`': helpers.spawn(['start', script], {
22+
'`forever.list` result': helpers.list({
23+
'should contain spawned process': function (list) {
24+
helpers.assertList(list);
25+
assert.equal(list[0].command, 'node');
26+
assert.equal(fs.realpathSync(list[0].file), fs.realpathSync(script));
27+
helpers.assertStartsWith(list[0].logFile, forever.config.get('root'));
28+
},
29+
'and stopping it using `forever stop`': helpers.spawn(['stop', script], {
30+
'`forever.list` result': helpers.list({
31+
'should not contain previous process': function (list) {
32+
assert.isNull(list);
33+
}
34+
})
35+
})
36+
})
37+
})
38+
}
39+
}).addBatch({
40+
'When using forever CLI': {
41+
'and starting script using `forever start` with arguments': helpers.spawn(['start', script].concat(options), {
42+
'`forever.list` result': helpers.list({
43+
'should contain spawned process with proper options': function (list) {
44+
helpers.assertList(list);
45+
assert.notEqual(list[0].uid, 'itShouldNotGoToUIDField');
46+
assert.deepEqual(list[0].options, options);
47+
}
48+
})
49+
})
50+
}
51+
}).export(module);
52+

test/helpers.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,51 @@ helpers.assertTimes = function (script, times, options) {
2424
assert.equal(child.times, times);
2525
}
2626
}
27-
};
27+
};
28+
29+
helpers.spawn = function (args, options) {
30+
options.topic = function () {
31+
var self = this;
32+
33+
args = [path.join(__dirname, '..', 'bin', 'forever')].concat(args);
34+
35+
var child = spawn(process.argv[0], args),
36+
stdout = '',
37+
stderr = '';
38+
39+
child.stdout.on('data', function (data) {
40+
stdout += data;
41+
});
42+
child.stderr.on('data', function (data) {
43+
stderr += data;
44+
});
45+
child.once('exit', function (exitCode) {
46+
//
47+
// Remark: We wait 200 ms because of forever boot up time (master
48+
// doesn't wait for slave to start up after it's forked, it just quits)
49+
//
50+
setTimeout(function () {
51+
self.callback(exitCode, stdout, stderr);
52+
}, 200);
53+
});
54+
};
55+
return options;
56+
};
57+
58+
helpers.list = function (options) {
59+
options.topic = function () {
60+
forever.list(false, this.callback)
61+
};
62+
return options;
63+
};
64+
65+
helpers.assertStartsWith = function (string, substring) {
66+
assert.equal(string.slice(0, substring.length), substring);
67+
};
68+
69+
helpers.assertList = function (list) {
70+
assert.isNotNull(list);
71+
assert.lengthOf(list, 1);
72+
};
73+
74+
>>>>>>> d07b6dc... [test] Add test for option parsing

0 commit comments

Comments
 (0)