Skip to content

Commit 2d7829d

Browse files
committed
Add facts for python2_version and python3_version with tests
1 parent a9d6279 commit 2d7829d

2 files changed

Lines changed: 94 additions & 6 deletions

File tree

lib/facter/python_version.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
# Make python versions available as facts
22

3+
def get_python_version(executable)
4+
if Facter::Util::Resolution.which(executable)
5+
Facter::Util::Resolution.exec("#{executable} -V 2>&1").match(/^.*(\d+\.\d+\.\d+)$/)[1]
6+
end
7+
end
8+
39
Facter.add("python_version") do
410
setcode do
5-
if Facter::Util::Resolution.which('python')
6-
Facter::Util::Resolution.exec('python -V 2>&1').match(/^.*(\d+\.\d+\.\d+)$/)[1]
11+
get_python_version 'python'
12+
end
13+
end
14+
15+
Facter.add("python2_version") do
16+
setcode do
17+
python2_version = get_python_version 'python2'
18+
if python2_version.nil?
19+
default_version = get_python_version 'python'
20+
if !default_version.nil? and default_version.start_with?('2')
21+
python2_version = default_version
22+
end
723
end
24+
python2_version
25+
end
26+
end
27+
28+
Facter.add("python3_version") do
29+
setcode do
30+
get_python_version 'python3'
831
end
932
end

spec/unit/facter/python_version_spec.rb

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@
55
Facter.clear
66
}
77

8-
let(:python_version_output) { <<-EOS
8+
let(:python2_version_output) { <<-EOS
99
Python 2.7.9
10+
EOS
11+
}
12+
let(:python3_version_output) { <<-EOS
13+
Python 3.3.0
1014
EOS
1115
}
1216

1317
describe "python_version" do
14-
context 'returns python version when python present' do
18+
context 'returns Python version when `python` present' do
1519
it do
1620
Facter::Util::Resolution.stubs(:exec)
1721
Facter::Util::Resolution.expects(:which).with("python").returns(true)
18-
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python_version_output)
22+
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python2_version_output)
1923
Facter.value(:python_version).should == "2.7.9"
2024
end
2125
end
2226

23-
context 'returns nil when python not present' do
27+
context 'returns nil when `python` not present' do
2428
it do
2529
Facter::Util::Resolution.stubs(:exec)
2630
Facter::Util::Resolution.expects(:which).with("python").returns(false)
@@ -29,4 +33,65 @@
2933
end
3034

3135
end
36+
37+
describe "python2_version" do
38+
context 'returns Python 2 version when `python2` is present' do
39+
it do
40+
Facter::Util::Resolution.stubs(:exec)
41+
Facter::Util::Resolution.expects(:which).with("python2").returns(true)
42+
Facter::Util::Resolution.expects(:exec).with("python2 -V 2>&1").returns(python2_version_output)
43+
Facter.value(:python2_version).should == '2.7.9'
44+
end
45+
end
46+
47+
context 'returns Python 2 version when `python2` is absent and `python` is Python 2' do
48+
it do
49+
Facter::Util::Resolution.stubs(:exec)
50+
Facter::Util::Resolution.expects(:which).with("python2").returns(false)
51+
Facter::Util::Resolution.expects(:which).with("python").returns(true)
52+
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python2_version_output)
53+
Facter.value(:python2_version).should == '2.7.9'
54+
end
55+
end
56+
57+
context 'returns nil when `python2` is absent and `python` is Python 3' do
58+
it do
59+
Facter::Util::Resolution.stubs(:exec)
60+
Facter::Util::Resolution.expects(:which).with("python2").returns(false)
61+
Facter::Util::Resolution.expects(:which).with("python").returns(true)
62+
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python3_version_output)
63+
Facter.value(:python2_version).should == nil
64+
end
65+
end
66+
67+
context 'returns nil when `python2` and `python` are absent' do
68+
it do
69+
Facter::Util::Resolution.stubs(:exec)
70+
Facter::Util::Resolution.expects(:which).with("python").returns(false)
71+
Facter::Util::Resolution.expects(:which).with("python2").returns(false)
72+
Facter.value(:python2_version).should == nil
73+
end
74+
end
75+
76+
end
77+
78+
describe "python3_version" do
79+
context 'returns Python 3 version when `python3` present' do
80+
it do
81+
Facter::Util::Resolution.stubs(:exec)
82+
Facter::Util::Resolution.expects(:which).with("python3").returns(true)
83+
Facter::Util::Resolution.expects(:exec).with("python3 -V 2>&1").returns(python3_version_output)
84+
Facter.value(:python3_version).should == "3.3.0"
85+
end
86+
end
87+
88+
context 'returns nil when `python3` not present' do
89+
it do
90+
Facter::Util::Resolution.stubs(:exec)
91+
Facter::Util::Resolution.expects(:which).with("python3").returns(false)
92+
Facter.value(:python3_version).should == nil
93+
end
94+
end
95+
96+
end
3297
end

0 commit comments

Comments
 (0)