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

MODULES-3944 Fix git version and other facts on macOS when git not installed #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 11 additions & 10 deletions lib/facter/git_exec_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
# Notes:
# None
Facter.add('git_exec_path') do
case Facter.value(:osfamily)
when 'windows'
null_path = 'nul'
else
null_path = '/dev/null'
end
git_exec_path_cmd = "git --exec-path 2>#{null_path}"
setcode do
Facter::Util::Resolution.exec(git_exec_path_cmd)
if Facter.value(:git_version)
null_path = case Facter.value(:osfamily)
when 'windows'
'nul'
else
'/dev/null'
end
git_exec_path_cmd = "git --exec-path 2>#{null_path}"
setcode do
Facter::Util::Resolution.exec(git_exec_path_cmd)
end
end
end

20 changes: 11 additions & 9 deletions lib/facter/git_html_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
# Notes:
# None
Facter.add('git_html_path') do
case Facter.value(:osfamily)
when 'windows'
null_path = 'nul'
else
null_path = '/dev/null'
end
git_html_path_cmd = "git --html-path 2>#{null_path}"
setcode do
Facter::Util::Resolution.exec(git_html_path_cmd)
if Facter.value(:git_version)
null_path = case Facter.value(:osfamily)
when 'windows'
'nul'
else
'/dev/null'
end
git_html_path_cmd = "git --html-path 2>#{null_path}"
setcode do
Facter::Util::Resolution.exec(git_html_path_cmd)
end
end
end
20 changes: 16 additions & 4 deletions lib/facter/git_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@
# None
Facter.add('git_version') do
setcode do
if Facter::Util::Resolution.which('git')
git_version_cmd = 'git --version 2>&1'
git_version_result = Facter::Util::Resolution.exec(git_version_cmd)
git_version_result.to_s.lines.first.strip.split(/version/)[1].strip
git = Facter::Util::Resolution.which('git')
if git
# On macOS, /usr/bin/git exists by default but is not actually git;
# instead it is a stub to git inside of the active Xcode directory.
# If there isn't one, calling git spawns an unwanted GUI prompt to
# install the Xcode command line tools.
if (git == '/usr/bin/git') && (Facter.value(:kernel) == 'Darwin')
# check if it is really git
Facter::Util::Resolution.exec('/usr/bin/xcode-select -p')
gitmissing = true if $CHILD_STATUS.exitstatus.nonzero?
end
unless gitmissing
git_version_cmd = 'git --version 2>&1'
git_version_result = Facter::Util::Resolution.exec(git_version_cmd)
git_version_result.to_s.lines.first.strip.split(/version/)[1].strip
end
end
end
end
8 changes: 4 additions & 4 deletions manifests/subtree.pp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
cwd => $source_dir,
path => ['/usr/bin', '/bin', '/usr/local/bin'],
}
->
package { [ 'asciidoc', 'xmlto', ]:

-> package { [ 'asciidoc', 'xmlto', ]:
ensure => present,
}
->
exec { 'Install git-subtree':

-> exec { 'Install git-subtree':
command => "make prefix=/usr libexecdir=${::git_exec_path} install",
onlyif => [
"test ! -f ${::git_exec_path}/git-subtree",
Expand Down
23 changes: 15 additions & 8 deletions spec/unit/facter/git_exec_path_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
require "spec_helper"
require 'spec_helper'

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

describe "git_exec_path" do
end

describe 'git_exec_path' do
context 'windows' do
it do
Facter.fact(:osfamily).stubs(:value).returns('windows')
Facter::Util::Resolution.expects(:exec).with("git --exec-path 2>nul").returns('windows_path_change')
Facter.fact(:git_version).stubs(:value).returns('1.7.1')
Facter::Util::Resolution.expects(:exec).with('git --exec-path 2>nul').returns('windows_path_change')
Facter.fact(:git_exec_path).value.should == 'windows_path_change'
end
end

context 'non-windows' do
it do
Facter.fact(:osfamily).stubs(:value).returns('RedHat')
Facter::Util::Resolution.expects(:exec).with("git --exec-path 2>/dev/null").returns('/usr/libexec/git-core')
Facter.fact(:git_version).stubs(:value).returns('1.7.1')
Facter::Util::Resolution.expects(:exec).with('git --exec-path 2>/dev/null').returns('/usr/libexec/git-core')
Facter.fact(:git_exec_path).value.should == '/usr/libexec/git-core'
end
end

context 'no git present' do
it do
Facter.fact(:git_version).stubs(:value).returns(nil)
Facter.fact(:git_exec_path).value.should be_nil
end
end
end
end
end
23 changes: 15 additions & 8 deletions spec/unit/facter/git_html_path_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
require "spec_helper"
require 'spec_helper'

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

describe "git_html_path" do
end

describe 'git_html_path' do
context 'windows' do
it do
Facter.fact(:osfamily).stubs(:value).returns('windows')
Facter::Util::Resolution.expects(:exec).with("git --html-path 2>nul").returns('windows_path_change')
Facter.fact(:git_version).stubs(:value).returns('1.7.1')
Facter::Util::Resolution.expects(:exec).with('git --html-path 2>nul').returns('windows_path_change')
Facter.fact(:git_html_path).value.should == 'windows_path_change'
end
end

context 'non-windows' do
it do
Facter.fact(:osfamily).stubs(:value).returns('RedHat')
Facter::Util::Resolution.expects(:exec).with("git --html-path 2>/dev/null").returns('/usr/share/doc/git-1.7.1')
Facter.fact(:git_version).stubs(:value).returns('1.7.1')
Facter::Util::Resolution.expects(:exec).with('git --html-path 2>/dev/null').returns('/usr/share/doc/git-1.7.1')
Facter.fact(:git_html_path).value.should == '/usr/share/doc/git-1.7.1'
end
end

context 'no git present' do
it do
Facter.fact(:git_version).stubs(:value).returns(nil)
Facter.fact(:git_html_path).value.should be_nil
end
end
end
end
end
18 changes: 9 additions & 9 deletions spec/unit/facter/git_version_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require "spec_helper"
require 'spec_helper'

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

describe "git_version" do
describe 'git_version' do
context 'vanilla git' do
it do
git_version_output = 'git version 2.1.2'
Facter::Util::Resolution.expects(:exec).with("git --version 2>&1").returns(git_version_output)
Facter.value(:git_version).should == "2.1.2"
Facter::Util::Resolution.expects(:exec).with('git --version 2>&1').returns(git_version_output)
Facter.value(:git_version).should == '2.1.2'
end
end

Expand All @@ -20,14 +20,14 @@
git version 2.1.2
hub version 1.12.2
EOS
Facter::Util::Resolution.expects(:exec).with("git --version 2>&1").returns(git_version_output)
Facter.value(:git_version).should == "2.1.2"
Facter::Util::Resolution.expects(:exec).with('git --version 2>&1').returns(git_version_output)
Facter.value(:git_version).should == '2.1.2'
end
end

context 'no git present' do
it do
Facter::Util::Resolution.expects(:which).with("git").returns(false)
Facter::Util::Resolution.expects(:which).with('git').returns(false)
Facter.value(:git_version).should be_nil
end
end
Expand Down