Skip to content

Commit

Permalink
A bit more intuitive messages if BG_CONFIG_FILE is set wrongly, BGBUI…
Browse files Browse the repository at this point in the history
…LD-233
  • Loading branch information
goldmann committed Jun 17, 2011
1 parent 528d28c commit fdda0cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,5 +2,6 @@ coverage
.idea
*.gem
*.log
*.swp
pkg
*.gemspec
17 changes: 12 additions & 5 deletions lib/boxgrinder-core/models/config.rb
Expand Up @@ -46,17 +46,24 @@ def initialize(values = {})
)

if ENV['BG_CONFIG_FILE']
unless ENV['BG_CONFIG_FILE'].strip.empty?
raise(Errno::ENOENT, ENV['BG_CONFIG_FILE']) unless File.exists? ENV['BG_CONFIG_FILE']
end
raise "You specified empty configuration file path. Please make sure you set correct path for BG_CONFIG_FILE environment variable." if ENV['BG_CONFIG_FILE'].strip.empty?
raise "Configuration file '#{ENV['BG_CONFIG_FILE']}' couldn't be found. Please make sure you set correct path for BG_CONFIG_FILE environment variable." unless File.exists?(ENV['BG_CONFIG_FILE'])
end

deep_merge(self, YAML.load_file(self.file)) if File.exists?(self.file)
merge!(values.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo })
deep_merge!(YAML.load_file(self.file)) if File.exists?(self.file)
merge_with_symbols!(values)

self.backtrace = true if [:debug, :trace].include?(self.log_level)
end

def merge_with_symbols!(values)
merge!(values.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo })
end

def deep_merge!(h)
deep_merge(self, h)
end

def deep_merge(first, second)
second.each_key do |k|
if first[k.to_sym].is_a?(Hash) and second[k].is_a?(Hash)
Expand Down
18 changes: 13 additions & 5 deletions spec/models/config-spec.rb
Expand Up @@ -24,8 +24,9 @@ module BoxGrinder
it "should not load options from file if it doesn't exist" do
ENV['BG_CONFIG_FILE'] = ""

config = Config.new
config.force.should == false
lambda {
config = Config.new
}.should raise_error(RuntimeError, "You specified empty configuration file path. Please make sure you set correct path for BG_CONFIG_FILE environment variable.")
end

it "should load empty config file" do
Expand All @@ -48,14 +49,21 @@ module BoxGrinder

it "should raise a file not found error if BG_CONFIG_FILE is set, but the path is invalid" do
ENV['BG_CONFIG_FILE'] = "leo/tol/stoy"
lambda { Config.new }.should raise_error(Errno::ENOENT)
lambda { Config.new }.should raise_error(RuntimeError, "Configuration file 'leo/tol/stoy' couldn't be found. Please make sure you set correct path for BG_CONFIG_FILE environment variable.")
end

it "should merge platform" do
it "should raise if the specified config file is whitespace" do
ENV['BG_CONFIG_FILE'] = " "

config = Config.new.merge(:platform => :ec2)
lambda {
config = Config.new
}.should raise_error(RuntimeError, "You specified empty configuration file path. Please make sure you set correct path for BG_CONFIG_FILE environment variable.")
end

it "should merge platform" do
# Make sure we don't have the variable defined anymore
ENV.delete('BG_CONFIG_FILE')
config = Config.new.merge(:platform => :ec2)
config.platform.should == :ec2
end
end
Expand Down

0 comments on commit fdda0cf

Please sign in to comment.