Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Commit

Permalink
Refactors facts about Java version
Browse files Browse the repository at this point in the history
These facts have since been refactored, ported these changes from https://github.com/puppetlabs/puppetlabs-java/
  • Loading branch information
petems committed May 6, 2015
1 parent 6575546 commit bf21c28
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/facter/java_major_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
4 changes: 3 additions & 1 deletion lib/facter/java_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/unit/facter/java_major_version.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 7 additions & 5 deletions spec/unit/facter/java_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit bf21c28

Please sign in to comment.