Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/teracy-dev/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'bundler/setup'

RSpec.configure do |config|
config.example_status_persistence_file_path = '.rspec_status'
end
2 changes: 2 additions & 0 deletions spec/teracy-dev/fixtures/teracy-dev/util/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this file contains only comments and should be treated as empty YAML
# expected to load as an empty hash
16 changes: 16 additions & 0 deletions spec/teracy-dev/utility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
Loading