Skip to content

Commit 1c82b20

Browse files
committed
Create safe_dup method and set facts to use it
1 parent 09e952a commit 1c82b20

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

lib/octocatalog-diff/facts.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_relative 'facts/json'
55
require_relative 'facts/yaml'
66
require_relative 'facts/puppetdb'
7+
require_relative 'util/util'
78
require_relative 'external/pson/pure'
89

910
module OctocatalogDiff
@@ -17,10 +18,10 @@ class Facts
1718
def initialize(options = {}, facts = nil)
1819
@node = options.fetch(:node, '')
1920
@timestamp = false
20-
@options = options.dup
21+
@options = OctocatalogDiff::Util::Util.safe_dup(options)
2122
if facts
2223
@facts = {}
23-
facts.each { |k, v| @facts[k] = v.dup }
24+
facts.each { |k, v| @facts[k] = OctocatalogDiff::Util::Util.safe_dup(v) }
2425
else
2526
case options[:backend]
2627
when :json
@@ -33,7 +34,7 @@ def initialize(options = {}, facts = nil)
3334
raise ArgumentError, 'Invalid fact source backend'
3435
end
3536
@facts = {}
36-
@orig_facts.each { |k, v| @facts[k] = v.dup }
37+
@orig_facts.each { |k, v| @facts[k] = OctocatalogDiff::Util::Util.safe_dup(v) }
3738
end
3839
end
3940

lib/octocatalog-diff/util/util.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def self.object_is_any_of?(object, classes)
1616
classes.each { |clazz| return true if object.is_a? clazz }
1717
false
1818
end
19+
20+
# Utility Method!
21+
# `.dup` can't be called on certain objects (Fixnum for example). This
22+
# method returns the original object if it can't be duplicated.
23+
def self.safe_dup(object)
24+
object.dup
25+
rescue TypeError
26+
object
27+
end
1928
end
2029
end
2130
end

spec/octocatalog-diff/tests/util/util_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,44 @@
1818
expect(described_class.object_is_any_of?(object, classes)).to eq(false)
1919
end
2020
end
21+
22+
describe '#safe_dup' do
23+
it 'should work with nil' do
24+
object = nil
25+
result = described_class.safe_dup(object)
26+
expect(object).to eq(result)
27+
end
28+
29+
it 'should work with a string' do
30+
object = 'boots and cats'
31+
result = described_class.safe_dup(object)
32+
expect(object).to eq(result)
33+
end
34+
35+
it 'should work with a symbol' do
36+
object = :chickens
37+
result = described_class.safe_dup(object)
38+
expect(object).to eq(result)
39+
end
40+
41+
it 'should work with an integer' do
42+
object = 42
43+
result = described_class.safe_dup(object)
44+
expect(object).to eq(result)
45+
end
46+
47+
it 'should work with a hash' do
48+
object = { 'foo' => 'bar', 'baz' => 'buzz' }
49+
result = described_class.safe_dup(object)
50+
expect(object).to eq(result)
51+
expect(object.object_id).not_to eq(result.object_id)
52+
end
53+
54+
it 'should work with an array' do
55+
object = %w[foo bar baz buzz]
56+
result = described_class.safe_dup(object)
57+
expect(object).to eq(result)
58+
expect(object.object_id).not_to eq(result.object_id)
59+
end
60+
end
2161
end

0 commit comments

Comments
 (0)