Skip to content

Commit 8579339

Browse files
authored
Merge pull request #625 from hoatle/bugs/#624-empty-override-file
@ #624 | Bug: vagrant status fails with undefined method 'each' for nil
2 parents a4249b9 + ab2d078 commit 8579339

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

lib/teracy-dev/util.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def self.load_yaml_file(file_path)
9090
if File.exist? file_path
9191
# TODO: exception handling
9292
result = YAML.load_file(file_path)
93-
if result == false
93+
if result == false || result.nil?
9494
@logger.debug("#{file_path} is empty")
9595
result = {}
9696
end
@@ -177,8 +177,11 @@ def self.require_version_valid?(version, requirements)
177177
# TODO: refactor this into a new module (merger.rb), currently, maintaining this is a nightmare
178178
def self.override(origin_hash, source_hash)
179179
# immutable
180-
origin_hash = origin_hash.clone
181-
source_hash = source_hash.clone
180+
origin_hash = origin_hash.clone unless origin_hash.nil?
181+
source_hash = source_hash.clone unless source_hash.nil?
182+
183+
origin_hash ||= {}
184+
source_hash ||= {}
182185

183186
source_hash.each do |key, value|
184187
next if value.nil?

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'bundler/setup'
2+
3+
RSpec.configure do |config|
4+
config.example_status_persistence_file_path = '.rspec_status'
5+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# this file contains only comments and should be treated as empty YAML
2+
# expected to load as an empty hash

spec/teracy-dev/utility_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
end
1212
end
1313

14+
context 'given a nil obj2' do
15+
it 'returns the same obj1' do
16+
obj1 = { 'msg' => 'hello' }
17+
new_obj = TeracyDev::Util.override(obj1, nil)
18+
expect(new_obj).to eql(obj1)
19+
end
20+
end
21+
22+
context 'when loading an empty YAML file' do
23+
it 'returns an empty hash' do
24+
file = File.dirname(__FILE__) + '/fixtures/teracy-dev/util/empty.yaml'
25+
result = TeracyDev::Util.load_yaml_file(file)
26+
expect(result).to eql({})
27+
end
28+
end
29+
1430
context 'given a simple obj2' do
1531
it 'returns the new obj' do
1632
obj1 = { 'msg' => 'hello' }

0 commit comments

Comments
 (0)