Skip to content

Commit 6888827

Browse files
Merge pull request #42 from smortex/unwrap-in-errors
Unwrap sensitive values in error messages
2 parents 2ab0d16 + b9dde53 commit 6888827

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lib/rspec-puppet/matchers/parameter_matcher.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def matches?(resource)
2828
actual = resource[@parameter]
2929
expected = @value
3030

31+
actual = RSpec::Puppet::Sensitive.new(actual.unwrap) if actual.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
32+
3133
# Puppet flattens an array with a single value into just the value and
3234
# this can cause confusion when testing as people expect when you put
3335
# an array in, you'll get an array out.

lib/rspec-puppet/sensitive.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def inspect
2424
"Sensitive(#{@value.inspect})"
2525
end
2626

27+
# @return the unwrapped value (needed to show diff)
28+
def to_s
29+
inspect
30+
end
31+
2732
# Check for equality with another value.
2833
# If compared to Puppet Sensitive type, it compares the wrapped values.
2934

spec/unit/matchers/parameter_matcher_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,34 @@
108108
expect(subject.matches?(foo_parameter: nil)).to be(false)
109109
end
110110
end
111+
112+
context 'with sensitive("foo") expected' do
113+
subject do
114+
described_class.new(:foo_parameter, RSpec::Puppet::Sensitive.new('foo'), :should)
115+
end
116+
117+
it 'matches sensitive("foo")' do
118+
expect(subject.matches?(foo_parameter: RSpec::Puppet::Sensitive.new('foo'))).to be(true)
119+
expect(subject.errors.size).to eq(0)
120+
end
121+
122+
it 'does not match sensitive("bar")' do
123+
expect(subject.matches?(foo_parameter: RSpec::Puppet::Sensitive.new('bar'))).to be(false)
124+
expect(subject.errors.size).to eq(1)
125+
expect(subject.errors[0].message).to eq('foo_parameter set to Sensitive("foo") but it is set to Sensitive("bar")')
126+
end
127+
128+
it 'does not matches "foo"' do
129+
expect(subject.matches?(foo_parameter: 'foo')).to be(false)
130+
expect(subject.errors.size).to eq(1)
131+
expect(subject.errors[0].message).to eq('foo_parameter set to Sensitive("foo") but it is set to "foo"')
132+
end
133+
134+
it 'does not matches "Sensitive [value redacted]"' do
135+
expect(subject.matches?(foo_parameter: 'Sensitive [value redacted]')).to be(false)
136+
expect(subject.errors.size).to eq(1)
137+
expect(subject.errors[0].message).to eq('foo_parameter set to Sensitive("foo") but it is set to "Sensitive [value redacted]"')
138+
end
139+
end
111140
end
112141
end

0 commit comments

Comments
 (0)