Skip to content

Commit c94bb84

Browse files
committed
Confine the phpversion fact on php in PATH
Addresses a review comment on #768: a confine is the idiomatic way to keep a command-backed fact quiet where the command is missing, as done in puppet-letsencrypt's certbot_version fact. The fact is now not resolved at all on nodes without php, rather than running php -v and discarding the failure. The on_fail: nil from the previous commit is kept: it still covers a php that is in PATH but cannot run (broken install, missing libraries, permissions), which the confine does not catch. Prompt: (review comment on the pull request) I think it should be using a `confine` like this: https://github.com/voxpupuli/puppet-letsencrypt/blob/836d38e5526e4b6286d3bfe28f1cd412ee7e2b4e/lib/facter/certbot_version.rb Prompt: #768 got a comment. I believe the work on this was done under [a local working copy] Assisted-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 3b0ddc5 commit c94bb84

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎lib/facter/phpversion.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
Facter.add(:phpversion) do
4+
confine { Facter::Core::Execution.which('php') }
5+
46
setcode do
57
output = Facter::Core::Execution.execute('php -v', on_fail: nil)
68

‎spec/unit/facter/phpversion_spec.rb‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
before do
99
Facter.clear
10+
allow(Facter).to receive(:log_exception).and_call_original
1011
load File.expand_path('../../../lib/facter/phpversion.rb', __dir__)
1112
end
1213

1314
after { Facter.clear }
1415

1516
context 'when php is installed' do
1617
before do
18+
allow(Facter::Core::Execution).to receive(:which).with('php').and_return('/usr/bin/php')
1719
allow(Facter::Core::Execution).to receive(:execute).with('php -v', any_args).and_return(
1820
"PHP 8.2.7 (cli) (built: Jun 8 2023 20:03:25) (NTS)\n" \
1921
"Copyright (c) The PHP Group\n" \
@@ -24,9 +26,21 @@
2426
it { is_expected.to eq '8.2.7' }
2527
end
2628

27-
context 'when php is not installed' do
29+
context 'when php is not in the path' do
2830
before do
29-
allow(Facter).to receive(:log_exception).and_call_original
31+
allow(Facter::Core::Execution).to receive(:which).with('php').and_return(nil)
32+
allow(Facter::Core::Execution).to receive(:execute)
33+
end
34+
35+
it 'resolves to nil without running php' do
36+
expect(fact).to be_nil
37+
expect(Facter::Core::Execution).not_to have_received(:execute)
38+
end
39+
end
40+
41+
context 'when php is in the path but cannot be executed' do
42+
before do
43+
allow(Facter::Core::Execution).to receive(:which).with('php').and_return('/usr/bin/php')
3044
allow(Facter::Core::Execution).to receive(:execute).with('php -v', any_args) do |_command, options = {}|
3145
on_fail = options.fetch(:on_fail, :raise)
3246
raise Facter::Core::Execution::ExecutionFailure, "Could not execute 'php -v': command not found" if on_fail == :raise

0 commit comments

Comments
 (0)