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

Commit

Permalink
Take upstream java fixes and put them here
Browse files Browse the repository at this point in the history
Got some advice from some Puppetlabs people on how to do facts better! :)
  • Loading branch information
petems committed Jan 8, 2015
1 parent abb8949 commit f96eabe
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
20 changes: 20 additions & 0 deletions lib/facter/java_major_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Fact: java_major_version
#
# Purpose: get Java's major version
#
# Resolution:
# Tests for presence of java, returns nil if not present
# returns output of "java -version" and splits on \n + '"'
# eg.
#
# Caveats:
# none
#
# Notes:
# None
Facter.add(:java_major_version) do
setcode do
java_version = Facter.value(:java_version)
java_patch_level = java_version.strip.split('_')[0].split('.')[1] unless java_version.nil?
end
end
6 changes: 1 addition & 5 deletions lib/facter/java_patch_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
Facter.add(:java_patch_level) do
setcode do
java_version = Facter.value(:java_version)
if java_version.nil?
"JAVA_NOT_INSTALLED"
else
java_patch_level = java_version.strip.split('_')[1]
end
java_patch_level = java_version.strip.split('_')[1] unless java_version.nil?
end
end
13 changes: 7 additions & 6 deletions lib/facter/java_version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fact: java_version
#
# Purpose: store java versions in the config DB
# Purpose: get full java version string
#
# Resolution:
# Tests for presence of java, returns nil if not present
Expand All @@ -11,9 +11,10 @@
#
# Notes:
# None
Facter.add(:java_version) do
setcode do
t_java = Facter::Util::Resolution.exec("java -version 2>&1")
java_version = t_java.to_s.lines.first.strip.split(/version/)[1].gsub(/"/, "").strip
if Facter::Util::Resolution.which('java')
Facter.add(:java_version) do
setcode do
Facter::Util::Resolution.exec('java -version 2>&1').lines.first.split(/"/)[1].strip
end
end
end
end
27 changes: 27 additions & 0 deletions spec/unit/facter/java_major_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "spec_helper"

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

describe "java_major_version" do
context 'returns major version when java_version fact present' do
before :each do
Facter.fact(:java_version).stubs(:value).returns('1.7.0_71')
end
it do
Facter.fact(:java_major_version).value.should == "7"
end
end

context 'returns nil when java not present' do
before :each do
Facter.fact(:java_version).stubs(:value).returns(nil)
end
it do
Facter.fact(:java_major_version).value.should be_nil
end
end
end
end
6 changes: 3 additions & 3 deletions spec/unit/facter/java_patch_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
context "if java is installed" do
context 'returns java patch version extracted from java_version fact' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return("1.7.0_71")
Facter.fact(:java_version).stubs(:value).returns('1.7.0_71')
end
it do
Facter.fact(:java_patch_level).value.should == "71"
Expand All @@ -20,10 +20,10 @@
context "if java is installed" do
context 'returns java patch version extracted from java_version fact' do
before :each do
allow(Facter.fact(:java_version)).to receive(:value).and_return(nil)
Facter.fact(:java_version).stubs(:value).returns(nil)
end
it do
Facter.fact(:java_patch_level).value.should == "JAVA_NOT_INSTALLED"
Facter.fact(:java_patch_level).value.should be_nil
end
end
end
Expand Down
15 changes: 6 additions & 9 deletions spec/unit/facter/java_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
describe Facter::Util::Fact do
before {
Facter.clear
allow(Facter::Util::Resolution).to receive(:exec).with(anything()).and_return(nil)
allow(Facter.fact(:kernel)).to receive(:value).and_return("Darwin")
}

describe "java_version" do
Expand All @@ -15,18 +13,17 @@
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(:exec).with("java -version 2>&1").
and_return(java_version_output)
Facter::Util::Resolution.expects(:which).with("java").returns(true)
Facter::Util::Resolution.expects(:exec).with("java -version 2>&1").returns(java_version_output)
Facter.fact(:java_version).value.should == "1.7.0_71"
end
end

context 'returns nil when java present' do
context 'returns nil when java not present' do
it do
java_version_output = "bash: java: command not found"
allow(Facter::Util::Resolution).to receive(:exec).with("java -version 2>&1").
and_return(java_version_output)
Facter.fact(:java_version).value.should be_nil
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with("java").returns(false)
Facter.fact(:java_version).should be_nil
end
end
end
Expand Down

0 comments on commit f96eabe

Please sign in to comment.