From bf21c288c55b2bb007c06240ca4d2498b1c6137c Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Mon, 9 Mar 2015 15:51:04 +0000 Subject: [PATCH] Refactors facts about Java version These facts have since been refactored, ported these changes from https://github.com/puppetlabs/puppetlabs-java/ --- lib/facter/java_major_version.rb | 2 +- lib/facter/java_version.rb | 4 +++- spec/unit/facter/java_major_version.rb | 31 ++++++++++++++++++++++++++ spec/unit/facter/java_version_spec.rb | 12 +++++----- 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 spec/unit/facter/java_major_version.rb diff --git a/lib/facter/java_major_version.rb b/lib/facter/java_major_version.rb index b0a0aae..bf34d03 100644 --- a/lib/facter/java_major_version.rb +++ b/lib/facter/java_major_version.rb @@ -17,4 +17,4 @@ java_version = Facter.value(:java_version) java_patch_level = java_version.strip.split('_')[0].split('.')[1] unless java_version.nil? end -end \ No newline at end of file +end diff --git a/lib/facter/java_version.rb b/lib/facter/java_version.rb index a7e26ca..ed00fa0 100644 --- a/lib/facter/java_version.rb +++ b/lib/facter/java_version.rb @@ -12,8 +12,10 @@ # Notes: # None Facter.add(:java_version) do + # This will fail on OS X when Java hasn't been installed yet. + next unless Kernel.system "/usr/libexec/java_home --failfast &>/dev/null" setcode do - if Facter::Util::Resolution.which('java') && Facter::Util::Resolution.exec('/usr/libexec/java_home --failfast &>/dev/null') + if Facter::Util::Resolution.which('java') Facter::Util::Resolution.exec('java -Xmx8m -version 2>&1').lines.first.split(/"/)[1].strip end end diff --git a/spec/unit/facter/java_major_version.rb b/spec/unit/facter/java_major_version.rb new file mode 100644 index 0000000..7855267 --- /dev/null +++ b/spec/unit/facter/java_major_version.rb @@ -0,0 +1,31 @@ +require "spec_helper" + +describe Facter::Util::Fact do + before { + Facter.clear + } + + describe "java_major_version" do + context "if java is not installed" do + context 'returns java major version extracted from java_version fact' do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71") + end + it do + Facter.fact(:java_major_version).value.should == "7" + end + end + end + + context "if java is not installed" do + context 'returns nil' do + before :each do + allow(Facter.fact(:java_version)).to receive(:value).and_return(nil) + end + it do + Facter.fact(:java_major_version).value.should == nil + end + end + end + end +end \ No newline at end of file diff --git a/spec/unit/facter/java_version_spec.rb b/spec/unit/facter/java_version_spec.rb index baf9ace..bd0403b 100644 --- a/spec/unit/facter/java_version_spec.rb +++ b/spec/unit/facter/java_version_spec.rb @@ -13,10 +13,12 @@ Java(TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode) EOS - allow(Facter::Util::Resolution).to receive(:which).with("java"). - and_return(true) - allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1"). - and_return(java_version_output) + allow(Facter::Util::Resolution).to receive(:which). + with("java").and_return(true) + expect(Kernel).to receive(:system). + with("/usr/libexec/java_home --failfast &>/dev/null").and_return(true) + allow(Facter::Util::Resolution).to receive(:exec). + with("java -Xmx8m -version 2>&1").and_return(java_version_output) Facter.fact(:java_version).value.should == "1.7.0_71" end end @@ -25,7 +27,7 @@ it do allow(Facter::Util::Resolution).to receive(:which).with("java"). and_return(false) - Facter.fact(:java_version).should be_nil + Facter.fact(:java_version).value.should be_nil end end end