Skip to content

Commit

Permalink
feat(cli): Better CLI args validation
Browse files Browse the repository at this point in the history
Start adding better CLI args validation (#603). Validate log-level to start.
  • Loading branch information
srawlins committed Jul 16, 2015
1 parent 09d64ed commit 73d31c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/cli.js
Expand Up @@ -38,7 +38,16 @@ var processArgs = function (argv, options, fs, path) {
}

if (helper.isString(options.logLevel)) {
options.logLevel = constant['LOG_' + options.logLevel.toUpperCase()] || constant.LOG_DISABLE
var logConstant = constant['LOG_' + options.logLevel.toUpperCase()]
if (helper.isDefined(logConstant)) {
options.logLevel = logConstant
} else {
console.error('Log level must be one of disable, error, warn, info, or debug.')
process.exit(1)
}
} else if (helper.isDefined(options.logLevel)) {
console.error('Log level must be one of disable, error, warn, info, or debug.')
process.exit(1)
}

if (helper.isString(options.singleRun)) {
Expand Down
31 changes: 28 additions & 3 deletions test/unit/cli.spec.coffee
Expand Up @@ -8,6 +8,8 @@ describe 'cli', ->
constant = require '../../lib/constants'
path = require 'path'
mocks = require 'mocks'
loadFile = mocks.loadFile
mockery = m = e = null

fsMock = mocks.fs.create
cwd:
Expand All @@ -26,9 +28,26 @@ describe 'cli', ->

processArgs = (args, opts) ->
argv = optimist.parse(args)
cli.processArgs argv, opts || {}, fsMock, pathMock

beforeEach -> setCWD '/'
e.processArgs argv, opts || {}, fsMock, pathMock

beforeEach ->
setCWD '/'
mockery = {}
mockery.process = exit: sinon.spy()
mockery.console = error: sinon.spy()

# load file under test
m = loadFile __dirname + '/../../lib/cli.js', mockery, {
global: {},
console: mockery.console,
process: mockery.process,
require: (path) ->
if path.indexOf('./') is 0
require '../../lib/' + path
else
require path
}
e = m.exports

describe 'processArgs', ->

Expand Down Expand Up @@ -95,6 +114,12 @@ describe 'cli', ->
options = processArgs ['--log-level', 'warn']
expect(options.logLevel).to.equal constant.LOG_WARN

options = processArgs ['--log-level', 'foo']
expect(mockery.process.exit).to.have.been.calledWith 1

options = processArgs ['--log-level']
expect(mockery.process.exit).to.have.been.calledWith 1


it 'should parse browsers into an array', ->
options = processArgs ['--browsers', 'Chrome,ChromeCanary,Firefox']
Expand Down

0 comments on commit 73d31c2

Please sign in to comment.