Skip to content

Commit

Permalink
[test] Add test for option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalecki authored and indexzero committed Nov 23, 2011
1 parent a52ee8a commit 6b1a08d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
52 changes: 52 additions & 0 deletions test/cli-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* cli-test.js: Tests for forever CLI
*
* (C) 2011 Nodejitsu Inc.
* MIT LICENCE
*
*/

var fs = require('fs'),
path = require('path'),
assert = require('assert'),
vows = require('vows'),
helpers = require('./helpers'),
forever = require('../lib/forever');

var script = path.join(__dirname, '..', 'examples', 'log-on-interval.js'),
options = ['--uid', 'itShouldNotGoToUIDField'];

vows.describe('forever/cli').addBatch({
'When using forever CLI': {
'and starting script using `forever start`': helpers.spawn(['start', script], {
'`forever.list` result': helpers.list({
'should contain spawned process': function (list) {
helpers.assertList(list);
assert.equal(list[0].command, 'node');
assert.equal(fs.realpathSync(list[0].file), fs.realpathSync(script));
helpers.assertStartsWith(list[0].logFile, forever.config.get('root'));
},
'and stopping it using `forever stop`': helpers.spawn(['stop', script], {
'`forever.list` result': helpers.list({
'should not contain previous process': function (list) {
assert.isNull(list);
}
})
})
})
})
}
}).addBatch({
'When using forever CLI': {
'and starting script using `forever start` with arguments': helpers.spawn(['start', script].concat(options), {
'`forever.list` result': helpers.list({
'should contain spawned process with proper options': function (list) {
helpers.assertList(list);
assert.notEqual(list[0].uid, 'itShouldNotGoToUIDField');
assert.deepEqual(list[0].options, options);
}
})
})
}
}).export(module);

49 changes: 48 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,51 @@ helpers.assertTimes = function (script, times, options) {
assert.equal(child.times, times);
}
}
};
};

helpers.spawn = function (args, options) {
options.topic = function () {
var self = this;

args = [path.join(__dirname, '..', 'bin', 'forever')].concat(args);

var child = spawn(process.argv[0], args),
stdout = '',
stderr = '';

child.stdout.on('data', function (data) {
stdout += data;
});
child.stderr.on('data', function (data) {
stderr += data;
});
child.once('exit', function (exitCode) {
//
// Remark: We wait 200 ms because of forever boot up time (master
// doesn't wait for slave to start up after it's forked, it just quits)
//
setTimeout(function () {
self.callback(exitCode, stdout, stderr);
}, 200);
});
};
return options;
};

helpers.list = function (options) {
options.topic = function () {
forever.list(false, this.callback)
};
return options;
};

helpers.assertStartsWith = function (string, substring) {
assert.equal(string.slice(0, substring.length), substring);
};

helpers.assertList = function (list) {
assert.isNotNull(list);
assert.lengthOf(list, 1);
};

>>>>>>> d07b6dc... [test] Add test for option parsing

0 comments on commit 6b1a08d

Please sign in to comment.