Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate confine block in case-insensitive way #2705

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/facter/custom_facts/util/confine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def to_s
end

# Evaluate the fact, returning true or false.
# if we have a block paramter then we only evaluate that instead
# if we have a block parameter then we only evaluate that instead
def true?
if @block && !@fact
begin
Expand All @@ -54,9 +54,11 @@ def true?

return false if value.nil?

# We call the block with both the downcased and raw fact value for
# backwards-compatibility.
if @block
begin
return !!@block.call(value)
return !!@block.call(value) || !!@block.call(fact.value) # rubocop:disable Style/DoubleNegation
rescue StandardError => e
log.debug "Confine raised #{e.class} #{e}"
return false
Expand Down
14 changes: 14 additions & 0 deletions spec/custom_facts/util/confine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ def confined(fact_value, *confines)
expect(confine.true?).to be true
end

it 'accepts and evaluate a block argument against the fact while respecting case' do
allow(fact).to receive(:value).and_return 'Foo'
confine = LegacyFacter::Util::Confine.new(:yay) { |f| f == 'Foo' }
expect(confine.true?).to be true
end

it 'accepts and evaluate multiple block arguments' do
allow(fact).to receive(:value).and_return 'bar'
first_confine = LegacyFacter::Util::Confine.new(:yay) { |f| f == 'foo' }
second_confine = LegacyFacter::Util::Confine.new(:yay) { |f| f == 'bar' }
expect(first_confine.true?).to be false
expect(second_confine.true?).to be true
end

it 'returns false if the block raises a StandardError when checking a fact' do
allow(fact).to receive(:value).and_return 'foo'
confine = LegacyFacter::Util::Confine.new(:yay) { |_f| raise StandardError }
Expand Down