File tree Expand file tree Collapse file tree 3 files changed +53
-3
lines changed
spec/octocatalog-diff/tests/util Expand file tree Collapse file tree 3 files changed +53
-3
lines changed Original file line number Diff line number Diff line change 4
4
require_relative 'facts/json'
5
5
require_relative 'facts/yaml'
6
6
require_relative 'facts/puppetdb'
7
+ require_relative 'util/util'
7
8
require_relative 'external/pson/pure'
8
9
9
10
module OctocatalogDiff
@@ -17,10 +18,10 @@ class Facts
17
18
def initialize ( options = { } , facts = nil )
18
19
@node = options . fetch ( :node , '' )
19
20
@timestamp = false
20
- @options = options . dup
21
+ @options = OctocatalogDiff :: Util :: Util . safe_dup ( options )
21
22
if facts
22
23
@facts = { }
23
- facts . each { |k , v | @facts [ k ] = v . dup }
24
+ facts . each { |k , v | @facts [ k ] = OctocatalogDiff :: Util :: Util . safe_dup ( v ) }
24
25
else
25
26
case options [ :backend ]
26
27
when :json
@@ -33,7 +34,7 @@ def initialize(options = {}, facts = nil)
33
34
raise ArgumentError , 'Invalid fact source backend'
34
35
end
35
36
@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 ) }
37
38
end
38
39
end
39
40
Original file line number Diff line number Diff line change @@ -16,6 +16,15 @@ def self.object_is_any_of?(object, classes)
16
16
classes . each { |clazz | return true if object . is_a? clazz }
17
17
false
18
18
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
19
28
end
20
29
end
21
30
end
Original file line number Diff line number Diff line change 18
18
expect ( described_class . object_is_any_of? ( object , classes ) ) . to eq ( false )
19
19
end
20
20
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
21
61
end
You can’t perform that action at this time.
0 commit comments