Skip to content

Commit

Permalink
Added tests for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
bbyars committed May 28, 2011
1 parent 232c65d commit 8a95976
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 18 deletions.
15 changes: 10 additions & 5 deletions server/bin/httpmock
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ require.paths.push(basedir);
require.paths.push(basedir + '/lib');
require.paths.push(basedir + '/deps');

var cli = require('cli'),
defaultOptions = { port: 3000, pidfile: 'httpmock.pid' },
commandLine = cli.parse(process.argv.slice(2), defaultOptions),
server = serverAt(commandLine.options);
try {
var cli = require('cli'),
defaultOptions = { port: 3000, pidfile: 'httpmock.pid' },
commandLine = cli.parse(process.argv.slice(2), defaultOptions),
server = serverAt(commandLine.options);

switch (commandLine.command) {
switch (commandLine.command) {
case 'start':
server.start();
break;
Expand All @@ -75,4 +76,8 @@ switch (commandLine.command) {
default:
error("Invalid command '" + commandLine.command + "'.");
break;
}
}
catch (err) {
error(error.message);
}
25 changes: 12 additions & 13 deletions server/lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
'use strict';


var parse = function(argv, defaultOptions) {
var parseOption = function (key, value) {
var parse = function (argv, defaultOptions) {
function parseOption(key, value) {
var OPTION_PREFIX = /^--/,
optionName;

if (key.match(OPTION_PREFIX) === null) {
error("Invalid option '" + key + "'.");
throw new Error("Invalid option '" + key + "'.");
}
optionName = key.replace(OPTION_PREFIX, '');

if (!defaultOptions.hasOwnProperty(optionName)) {
error("Option '" + optionName + "' not recognized.");
throw new Error("Option '" + optionName + "' not recognized.");
}
if (value === undefined) {
error("No argument provided for option '" + optionName + "'.");
throw new Error("No argument provided for option '" + optionName + "'.");
}

return {
key: optionName,
value: value
};
};
}

var parseOptions = function () {
function parseOptions() {
var options = {},
option,
key,
i;

// Add custom options
for (i = 1; i < argv.length; i += 2) {
option = parseOption(argv[i], argv[i+1]);
option = parseOption(argv[i], argv[i + 1]);
options[option.key] = option.value;
}

Expand All @@ -44,16 +43,16 @@ var parse = function(argv, defaultOptions) {
}

return options;
};
}

var parseCommand = function () {
function parseCommand() {
var command = argv[0];

if (command === undefined) {
error("Missing command.");
throw new Error("Missing command.");
}
return command;
};
}

return {
command: parseCommand(),
Expand Down
2 changes: 2 additions & 0 deletions server/test/testExtensions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

require('../lib/extensions');

var url = require('url'),
http = require('http'),
nodeunitTypes = require('../deps/nodeunit/lib/types'),
Expand Down
80 changes: 80 additions & 0 deletions server/test/unit/cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

require('../testExtensions');

var testCase = require('nodeunit').testCase,
cli = require('cli'),
// test.throws behaves differently for reasons that I don't understand
assert = require('assert');

exports.cli = testCase({
'should parse command': function (test) {
var result = cli.parse(['command']);

test.strictEqual(result.command, 'command');
test.done();
},

'should throw if missing command': function (test) {
var parseWithEmptyCommand = function () {
cli.parse([]);
};

assert.throws(parseWithEmptyCommand, /Missing command/);
test.done();
},

'should return defaultOptions if no options provided': function (test) {
var defaultOptions = { key: 'value' },
result = cli.parse(['command'], defaultOptions);

test.jsonEquals(result.options, defaultOptions);
test.done();
},

'should throw if invalid option prefix': function (test) {
var defaultOptions = { key: 'value' },
parseWithInvalidOptionPrefix = function () {
cli.parse(['command', 'key', '1'], defaultOptions);
};

assert.throws(parseWithInvalidOptionPrefix, /Invalid option 'key'/);
test.done();
},

'should not accept options outside of defaultOptions': function (test) {
var defaultOptions = { key: 'value' },
parseWithInvalidOption = function () {
cli.parse(['command', '--invalid', 'option'], defaultOptions);
};

assert.throws(parseWithInvalidOption, /Option 'invalid' not recognized/);
test.done();
},

'should throw if no argument provided for option': function (test) {
var defaultOptions = { key: 'value' },
parseWithNoArgument = function () {
cli.parse(['command', '--key'], defaultOptions);
};

assert.throws(parseWithNoArgument, /No argument provided for option 'key'/);
test.done();
},

'should overwrite option if provided': function (test) {
var defaultOptions = { key: 'value' },
result = cli.parse(['command', '--key', 'changed'], defaultOptions);

test.jsonEquals(result.options, { key: 'changed' });
test.done();
},

'should maintain unchanged options': function (test) {
var defaultOptions = { first: 'one', second: 'two' },
result = cli.parse(['command', '--first', 'changed'], defaultOptions);

test.jsonEquals(result.options, { first: 'changed', second: 'two' });
test.done();
}
});

0 comments on commit 8a95976

Please sign in to comment.