Skip to content

Commit 2d118cb

Browse files
committed
Evaluate confine block in case-insensitive way
Prior to this commit when a user provided a confine block, Facter would downcase the value when evaluating it. For example: confine :kernel do |value| value == "Linux" end While Facter's public documentation states that this is a valid way to write a confine block, it would incorrectly and unexpectedly evaluate as false on Linux systems. However, these other styles of confine would return true: confine :kernel => "Linux" confine kernel: "Linux" This downcasing behavior was introduced in 7a81945 as a way of comparing values in a case-insensitive way. However when block confines were introduced in e4c8689, it added block evaluation before value comparison, making the case-insensitive comparison moot with blocks. This commit retains existing behavior of evaluating a confine block with a downcased fact value, while adding evaluation with the raw fact value to ensure expected behavior.
1 parent 5335371 commit 2d118cb

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/facter/custom_facts/util/confine.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def to_s
3535
end
3636

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

5555
return false if value.nil?
5656

57+
# We call the block with both the downcased and raw fact value for
58+
# backwards-compatibility.
5759
if @block
5860
begin
59-
return !!@block.call(value)
61+
return !!@block.call(value) || !!@block.call(fact.value) # rubocop:disable Style/DoubleNegation
6062
rescue StandardError => e
6163
log.debug "Confine raised #{e.class} #{e}"
6264
return false

spec/custom_facts/util/confine_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ def confined(fact_value, *confines)
126126
expect(confine.true?).to be true
127127
end
128128

129+
it 'accepts and evaluate a block argument against the fact while respecting case' do
130+
allow(fact).to receive(:value).and_return 'Foo'
131+
confine = LegacyFacter::Util::Confine.new(:yay) { |f| f == 'Foo' }
132+
expect(confine.true?).to be true
133+
end
134+
135+
it 'accepts and evaluate multiple block arguments' do
136+
allow(fact).to receive(:value).and_return 'bar'
137+
first_confine = LegacyFacter::Util::Confine.new(:yay) { |f| f == 'foo' }
138+
second_confine = LegacyFacter::Util::Confine.new(:yay) { |f| f == 'bar' }
139+
expect(first_confine.true?).to be false
140+
expect(second_confine.true?).to be true
141+
end
142+
129143
it 'returns false if the block raises a StandardError when checking a fact' do
130144
allow(fact).to receive(:value).and_return 'foo'
131145
confine = LegacyFacter::Util::Confine.new(:yay) { |_f| raise StandardError }

0 commit comments

Comments
 (0)