diff --git a/lib/teracy-dev/util.rb b/lib/teracy-dev/util.rb index 66f129e4..a18f3365 100644 --- a/lib/teracy-dev/util.rb +++ b/lib/teracy-dev/util.rb @@ -90,7 +90,7 @@ def self.load_yaml_file(file_path) if File.exist? file_path # TODO: exception handling result = YAML.load_file(file_path) - if result == false + if result == false || result.nil? @logger.debug("#{file_path} is empty") result = {} end @@ -177,8 +177,11 @@ def self.require_version_valid?(version, requirements) # TODO: refactor this into a new module (merger.rb), currently, maintaining this is a nightmare def self.override(origin_hash, source_hash) # immutable - origin_hash = origin_hash.clone - source_hash = source_hash.clone + origin_hash = origin_hash.clone unless origin_hash.nil? + source_hash = source_hash.clone unless source_hash.nil? + + origin_hash ||= {} + source_hash ||= {} source_hash.each do |key, value| next if value.nil? diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..a6d97b4c --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,5 @@ +require 'bundler/setup' + +RSpec.configure do |config| + config.example_status_persistence_file_path = '.rspec_status' +end diff --git a/spec/teracy-dev/fixtures/teracy-dev/util/empty.yaml b/spec/teracy-dev/fixtures/teracy-dev/util/empty.yaml new file mode 100644 index 00000000..7c62a4a1 --- /dev/null +++ b/spec/teracy-dev/fixtures/teracy-dev/util/empty.yaml @@ -0,0 +1,2 @@ +# this file contains only comments and should be treated as empty YAML +# expected to load as an empty hash diff --git a/spec/teracy-dev/utility_spec.rb b/spec/teracy-dev/utility_spec.rb index f62bcdb3..d173a633 100644 --- a/spec/teracy-dev/utility_spec.rb +++ b/spec/teracy-dev/utility_spec.rb @@ -11,6 +11,22 @@ end end + context 'given a nil obj2' do + it 'returns the same obj1' do + obj1 = { 'msg' => 'hello' } + new_obj = TeracyDev::Util.override(obj1, nil) + expect(new_obj).to eql(obj1) + end + end + + context 'when loading an empty YAML file' do + it 'returns an empty hash' do + file = File.dirname(__FILE__) + '/fixtures/teracy-dev/util/empty.yaml' + result = TeracyDev::Util.load_yaml_file(file) + expect(result).to eql({}) + end + end + context 'given a simple obj2' do it 'returns the new obj' do obj1 = { 'msg' => 'hello' }