Skip to content

Commit

Permalink
Enhancement: validate additional_json
Browse files Browse the repository at this point in the history
  • Loading branch information
till committed Oct 31, 2013
1 parent 1c39549 commit fc58964
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 11 additions & 8 deletions lib/bib/vagrant/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def initialize(home = "~", verbose = true)
end

def get

vagrantconfig = get_defaults

begin
Expand Down Expand Up @@ -51,17 +50,12 @@ def validate!(config)
errors << "cookbook_path: does not exist" unless File.directory?(config['cookbook_path'])
errors << "chef_log_level: must be one of #{log_level.join}" unless log_level.include?(config['chef_log_level'])


if !config['additional_json'].empty?
begin
JSON.parse(config['additional_json'])
rescue JSON::ParserError
errors << "additional_json: should be empty or valid json"
end
errors << "additional_json: must be empty or valid json" unless is_valid_json?(config['additional_json'])
end

if errors.count == 0
return
return true
end

raise "Errors: #{errors.join(', ')}"
Expand Down Expand Up @@ -91,6 +85,15 @@ def get_defaults
}
end

def is_valid_json?(json)
begin
JSON.parse(json)
return true
rescue JSON::ParserError
false
end
end

end
end
end
7 changes: 6 additions & 1 deletion test/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def test_validate!
c.validate!(config)
}

config["additional_json"] = "{'hello': 'world'}"
config["nfs"] = false
config["cookbook_path"] = @@fixture_dir

assert(c.validate!(config))

config["additional_json"] = "{'hello'}"

assert_raises(RuntimeError) {
c.validate!(config)
Expand Down

0 comments on commit fc58964

Please sign in to comment.