Skip to content

Commit 8d21da1

Browse files
authored
Merge pull request #8603 from jpartlow/pup-11061-sensitive-equality
(PUP-11061) Override Sensitive equality/hash
2 parents 8fc33e8 + b19133c commit 8d21da1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/puppet/pops/types/p_sensitive_type.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ def to_s
2424
def inspect
2525
"#<#{self}>"
2626
end
27+
28+
def hash
29+
@value.hash
30+
end
31+
32+
def ==(other)
33+
other.is_a?(Sensitive) &&
34+
other.hash == hash
35+
end
36+
alias eql? ==
2737
end
2838

2939
def self.register_ptype(loader, ir)

spec/unit/pops/types/p_sensitive_type_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ module Types
113113
expect(eval_and_collect_notices(code)).to eq(['Sensitive[Integer] != Sensitive[String]'])
114114
end
115115

116+
it 'equals another instance with the same value' do
117+
code =<<-CODE
118+
$i = Sensitive('secret')
119+
$o = Sensitive('secret')
120+
notice($i == $o)
121+
CODE
122+
expect(eval_and_collect_notices(code)).to eq(['true'])
123+
end
124+
125+
it 'has equal hash keys for same values' do
126+
code =<<-CODE
127+
$i = Sensitive('secret')
128+
$o = Sensitive('secret')
129+
notice({$i => 1} == {$o => 1})
130+
CODE
131+
expect(eval_and_collect_notices(code)).to eq(['true'])
132+
end
133+
116134
it 'can be created from another sensitive instance ' do
117135
code =<<-CODE
118136
$o = Sensitive("hunter2")

0 commit comments

Comments
 (0)