Skip to content

Commit ce29405

Browse files
committed
Add pip2 and pip3 version facts and improve handling of missing output
Partly fixes voxpupuli#527. Signed-off-by: Wiebe Verweij <[email protected]>
1 parent ddb4e95 commit ce29405

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

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

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
@@ -29,4 +41,42 @@
2941
end
3042
end
3143
end
44+
45+
describe 'pip2_version' do
46+
context 'returns pip2 version when pip2 present' do
47+
it do
48+
Facter::Util::Resolution.stubs(:exec)
49+
Facter::Util::Resolution.expects(:which).with('pip2').returns(true)
50+
Facter::Util::Resolution.expects(:exec).with('pip2 --version 2>&1').returns(pip2_version_output)
51+
expect(Facter.value(:pip2_version)).to eq('9.0.1')
52+
end
53+
end
54+
55+
context 'returns nil when pip2 not present' do
56+
it do
57+
Facter::Util::Resolution.stubs(:exec)
58+
Facter::Util::Resolution.expects(:which).with('pip2').returns(false)
59+
expect(Facter.value(:pip2_version)).to eq(nil)
60+
end
61+
end
62+
end
63+
64+
describe 'pip3_version' do
65+
context 'returns pip3 version when pip3 present' do
66+
it do
67+
Facter::Util::Resolution.stubs(:exec)
68+
Facter::Util::Resolution.expects(:which).with('pip3').returns(true)
69+
Facter::Util::Resolution.expects(:exec).with('pip3 --version 2>&1').returns(pip3_version_output)
70+
expect(Facter.value(:pip3_version)).to eq('18.1')
71+
end
72+
end
73+
74+
context 'returns nil when pip3 not present' do
75+
it do
76+
Facter::Util::Resolution.stubs(:exec)
77+
Facter::Util::Resolution.expects(:which).with('pip3').returns(false)
78+
expect(Facter.value(:pip3_version)).to eq(nil)
79+
end
80+
end
81+
end
3282
end

0 commit comments

Comments
 (0)