Skip to content

Commit 3b0ddc5

Browse files
committed
Stop the phpversion fact raising without php
Facter::Core::Execution.execute raises ExecutionFailure when the command cannot be run, whereas the Facter::Util::Resolution.exec it replaced in f351625 returned nil. Since that commit every node without php logs Error: Facter: Error while resolving custom fact fact='phpversion', resolution='<anonymous>': Could not execute 'php -v': command not found on every run - the same class of noise the nil guard in 6c1e69e (issue 134) was added to prevent. Passing on_fail: nil restores the old semantics, and is what the deprecated Facter::Core::Execution.exec did internally. The fact resolves to nil either way, since Facter catches and logs the exception, so the added spec asserts that nothing is logged. Assisted-By: Claude Opus 5 <noreply@anthropic.com> Reviewed-by: Tobias Brox <tobias@redpill-linpro.com>
1 parent 1a5c207 commit 3b0ddc5

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎lib/facter/phpversion.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Facter.add(:phpversion) do
44
setcode do
5-
output = Facter::Core::Execution.execute('php -v')
5+
output = Facter::Core::Execution.execute('php -v', on_fail: nil)
66

77
unless output.nil?
88
output.split("\n").first.split
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'phpversion fact' do
6+
subject(:fact) { Facter.fact(:phpversion).value }
7+
8+
before do
9+
Facter.clear
10+
load File.expand_path('../../../lib/facter/phpversion.rb', __dir__)
11+
end
12+
13+
after { Facter.clear }
14+
15+
context 'when php is installed' do
16+
before do
17+
allow(Facter::Core::Execution).to receive(:execute).with('php -v', any_args).and_return(
18+
"PHP 8.2.7 (cli) (built: Jun 8 2023 20:03:25) (NTS)\n" \
19+
"Copyright (c) The PHP Group\n" \
20+
"Zend Engine v4.2.7, Copyright (c) Zend Technologies\n",
21+
)
22+
end
23+
24+
it { is_expected.to eq '8.2.7' }
25+
end
26+
27+
context 'when php is not installed' do
28+
before do
29+
allow(Facter).to receive(:log_exception).and_call_original
30+
allow(Facter::Core::Execution).to receive(:execute).with('php -v', any_args) do |_command, options = {}|
31+
on_fail = options.fetch(:on_fail, :raise)
32+
raise Facter::Core::Execution::ExecutionFailure, "Could not execute 'php -v': command not found" if on_fail == :raise
33+
34+
on_fail
35+
end
36+
end
37+
38+
it 'resolves to nil without logging an exception' do
39+
expect(fact).to be_nil
40+
expect(Facter).not_to have_received(:log_exception)
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)