Skip to content

Commit

Permalink
fix: Make sure logging level is set early enough in Dredd's lifecycle…
Browse files Browse the repository at this point in the history
… (plus some changes in wording of the 'Usage' text)
  • Loading branch information
honzajavorek committed Oct 4, 2016
1 parent 84817fd commit 826df23
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
44 changes: 27 additions & 17 deletions src/apply-configuration.coffee
@@ -1,7 +1,8 @@
{EventEmitter} = require 'events'
clone = require 'clone'

logger = require './logger'
logger = require('./logger')


coerceToArray = (value) ->
if Array.isArray value
Expand All @@ -13,8 +14,23 @@ coerceToArray = (value) ->
else
return value

applyConfiguration = (config) ->

applyLoggingOptions = (options) ->
# coerce color to bool
if options.color == 'false'
options.color = false
else if options.color == 'true'
options.color = true

logger.transports.console.colorize = options.color
logger.transports.console.silent = options.silent
logger.transports.console.timestamp = options.timestamp
logger.transports.console.level = options.level

return options


applyConfiguration = (config) ->
configuration =
blueprintPath: null
server: null
Expand Down Expand Up @@ -53,7 +69,6 @@ applyConfiguration = (config) ->
'hooks-worker-handler-host': 'localhost'
'hooks-worker-handler-port': 61321


# normalize options and config
for own key, value of config
# copy anything except "custom" hash
Expand All @@ -64,7 +79,7 @@ applyConfiguration = (config) ->
for own customKey, customVal of config['custom'] or {}
configuration['custom'][customKey] = clone customVal, false

#coerce single/multiple options into an array
# coerce single/multiple options into an array
configuration.options.reporter = coerceToArray(configuration.options.reporter)
configuration.options.output = coerceToArray(configuration.options.output)
configuration.options.header = coerceToArray(configuration.options.header)
Expand All @@ -76,24 +91,19 @@ applyConfiguration = (config) ->
if config.blueprintPath
configuration.options.path.push config.blueprintPath

# coerce color to bool
if configuration.options.color == 'false'
configuration.options.color = false
else if configuration.options.color == 'true'
configuration.options.color = true

for method in configuration.options.method
configuration.options.method.map((method) ->
method.toUpperCase()
)

if configuration.options.user?
authHeader = 'Authorization:Basic ' + new Buffer(configuration.options.user).toString('base64')
configuration.options.header.push authHeader

logger.transports.console.colorize = configuration.options.color
logger.transports.console.silent = configuration.options.silent
logger.transports.console.level = configuration.options.level
logger.transports.console.timestamp = configuration.options.timestamp

configuration.options = applyLoggingOptions(configuration.options)
return configuration

module.exports = applyConfiguration

module.exports = {
applyLoggingOptions
applyConfiguration
}
22 changes: 12 additions & 10 deletions src/dredd-command.coffee
Expand Up @@ -8,6 +8,7 @@ spawnArgs = require 'spawn-args'

Dredd = require './dredd'
interactiveConfig = require './interactive-config'
{applyLoggingOptions} = require './apply-configuration'
configUtils = require './config-utils'
logger = require('./logger')

Expand Down Expand Up @@ -36,25 +37,24 @@ class DreddCommand
@custom.argv = []

setOptimistArgv: ->
@optimist = optimist(@custom['argv'], @custom['cwd'])
@optimist = optimist(@custom.argv, @custom.cwd)
@cliArgv = @optimist.argv

@optimist.usage(
"""
@optimist.usage('''
Usage:
$ dredd init
Or:
$ dredd <path or URL to API description document> <api_endpoint> [OPTIONS]
$ dredd <path or URL to API description document> <URL of tested server> [OPTIONS]
Example:
$ dredd ./api-description.apib http://localhost:3000 --dry-run
"""
)
.options(Dredd.options)
.wrap(80)
''')
.options(Dredd.options)
.wrap(80)

@argv = @optimist.argv
@argv = applyLoggingOptions(@argv)

# Gracefully terminate server
stopServer: (callback) ->
Expand Down Expand Up @@ -195,8 +195,9 @@ class DreddCommand
return @_processExit(0)

loadDreddFile: ->
logger.verbose('Loading configuration file.')
configPath = @argv.config
logger.verbose('Loading configuration file:', configPath)

if configPath and fs.existsSync configPath
logger.info("Configuration '#{configPath}' found, ignoring other arguments.")
@argv = configUtils.load(configPath)
Expand All @@ -206,6 +207,8 @@ class DreddCommand
if key != "_" and key != "$0"
@argv[key] = value

@argv = applyLoggingOptions(@argv)

parseCustomConfig: ->
@argv.custom = configUtils.parseCustom @argv.custom

Expand Down Expand Up @@ -266,7 +269,6 @@ class DreddCommand
, waitMilis

run: ->
logger.verbose('Preparing for start of testing.')
for task in [
@setOptimistArgv
@parseCustomConfig
Expand Down

0 comments on commit 826df23

Please sign in to comment.