File tree Expand file tree Collapse file tree
spec/octocatalog-diff/tests/util Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44require_relative 'facts/json'
55require_relative 'facts/yaml'
66require_relative 'facts/puppetdb'
7+ require_relative 'util/util'
78require_relative 'external/pson/pure'
89
910module 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
Original file line number Diff line number Diff 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
2130end
Original file line number Diff line number Diff line change 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
2161end
You can’t perform that action at this time.
0 commit comments