Skip to content

Commit

Permalink
Print an error message when there are syntax errors in a config file. F…
Browse files Browse the repository at this point in the history
…ixes #2
  • Loading branch information
Brandon Fryslie committed Feb 23, 2017
1 parent 355e428 commit 6448aad
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 35 deletions.
9 changes: 5 additions & 4 deletions lib/config_lib.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ util = require './util'
_log = (args...) -> util.debug_log.apply null, [__filename].concat args

config_dir = process.env.STACKER_CONFIG_DIR ? "#{process.env.HOME}/.stacker"
config_file = "#{config_dir}/config.coffee"
config_file_path = "#{config_dir}/config.coffee"

exports =
get_config_dir: -> config_dir

get_config_file: -> config_file
get_config_file: -> config_file_path

get_config: _.memoize ->
config = {}
try
_log "requiring config file #{config_file}"
config = require config_file
_log "requiring config file #{config_file_path}"
config = require config_file_path
catch e
util.log_error "Error: Could not require stacker config file '#{config_file_path}'. Please check the syntax.".red
_log e
config?(exported_util) ? config

Expand Down
14 changes: 11 additions & 3 deletions lib/task_config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ require_task_config = _.memoize ->
task_config = {}

try
for file in fs.readdirSync(task_dir, ->) when fs.statSync("#{task_dir}/#{file}").isFile()
_log "requiring task file #{task_dir}/#{file}"
task_config[file.replace(/\.coffee$/, '')] = require "#{task_dir}/#{file}"
files = (file for file in fs.readdirSync(task_dir, ->) when fs.statSync("#{task_dir}/#{file}").isFile())
catch e
util.die "Error: could not find tasks directory #{task_dir}"

for file in files
_log "requiring task file #{task_dir}/#{file}"
try
config_file_path = "#{task_dir}/#{file}"
task_config[file.replace(/\.coffee$/, '')] = require config_file_path
catch e
util.log_error "Error: Could not require task config file '#{config_file_path}'. Please check the syntax.".red
_log e

task_config

Expand Down
41 changes: 21 additions & 20 deletions lib/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ log_error = (msg...) ->
msg = color_array msg, 'red'
print.apply null, msg

# only print error
log_error = (msg...) ->
msg = color_array msg, 'red'
print.apply null, msg
# print error and exit with status code 1
die = (msg...) ->
log_error.apply null, msg
process.exit 1

# debug logging
get_debug = ->
Expand Down Expand Up @@ -229,27 +229,28 @@ get_current_version = ->
JSON.parse(fs.readFileSync "#{__dirname}/../package.json").version

module.exports = {
get_debug: -> DEBUG
set_debug
beautify_obj
clone_apply
color_array
debug_log
die
error
get_color_fn
get_current_version
get_debug: -> DEBUG
kill_tree
log_error
log_proc_error
prefix_pipe_output
print
trim
color_array
regex_extract
clone_apply
object_map
pipe_with_prefix
get_color_fn
wait_for_keypress
try_to_clone
prefix_pipe_output
pretty_command_str
beautify_obj
object_map
print
print_process_status
kill_tree
regex_extract
set_debug
start_progress_indicator
get_current_version
}
trim
try_to_clone
wait_for_keypress
}
25 changes: 25 additions & 0 deletions test/config.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ task_config_fn = """
wait_for: 'Started'
"""

task_config_invalid = """
module.exports = (state, util) ->
name:
"""

parallel 'task config', ->
it 'can use a config that is a plain JS object', ->
with_stacker
Expand All @@ -33,6 +38,14 @@ parallel 'task config', ->
, (stacker) ->
stacker.wait_for /Started Fn!/

it 'throws an error when requiring an invalid task config', ->
with_stacker
cmd: 'test-fn'
task_config:
'test-fn': task_config_invalid
, (stacker) ->
stacker.wait_for /Error: Could not require task config file/

stacker_config_plain_js = """
module.exports =
args:
Expand All @@ -47,6 +60,11 @@ module.exports = ->
default: 'wonderful argument'
"""

stacker_config_invalid = """
module.exports = ->
args:
"""

parallel 'stacker config', ->
it 'can use a config that is a plain JS object', ->
with_stacker
Expand All @@ -63,3 +81,10 @@ parallel 'stacker config', ->
, (stacker) ->
stacker.send_cmd 'env'
stacker.wait_for /config_argument=wonderful argument/

it 'throws an error when requiring an invalid stacker config', ->
with_stacker
cmd: ''
stacker_config: stacker_config_invalid
, (stacker) ->
stacker.wait_for /Error: Could not require stacker config file/
16 changes: 8 additions & 8 deletions test/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ Stacker = class Stacker
@wait_for(/Killed running tasks!|/)

with_stacker = (opt, fn) ->
dirPath = null
if opt.stacker_config? or !_.isEmpty opt.task_config
dirPath = temp.mkdirSync()
opt.env ?= {}
opt.env.STACKER_CONFIG_DIR = dirPath
# Create stacker config path
dirPath = temp.mkdirSync()
fs.mkdirSync("#{dirPath}/tasks")
fs.writeFileSync "#{dirPath}/config.coffee", opt.stacker_config

if opt.stacker_config?
fs.writeFileSync "#{dirPath}/config.coffee", opt.stacker_config
# Setup stacker config path
opt.env ?= {}
opt.env.STACKER_CONFIG_DIR = dirPath

# Setup task configs
if !_.isEmpty opt.task_config
fs.mkdirSync("#{dirPath}/tasks")
for name, config of opt.task_config
fs.writeFileSync "#{dirPath}/tasks/#{name}.coffee", config

Expand Down

0 comments on commit 6448aad

Please sign in to comment.