Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions lib/facter/phpversion.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

Facter.add(:phpversion) do
confine { Facter::Core::Execution.which('php') }

setcode do
output = Facter::Core::Execution.execute('php -v')

Expand Down
39 changes: 39 additions & 0 deletions spec/unit/facter/phpversion_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'phpversion fact' do
subject(:fact) { Facter.fact(:phpversion).value }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually executing facter? I don't think this is right. Same for the load in line 10. Please compare this to other tests, like https://github.com/voxpupuli/puppet-collectd/blob/master/spec/unit/collectd_real_version_spec.rb


before do
Facter.clear
load File.expand_path('../../../lib/facter/phpversion.rb', __dir__)
end

after { Facter.clear }

context 'when php is installed' do
before do
allow(Facter::Core::Execution).to receive(:which).with('php').and_return('/usr/bin/php')
allow(Facter::Core::Execution).to receive(:execute).with('php -v').and_return(
"PHP 8.2.7 (cli) (built: Jun 8 2023 20:03:25) (NTS)\n" \
"Copyright (c) The PHP Group\n" \
"Zend Engine v4.2.7, Copyright (c) Zend Technologies\n",
)
end

it { is_expected.to eq '8.2.7' }
end

context 'when php is not in the path' do
before do
allow(Facter::Core::Execution).to receive(:which).with('php').and_return(nil)
allow(Facter::Core::Execution).to receive(:execute)
end

it 'resolves to nil without running php' do
expect(fact).to be_nil
expect(Facter::Core::Execution).not_to have_received(:execute)
end
end
end
Loading