Skip to content

Commit a31ecdb

Browse files
authored
Merge pull request #355 from jcpunk/python-release
Add python release as available facts
2 parents 9a1103d + 046097a commit a31ecdb

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

lib/facter/python_release.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Make python release available as facts
2+
3+
def get_python_release(executable)
4+
if Facter::Util::Resolution.which(executable)
5+
results = Facter::Util::Resolution.exec("#{executable} -V 2>&1").match(/^.*(\d+\.\d+)\.\d+\+?$/)
6+
if results
7+
results[1]
8+
end
9+
end
10+
end
11+
12+
Facter.add("python_release") do
13+
setcode do
14+
get_python_release 'python'
15+
end
16+
end
17+
18+
Facter.add("python2_release") do
19+
setcode do
20+
default_release = get_python_release 'python'
21+
if default_release.nil? or !default_release.start_with?('2')
22+
get_python_release 'python2'
23+
else
24+
default_release
25+
end
26+
end
27+
end
28+
29+
Facter.add("python3_release") do
30+
setcode do
31+
get_python_release 'python3'
32+
end
33+
end
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require "spec_helper"
2+
3+
describe Facter::Util::Fact do
4+
before {
5+
Facter.clear
6+
}
7+
8+
let(:python2_version_output) { <<-EOS
9+
Python 2.7.9
10+
EOS
11+
}
12+
let(:python3_version_output) { <<-EOS
13+
Python 3.3.0
14+
EOS
15+
}
16+
17+
describe "python_release" do
18+
context 'returns Python release when `python` present' do
19+
it do
20+
Facter::Util::Resolution.stubs(:exec)
21+
Facter::Util::Resolution.expects(:which).with("python").returns(true)
22+
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python2_version_output)
23+
expect(Facter.value(:python_release)).to eq("2.7")
24+
end
25+
end
26+
27+
context 'returns nil when `python` not present' do
28+
it do
29+
Facter::Util::Resolution.stubs(:exec)
30+
Facter::Util::Resolution.expects(:which).with("python").returns(false)
31+
expect(Facter.value(:python_release)).to eq(nil)
32+
end
33+
end
34+
35+
end
36+
37+
describe "python2_release" do
38+
context 'returns Python 2 release when `python` is present and Python 2' do
39+
it do
40+
Facter::Util::Resolution.stubs(:exec)
41+
Facter::Util::Resolution.expects(:which).with("python").returns(true)
42+
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python2_version_output)
43+
expect(Facter.value(:python2_release)).to eq('2.7')
44+
end
45+
end
46+
47+
context 'returns Python 2 release when `python` is Python 3 and `python2` is present' do
48+
it do
49+
Facter::Util::Resolution.stubs(:exec)
50+
Facter::Util::Resolution.expects(:which).with("python").returns(true)
51+
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python3_version_output)
52+
Facter::Util::Resolution.expects(:which).with("python2").returns(true)
53+
Facter::Util::Resolution.expects(:exec).with("python2 -V 2>&1").returns(python2_version_output)
54+
expect(Facter.value(:python2_release)).to eq('2.7')
55+
end
56+
end
57+
58+
context 'returns nil when `python` is Python 3 and `python2` is absent' do
59+
it do
60+
Facter::Util::Resolution.stubs(:exec)
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::Util::Resolution.expects(:which).with("python2").returns(false)
64+
expect(Facter.value(:python2_release)).to eq(nil)
65+
end
66+
end
67+
68+
context 'returns nil when `python2` and `python` are absent' do
69+
it do
70+
Facter::Util::Resolution.stubs(:exec)
71+
Facter::Util::Resolution.expects(:which).with("python").returns(false)
72+
Facter::Util::Resolution.expects(:which).with("python2").returns(false)
73+
expect(Facter.value(:python2_release)).to eq(nil)
74+
end
75+
end
76+
77+
end
78+
79+
describe "python3_release" do
80+
context 'returns Python 3 release when `python3` present' do
81+
it do
82+
Facter::Util::Resolution.stubs(:exec)
83+
Facter::Util::Resolution.expects(:which).with("python3").returns(true)
84+
Facter::Util::Resolution.expects(:exec).with("python3 -V 2>&1").returns(python3_version_output)
85+
expect(Facter.value(:python3_release)).to eq("3.3")
86+
end
87+
end
88+
89+
context 'returns nil when `python3` not present' do
90+
it do
91+
Facter::Util::Resolution.stubs(:exec)
92+
Facter::Util::Resolution.expects(:which).with("python3").returns(false)
93+
expect(Facter.value(:python3_release)).to eq(nil)
94+
end
95+
end
96+
97+
end
98+
end

0 commit comments

Comments
 (0)