Skip to content

Commit afc4022

Browse files
authored
Merge pull request #555 from wiebe/fix_undefined_method_error_in_facts
Fix undefined method error in facts
2 parents 8ce4391 + 99ab8cc commit afc4022

3 files changed

Lines changed: 72 additions & 4 deletions

File tree

lib/facter/pip_version.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
# Make pip version available as a fact
22

3+
def get_pip_version(executable)
4+
if Facter::Util::Resolution.which(executable) # rubocop:disable Style/GuardClause
5+
results = Facter::Util::Resolution.exec("#{executable} --version 2>&1").match(%r{^pip (\d+\.\d+\.?\d*).*$})
6+
results[1] if results
7+
end
8+
end
9+
310
Facter.add('pip_version') do
411
setcode do
5-
if Facter::Util::Resolution.which('pip')
6-
Facter::Util::Resolution.exec('pip --version 2>&1').match(%r{^pip (\d+\.\d+\.?\d*).*$})[1]
7-
end
12+
get_pip_version 'pip'
13+
end
14+
end
15+
16+
Facter.add('pip2_version') do
17+
setcode do
18+
get_pip_version 'pip2'
19+
end
20+
end
21+
22+
Facter.add('pip3_version') do
23+
setcode do
24+
get_pip_version 'pip3'
825
end
926
end

lib/facter/virtualenv_version.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Facter.add('virtualenv_version') do
44
setcode do
55
if Facter::Util::Resolution.which('virtualenv')
6-
Facter::Util::Resolution.exec('virtualenv --version 2>&1').match(%r{(\d+\.\d+\.?\d*).*$})[1]
6+
results = Facter::Util::Resolution.exec('virtualenv --version 2>&1').match(%r{(\d+\.\d+\.?\d*).*$})
7+
results[1] if results
78
end
89
end
910
end

spec/unit/facter/pip_version_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
EOS
1212
end
1313

14+
let(:pip2_version_output) do
15+
<<-EOS
16+
pip 9.0.1 from /usr/lib/python2.7/dist-packages/pip (python 2.7)
17+
EOS
18+
end
19+
20+
let(:pip3_version_output) do
21+
<<-EOS
22+
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
23+
EOS
24+
end
25+
1426
describe 'pip_version' do
1527
context 'returns pip version when pip present' do
1628
it do
@@ -27,4 +39,42 @@
2739
end
2840
end
2941
end
42+
43+
describe 'pip2_version' do
44+
context 'returns pip2 version when pip2 present' do
45+
it do
46+
Facter::Util::Resolution.stubs(:exec)
47+
Facter::Util::Resolution.expects(:which).with('pip2').returns(true)
48+
Facter::Util::Resolution.expects(:exec).with('pip2 --version 2>&1').returns(pip2_version_output)
49+
expect(Facter.value(:pip2_version)).to eq('9.0.1')
50+
end
51+
end
52+
53+
context 'returns nil when pip2 not present' do
54+
it do
55+
Facter::Util::Resolution.stubs(:exec)
56+
Facter::Util::Resolution.expects(:which).with('pip2').returns(false)
57+
expect(Facter.value(:pip2_version)).to eq(nil)
58+
end
59+
end
60+
end
61+
62+
describe 'pip3_version' do
63+
context 'returns pip3 version when pip3 present' do
64+
it do
65+
Facter::Util::Resolution.stubs(:exec)
66+
Facter::Util::Resolution.expects(:which).with('pip3').returns(true)
67+
Facter::Util::Resolution.expects(:exec).with('pip3 --version 2>&1').returns(pip3_version_output)
68+
expect(Facter.value(:pip3_version)).to eq('18.1')
69+
end
70+
end
71+
72+
context 'returns nil when pip3 not present' do
73+
it do
74+
Facter::Util::Resolution.stubs(:exec)
75+
Facter::Util::Resolution.expects(:which).with('pip3').returns(false)
76+
expect(Facter.value(:pip3_version)).to eq(nil)
77+
end
78+
end
79+
end
3080
end

0 commit comments

Comments
 (0)