Skip to content

Commit

Permalink
fixing issue with empty strings not being a valid option value
Browse files Browse the repository at this point in the history
  • Loading branch information
mac- committed Jan 8, 2014
1 parent c7286d7 commit 625ab73
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Makefile
Expand Up @@ -11,8 +11,11 @@ test:
./node_modules/.bin/jshint lib/* --config test/jshint/config.json
@NODE_ENV=test ./node_modules/.bin/mocha --recursive --reporter spec --timeout 3000 test

test-cov:
test-lcov:
@NODE_ENV=test ./node_modules/.bin/mocha --require blanket --recursive --timeout 3000 -R mocha-lcov-reporter test

test-cov:
@NODE_ENV=test ./node_modules/.bin/mocha --require blanket --recursive --timeout 3000 -R travis-cov test

test-cov-html:
@NODE_ENV=test ./node_modules/.bin/mocha --require blanket --recursive --timeout 3000 -R html-cov test > test/coverage.html
Expand Down
11 changes: 10 additions & 1 deletion lib/opter.js
Expand Up @@ -160,7 +160,16 @@ module.exports = function (options, appVersion) {
for (optName in options) {
if (options.hasOwnProperty(optName)) {
option = options[optName];
var value = commander[optName] || process.env[optName.replace(/\./g, '_')] || path.get(configFile, optName) || option.defaultValue;
var value = commander[optName];
if (value === undefined) {
value = process.env[optName.replace(/\./g, '_')];
if (value === undefined) {
value = path.get(configFile, optName);
if (value === undefined) {
value = option.defaultValue;
}
}
}
if (option.hasOwnProperty('type')) {
switch (option.type) {

Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -4,7 +4,7 @@
"contributors": [
"Mac Angell <mac.ang311@gmail.com>"
],
"version": "0.6.0",
"version": "0.6.1",
"dependencies": {
"commander": "2.x.x",
"underscore": "1.x.x",
Expand All @@ -14,6 +14,7 @@
"mocha": "1.x.x",
"jshint": "0.x.x",
"blanket": "1.0.x",
"travis-cov": "0.2.x",
"rewire": "2.x.x",
"coveralls": "2.x.x",
"mocha-lcov-reporter": "0.x.x"
Expand All @@ -30,7 +31,7 @@
"url": "https://github.com/mac-/opter"
},
"scripts": {
"test": "make test && make test-cov | ./node_modules/coveralls/bin/coveralls.js",
"test": "make test && make test-cov && make test-lcov | ./node_modules/coveralls/bin/coveralls.js",
"blanket": {
"pattern": "//^((?!\/node_modules\/)(?!\/test\/).)*$/ig",
"onlyCwd": true,
Expand Down
17 changes: 17 additions & 0 deletions test/opter.test.js
Expand Up @@ -78,6 +78,23 @@ describe('Opter Unit Tests', function() {
done();
});

it('should read empty string values from args', function(done) {

setCommandLineArgsAndEnvVars([
'node', './test/opter.test.js',
'--my-option-from-args1', ''
]);

var cfg = opter({
myOptionFromArgs1: {
defaultValue: 'default1',
argument: 'string'
}
}, '0.1.0');
assert.strictEqual(cfg.myOptionFromArgs1, '', 'myOptionFromArgs1 is an empty string');
done();
});

it('should read boolean values from args', function(done) {

setCommandLineArgsAndEnvVars([
Expand Down

0 comments on commit 625ab73

Please sign in to comment.