diff --git a/lib/facter/python_release.rb b/lib/facter/python_release.rb new file mode 100644 index 00000000..348549c1 --- /dev/null +++ b/lib/facter/python_release.rb @@ -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 diff --git a/spec/unit/facter/python_release_spec.rb b/spec/unit/facter/python_release_spec.rb new file mode 100644 index 00000000..dc6895a4 --- /dev/null +++ b/spec/unit/facter/python_release_spec.rb @@ -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