Skip to content

Commit

Permalink
configuration.options.custom.[env|cwd|stdout|...]
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-kubula committed Feb 20, 2015
1 parent 381a004 commit dbed78e
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/add-hooks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async = require 'async'
hooks = require './hooks'
logger = require './logger'

addHooks = (runner, transactions, emitter) ->
addHooks = (runner, transactions, emitter, customConfig) ->
@emitter = emitter

for transaction in transactions
Expand All @@ -23,7 +23,7 @@ addHooks = (runner, transactions, emitter) ->

try
for file in files
proxyquire path.resolve(process.cwd(), file), {
proxyquire path.resolve((customConfig?.cwd or process.cwd()), file), {
'hooks': hooks
}
catch error
Expand Down
1 change: 1 addition & 0 deletions src/apply-configuration.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ applyConfiguration = (config) ->
server: null
emitter: new EventEmitter
options:
custom: {} # used for custom settings of various APIs or reporters
'dry-run': false
silent: false
reporter: null
Expand Down
44 changes: 22 additions & 22 deletions src/command.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class DreddCommand
# without the "new" keyword
@custom ?= {}

if typeof @custom.cwd isnt 'string'
if not @custom.cwd or typeof @custom.cwd isnt 'string'
@custom.cwd = process.cwd()

if not Array.isArray @custom.argv
if not @custom.argv or not Array.isArray @custom.argv
@custom.argv = process.argv.slice(2)

if not (@ instanceof DreddCommand)
return new DreddCommand()
return new DreddCommand(options, @cb)

warmUp: ->
@finished = false

@argv = optimist(@custom['argv'], @cwd).usage(
@argv = optimist(@custom['argv'], @custom['cwd']).usage(
"""
Usage:
dredd <path or URL to blueprint> <api_endpoint> [OPTIONS]
Expand All @@ -41,27 +41,24 @@ class DreddCommand

argError = false

if not @cb and @exit
@cb = @exit

if not @cb
@sigIntEventAdd = true
processExit = @exit or process.exit
@_processExit = @exit or process.exit
else
processExit = (code) =>
@_processExit = (code) =>
if @finished then return
@finished = true
if @sigIntEventAdded
process.removeEventListener 'SIGINT', @commandSigInt
@finished = true
return @cb code

if @argv.help is true
optimist.showHelp(console.error)
return @processExit(0)
return @_processExit(0)

if @argv.version is true
console.log version
return @processExit(0)
return @_processExit(0)

if not @argv._[0]?
console.error("\nError: Must specify path to blueprint file.")
Expand All @@ -74,15 +71,15 @@ class DreddCommand
if argError
console.error("\n")
optimist.showHelp(console.error)
return @processExit(1)
return @_processExit(1)

# transform path and p argument to array if it's not
if !Array.isArray(@argv['path'])
@argv['path'] = @argv['p'] = [@argv['path']]

configurationForDredd = @initConfig()
@dreddInstance = @initDredd configurationForDredd
return @
return self = @

lastArgvIsApiEndpoint: ->
# some shells are automatically expanding globs and concating result as arguments
Expand All @@ -104,26 +101,29 @@ class DreddCommand
configuration =
'server': @server
'options': @argv
'customEnv': @custom['env'] or process.env
'customStdout': @custom['stdout'] or process.stdout

# push first argument (without some known configuration --key) into paths
configuration.options.path ?= []
configuration.options.path.push @argv._[0]

configuration.options.custom ?= {}
configuration.options.custom['env'] = @custom['env'] or process.env
configuration.options.custom['stdout'] = @custom['stdout'] or process.stdout
configuration.options.custom['cwd'] = @custom['cwd'] or process.cwd()

return configuration

initDredd: (configuration) ->
try
dredd = new Dredd configuration
catch e
return @processExit e.code
return @_processExit e.code
return dredd

commandSigInt: =>
console.log "\nShutting down from SIGINT (Ctrl-C)"
@dreddInstance.transactionsComplete =>
@processExit(0)
@_processExit(0)

takeoff: =>
if @finished then return
Expand All @@ -144,14 +144,14 @@ class DreddCommand
console.error error.message
if error.stack
console.error error.stack
@processExit(1)
@_processExit(1)
else if stats.failures + stats.errors > 0
@processExit(1)
@_processExit(1)
else
@processExit(0)
@_processExit(0)
return
catch e
@processExit e.code
@_processExit e.code

return @

Expand Down
8 changes: 4 additions & 4 deletions src/configure-reporters.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ configureReporters = (config, stats, tests) ->
when 'junit'
xUnitReporter = new XUnitReporter(emitter, stats, tests, path, config.options.details)
when 'dot'
dotReporter = new DotReporter(emitter, stats, tests, (config.customStdout or process.stdout))
dotReporter = new DotReporter(emitter, stats, tests, config)
when 'nyan'
nyanCatReporter = new NyanCatReporter(emitter, stats, tests, (config.customStdout or process.stdout))
nyanCatReporter = new NyanCatReporter(emitter, stats, tests, config)
when 'html'
htmlReporter = new HtmlReporter(emitter, stats, tests, path, config.options.details)
when 'markdown'
mdReporter = new MarkdownReporter(emitter, stats, tests, path, config.options.details)
when 'apiary'
apiaryReporter = new ApiaryReporter(emitter, stats, tests, (config.customEnv or process.env))
apiaryReporter = new ApiaryReporter(emitter, stats, tests, config)
else
logger.warn "Invalid reporter #{reporter} selected, ignoring."

Expand All @@ -59,7 +59,7 @@ configureReporters = (config, stats, tests) ->
usedFileReportersLength = usedFileReporters.length
if reporters.indexOf('apiary') != -1
usedFileReportersLength = usedFileReportersLength - 1
if (config.customEnv or process.env)['DREDD_REST_TOKEN'] == undefined or (config.customEnv or process.env)['DREDD_REST_SUITE'] == undefined
unless (config.customEnv or process.env)['DREDD_REST_TOKEN']? and (config.customEnv or process.env)['DREDD_REST_SUITE']?
logger.warn "Apiary reporter environment variable DREDD_REST_TOKEN or DREDD_REST_SUITE not defined."

if usedFileReportersLength > outputs.length
Expand Down
40 changes: 24 additions & 16 deletions src/reporters/apiary-reporter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ String::startsWith = (str) ->
return this.slice(0, str.length) is str

class ApiaryReporter
constructor: (emitter, stats, tests, env) ->
constructor: (emitter, stats, tests, config) ->
@type = "cli"
@stats = stats
@tests = tests
Expand All @@ -21,33 +21,41 @@ class ApiaryReporter
@remoteId = null
@reportUrl = null
@configureEmitter emitter
@env = env

@env = @_fromConfigOrEnv.bind @, config

@errors = []
@verbose = @env['DREDD_REST_DEBUG']?
@verbose = @env('DREDD_REST_DEBUG')?
@configuration =
apiUrl: @env['APIARY_API_URL'] || "https://api.apiary.io"
apiToken: @env['APIARY_API_KEY'] || null
apiSuite: @env['APIARY_API_NAME'] || 'public'
apiUrl: @env('APIARY_API_URL') || "https://api.apiary.io"
apiToken: @env('APIARY_API_KEY') || null
apiSuite: @env('APIARY_API_NAME') || 'public'

logger.info 'Using apiary reporter.'

_fromConfigOrEnv: (config, property) ->
if not property?
return Object.keys(config?.custom or process.env)

returnedValue = config?.custom?[property]
if returnedValue?
returnedValue
else
process.env[property]

configureEmitter: (emitter) =>
emitter.on 'start', (blueprintsData, callback) =>
@uuid = uuid.v4()
@startedAt = Math.round(new Date().getTime() / 1000)

ciVars = [/^TRAVIS/, /^CIRCLE/, /^CI/, /^DRONE/]
envVarNames = Object.keys @env
ciVars = /^(TRAVIS|CIRCLE|CI|DRONE)/
envVarNames = @env()
ciEnvVars = {}
for envVarName in envVarNames
ciEnvVar = false

for ciVar in ciVars
if envVarName.match(ciVar) != null
ciEnvVar = true

if ciEnvVar == true
ciEnvVars[envVarName] = @env[envVarName]
if envVarName.match(ciVars)?
ciEnvVars[envVarName] = @env(envVarName)

# transform blueprints data to array
blueprints = []
Expand All @@ -56,9 +64,9 @@ class ApiaryReporter

data =
blueprints: blueprints
agent: @env['DREDD_AGENT'] || @env['USER']
agent: @env('DREDD_AGENT') || @env('USER')
agentRunUuid: @uuid
hostname: @env['DREDD_HOSTNAME'] || os.hostname()
hostname: @env('DREDD_HOSTNAME') || os.hostname()
startedAt: @startedAt
public: true
status: 'running'
Expand Down
4 changes: 2 additions & 2 deletions src/reporters/dot-reporter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ logger = require './../logger'
prettifyResponse = require './../prettify-response'

class DotReporter
constructor: (emitter, stats, tests, details, stdout) ->
constructor: (emitter, stats, tests, details, config) ->
@type = "dot"
@stats = stats
@tests = tests
@stdout = stdout or process.stdout
@stdout = config?.options?.custom?.stdout or process.stdout
@configureEmitter emitter
@errors = []

Expand Down
4 changes: 2 additions & 2 deletions src/reporters/nyan-reporter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ logger = require './../logger'
prettifyResponse = require './../prettify-response'

class NyanCatReporter
constructor: (emitter, stats, tests, stdout) ->
constructor: (emitter, stats, tests, config) ->
@type = "nyan"
@stats = stats
@tests = tests
@stdout = stdout or process.stdout
@stdout = config?.options?.custom?.stdout or process.stdout
@isatty = tty.isatty 1 and tty.isatty 2
if @isatty
if @stdout.getWindowSize
Expand Down
2 changes: 1 addition & 1 deletion src/transaction-runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TransactionRunner
async.mapSeries transactions, @configureTransaction, (err, results) ->
transactions = results

addHooks {}, transactions, @configuration.emitter
addHooks {}, transactions, @configuration.emitter, @configuration.options.custom

@executeAllTransactions(transactions, callback)

Expand Down
Loading

0 comments on commit dbed78e

Please sign in to comment.