From 0364f7203286f5c599ea72f213ecb7b9bcb8cf3f Mon Sep 17 00:00:00 2001 From: Samuel Keeley Date: Wed, 5 Oct 2016 23:11:40 +0200 Subject: [PATCH] MODULES-3944 Fix git version and other facts on macOS when git not installed --- lib/facter/git_exec_path.rb | 21 +++++++++++---------- lib/facter/git_html_path.rb | 20 +++++++++++--------- lib/facter/git_version.rb | 20 ++++++++++++++++---- spec/unit/facter/git_exec_path_spec.rb | 23 +++++++++++++++-------- spec/unit/facter/git_html_path_spec.rb | 23 +++++++++++++++-------- spec/unit/facter/git_version_spec.rb | 18 +++++++++--------- 6 files changed, 77 insertions(+), 48 deletions(-) diff --git a/lib/facter/git_exec_path.rb b/lib/facter/git_exec_path.rb index 3a4ad8f..fd89759 100644 --- a/lib/facter/git_exec_path.rb +++ b/lib/facter/git_exec_path.rb @@ -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 - diff --git a/lib/facter/git_html_path.rb b/lib/facter/git_html_path.rb index 471fed7..1abc480 100644 --- a/lib/facter/git_html_path.rb +++ b/lib/facter/git_html_path.rb @@ -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 diff --git a/lib/facter/git_version.rb b/lib/facter/git_version.rb index c0ece6a..e5cc9f9 100644 --- a/lib/facter/git_version.rb +++ b/lib/facter/git_version.rb @@ -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 diff --git a/spec/unit/facter/git_exec_path_spec.rb b/spec/unit/facter/git_exec_path_spec.rb index 0394869..c0dac7d 100644 --- a/spec/unit/facter/git_exec_path_spec.rb +++ b/spec/unit/facter/git_exec_path_spec.rb @@ -1,16 +1,16 @@ -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 @@ -18,10 +18,17 @@ 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 \ No newline at end of file +end diff --git a/spec/unit/facter/git_html_path_spec.rb b/spec/unit/facter/git_html_path_spec.rb index 4f332e2..370ca6f 100644 --- a/spec/unit/facter/git_html_path_spec.rb +++ b/spec/unit/facter/git_html_path_spec.rb @@ -1,16 +1,16 @@ -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 @@ -18,10 +18,17 @@ 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 \ No newline at end of file +end diff --git a/spec/unit/facter/git_version_spec.rb b/spec/unit/facter/git_version_spec.rb index e120577..c73d86c 100644 --- a/spec/unit/facter/git_version_spec.rb +++ b/spec/unit/facter/git_version_spec.rb @@ -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 @@ -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