Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/facter/python_release.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Make python release available as facts

def get_python_release(executable)
if Facter::Util::Resolution.which(executable)
results = Facter::Util::Resolution.exec("#{executable} -V 2>&1").match(/^.*(\d+\.\d+)\.\d+\+?$/)
if results
results[1]
end
end
end

Facter.add("python_release") do
setcode do
get_python_release 'python'
end
end

Facter.add("python2_release") do
setcode do
default_release = get_python_release 'python'
if default_release.nil? or !default_release.start_with?('2')
get_python_release 'python2'
else
default_release
end
end
end

Facter.add("python3_release") do
setcode do
get_python_release 'python3'
end
end
98 changes: 98 additions & 0 deletions spec/unit/facter/python_release_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require "spec_helper"

describe Facter::Util::Fact do
before {
Facter.clear
}

let(:python2_version_output) { <<-EOS
Python 2.7.9
EOS
}
let(:python3_version_output) { <<-EOS
Python 3.3.0
EOS
}

describe "python_release" do
context 'returns Python release when `python` present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("python").returns(true)
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python2_version_output)
expect(Facter.value(:python_release)).to eq("2.7")
end
end

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

end

describe "python2_release" do
context 'returns Python 2 release when `python` is present and Python 2' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("python").returns(true)
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python2_version_output)
expect(Facter.value(:python2_release)).to eq('2.7')
end
end

context 'returns Python 2 release when `python` is Python 3 and `python2` is present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("python").returns(true)
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python3_version_output)
Facter::Util::Resolution.expects(:which).with("python2").returns(true)
Facter::Util::Resolution.expects(:exec).with("python2 -V 2>&1").returns(python2_version_output)
expect(Facter.value(:python2_release)).to eq('2.7')
end
end

context 'returns nil when `python` is Python 3 and `python2` is absent' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("python").returns(true)
Facter::Util::Resolution.expects(:exec).with("python -V 2>&1").returns(python3_version_output)
Facter::Util::Resolution.expects(:which).with("python2").returns(false)
expect(Facter.value(:python2_release)).to eq(nil)
end
end

context 'returns nil when `python2` and `python` are absent' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("python").returns(false)
Facter::Util::Resolution.expects(:which).with("python2").returns(false)
expect(Facter.value(:python2_release)).to eq(nil)
end
end

end

describe "python3_release" do
context 'returns Python 3 release when `python3` present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("python3").returns(true)
Facter::Util::Resolution.expects(:exec).with("python3 -V 2>&1").returns(python3_version_output)
expect(Facter.value(:python3_release)).to eq("3.3")
end
end

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

end
end