Skip to content

Commit

Permalink
Merge pull request #555 from wiebe/fix_undefined_method_error_in_facts
Browse files Browse the repository at this point in the history
Fix undefined method error in facts
  • Loading branch information
bastelfreak authored Sep 12, 2020
2 parents 8ce4391 + 99ab8cc commit afc4022
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/facter/pip_version.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# Make pip version available as a fact

def get_pip_version(executable)
if Facter::Util::Resolution.which(executable) # rubocop:disable Style/GuardClause
results = Facter::Util::Resolution.exec("#{executable} --version 2>&1").match(%r{^pip (\d+\.\d+\.?\d*).*$})
results[1] if results
end
end

Facter.add('pip_version') do
setcode do
if Facter::Util::Resolution.which('pip')
Facter::Util::Resolution.exec('pip --version 2>&1').match(%r{^pip (\d+\.\d+\.?\d*).*$})[1]
end
get_pip_version 'pip'
end
end

Facter.add('pip2_version') do
setcode do
get_pip_version 'pip2'
end
end

Facter.add('pip3_version') do
setcode do
get_pip_version 'pip3'
end
end
3 changes: 2 additions & 1 deletion lib/facter/virtualenv_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Facter.add('virtualenv_version') do
setcode do
if Facter::Util::Resolution.which('virtualenv')
Facter::Util::Resolution.exec('virtualenv --version 2>&1').match(%r{(\d+\.\d+\.?\d*).*$})[1]
results = Facter::Util::Resolution.exec('virtualenv --version 2>&1').match(%r{(\d+\.\d+\.?\d*).*$})
results[1] if results
end
end
end
50 changes: 50 additions & 0 deletions spec/unit/facter/pip_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
EOS
end

let(:pip2_version_output) do
<<-EOS
pip 9.0.1 from /usr/lib/python2.7/dist-packages/pip (python 2.7)
EOS
end

let(:pip3_version_output) do
<<-EOS
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
EOS
end

describe 'pip_version' do
context 'returns pip version when pip present' do
it do
Expand All @@ -27,4 +39,42 @@
end
end
end

describe 'pip2_version' do
context 'returns pip2 version when pip2 present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip2').returns(true)
Facter::Util::Resolution.expects(:exec).with('pip2 --version 2>&1').returns(pip2_version_output)
expect(Facter.value(:pip2_version)).to eq('9.0.1')
end
end

context 'returns nil when pip2 not present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip2').returns(false)
expect(Facter.value(:pip2_version)).to eq(nil)
end
end
end

describe 'pip3_version' do
context 'returns pip3 version when pip3 present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip3').returns(true)
Facter::Util::Resolution.expects(:exec).with('pip3 --version 2>&1').returns(pip3_version_output)
expect(Facter.value(:pip3_version)).to eq('18.1')
end
end

context 'returns nil when pip3 not present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip3').returns(false)
expect(Facter.value(:pip3_version)).to eq(nil)
end
end
end
end

0 comments on commit afc4022

Please sign in to comment.