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

Commit 6ca618e

Browse files
committed
MODULES-3944 Fix git version and other facts on macOS when git not installed
1 parent 5e86224 commit 6ca618e

File tree

6 files changed

+79
-48
lines changed

6 files changed

+79
-48
lines changed

lib/facter/git_exec_path.rb

+11-10
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
# Notes:
1212
# None
1313
Facter.add('git_exec_path') do
14-
case Facter.value(:osfamily)
15-
when 'windows'
16-
null_path = 'nul'
17-
else
18-
null_path = '/dev/null'
19-
end
20-
git_exec_path_cmd = "git --exec-path 2>#{null_path}"
21-
setcode do
22-
Facter::Util::Resolution.exec(git_exec_path_cmd)
14+
if Facter.value(:git_version)
15+
null_path = case Facter.value(:osfamily)
16+
when 'windows'
17+
'nul'
18+
else
19+
'/dev/null'
20+
end
21+
git_exec_path_cmd = "git --exec-path 2>#{null_path}"
22+
setcode do
23+
Facter::Util::Resolution.exec(git_exec_path_cmd)
24+
end
2325
end
2426
end
25-

lib/facter/git_html_path.rb

+11-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
# Notes:
1212
# None
1313
Facter.add('git_html_path') do
14-
case Facter.value(:osfamily)
15-
when 'windows'
16-
null_path = 'nul'
17-
else
18-
null_path = '/dev/null'
19-
end
20-
git_html_path_cmd = "git --html-path 2>#{null_path}"
21-
setcode do
22-
Facter::Util::Resolution.exec(git_html_path_cmd)
14+
if Facter.value(:git_version)
15+
null_path = case Facter.value(:osfamily)
16+
when 'windows'
17+
'nul'
18+
else
19+
'/dev/null'
20+
end
21+
git_html_path_cmd = "git --html-path 2>#{null_path}"
22+
setcode do
23+
Facter::Util::Resolution.exec(git_html_path_cmd)
24+
end
2325
end
2426
end

lib/facter/git_version.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@
1212
# None
1313
Facter.add('git_version') do
1414
setcode do
15-
if Facter::Util::Resolution.which('git')
16-
git_version_cmd = 'git --version 2>&1'
17-
git_version_result = Facter::Util::Resolution.exec(git_version_cmd)
18-
git_version_result.to_s.lines.first.strip.split(/version/)[1].strip
15+
git = Facter::Util::Resolution.which('git')
16+
if git
17+
# On macOS, /usr/bin/git exists by default but is not actually git;
18+
# instead it is a stub to git inside of the active Xcode directory.
19+
# If there isn't one, calling git spawns an unwanted GUI prompt to
20+
# install the Xcode command line tools.
21+
if (git == '/usr/bin/git') && (Facter.value(:kernel) == 'Darwin')
22+
# check if it is really git
23+
Facter::Util::Resolution.exec('/usr/bin/xcode-select -p')
24+
gitmissing = true if $CHILD_STATUS.exitstatus.nonzero?
25+
end
26+
unless gitmissing
27+
git_version_cmd = 'git --version 2>&1'
28+
git_version_result = Facter::Util::Resolution.exec(git_version_cmd)
29+
git_version_result.to_s.lines.first.strip.split(/version/)[1].strip
30+
end
1931
end
2032
end
2133
end
+15-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
require "spec_helper"
1+
require 'spec_helper'
22

33
describe Facter::Util::Fact do
4-
before {
4+
before do
55
Facter.clear
6-
}
7-
8-
describe "git_exec_path" do
6+
end
97

8+
describe 'git_exec_path' do
109
context 'windows' do
1110
it do
1211
Facter.fact(:osfamily).stubs(:value).returns('windows')
13-
Facter::Util::Resolution.expects(:exec).with("git --exec-path 2>nul").returns('windows_path_change')
12+
Facter.fact(:git_version).stubs(:value).returns('1.7.1')
13+
Facter::Util::Resolution.expects(:exec).with('git --exec-path 2>nul').returns('windows_path_change')
1414
Facter.fact(:git_exec_path).value.should == 'windows_path_change'
1515
end
1616
end
1717

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

27+
context 'no git present' do
28+
it do
29+
Facter.fact(:git_version).stubs(:value).returns(nil)
30+
Facter.fact(:git_exec_path).value.should be_nil
31+
end
32+
end
2633
end
27-
end
34+
end
+15-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
require "spec_helper"
1+
require 'spec_helper'
22

33
describe Facter::Util::Fact do
4-
before {
4+
before do
55
Facter.clear
6-
}
7-
8-
describe "git_html_path" do
6+
end
97

8+
describe 'git_html_path' do
109
context 'windows' do
1110
it do
1211
Facter.fact(:osfamily).stubs(:value).returns('windows')
13-
Facter::Util::Resolution.expects(:exec).with("git --html-path 2>nul").returns('windows_path_change')
12+
Facter.fact(:git_version).stubs(:value).returns('1.7.1')
13+
Facter::Util::Resolution.expects(:exec).with('git --html-path 2>nul').returns('windows_path_change')
1414
Facter.fact(:git_html_path).value.should == 'windows_path_change'
1515
end
1616
end
1717

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

27+
context 'no git present' do
28+
it do
29+
Facter.fact(:git_version).stubs(:value).returns(nil)
30+
Facter.fact(:git_html_path).value.should be_nil
31+
end
32+
end
2633
end
27-
end
34+
end

spec/unit/facter/git_version_spec.rb

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
require "spec_helper"
1+
require 'spec_helper'
22

33
describe Facter::Util::Fact do
4-
before {
4+
before do
55
Facter.clear
6-
}
6+
end
77

8-
describe "git_version" do
8+
describe 'git_version' do
99
context 'vanilla git' do
1010
it do
1111
git_version_output = 'git version 2.1.2'
12-
Facter::Util::Resolution.expects(:exec).with("git --version 2>&1").returns(git_version_output)
13-
Facter.value(:git_version).should == "2.1.2"
12+
Facter::Util::Resolution.expects(:exec).with('uname -s')
13+
Facter::Util::Resolution.expects(:exec).with('git --version 2>&1').returns(git_version_output)
14+
Facter.value(:git_version).should == '2.1.2'
1415
end
1516
end
1617

@@ -20,14 +21,15 @@
2021
git version 2.1.2
2122
hub version 1.12.2
2223
EOS
23-
Facter::Util::Resolution.expects(:exec).with("git --version 2>&1").returns(git_version_output)
24-
Facter.value(:git_version).should == "2.1.2"
24+
Facter::Util::Resolution.expects(:exec).with('uname -s')
25+
Facter::Util::Resolution.expects(:exec).with('git --version 2>&1').returns(git_version_output)
26+
Facter.value(:git_version).should == '2.1.2'
2527
end
2628
end
2729

2830
context 'no git present' do
2931
it do
30-
Facter::Util::Resolution.expects(:which).with("git").returns(false)
32+
Facter::Util::Resolution.expects(:which).with('git').returns(false)
3133
Facter.value(:git_version).should be_nil
3234
end
3335
end

0 commit comments

Comments
 (0)