From 625ab7361d28cd4c6346de529c846e7c3d119f41 Mon Sep 17 00:00:00 2001 From: mac- Date: Wed, 8 Jan 2014 12:51:47 -0700 Subject: [PATCH] fixing issue with empty strings not being a valid option value --- Makefile | 5 ++++- lib/opter.js | 11 ++++++++++- package.json | 5 +++-- test/opter.test.js | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 85e940b..6841acb 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/lib/opter.js b/lib/opter.js index b269dda..1d42b0c 100644 --- a/lib/opter.js +++ b/lib/opter.js @@ -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) { diff --git a/package.json b/package.json index f6a1332..76c5f8b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "contributors": [ "Mac Angell " ], - "version": "0.6.0", + "version": "0.6.1", "dependencies": { "commander": "2.x.x", "underscore": "1.x.x", @@ -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" @@ -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, diff --git a/test/opter.test.js b/test/opter.test.js index 9c5d3ef..6ccf5ca 100644 --- a/test/opter.test.js +++ b/test/opter.test.js @@ -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([