Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added tests for cli

  • Loading branch information...
commit 8a959768ccde8c66d110899e0b8f19d750a2e7e5 1 parent 232c65d
@bbyars authored
View
15 server/bin/httpmock
@@ -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;
@@ -75,4 +76,8 @@ switch (commandLine.command) {
default:
error("Invalid command '" + commandLine.command + "'.");
break;
+ }
+}
+catch (err) {
+ error(error.message);
}
View
25 server/lib/cli.js
@@ -1,30 +1,29 @@
'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,
@@ -32,7 +31,7 @@ var parse = function(argv, defaultOptions) {
// 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;
}
@@ -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(),
View
2  server/test/testExtensions.js
@@ -1,5 +1,7 @@
'use strict';
+require('../lib/extensions');
+
var url = require('url'),
http = require('http'),
nodeunitTypes = require('../deps/nodeunit/lib/types'),
View
80 server/test/unit/cli.test.js
@@ -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();
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.