Skip to content

Commit

Permalink
Fixed darwin detection to not read content of sw_vers.
Browse files Browse the repository at this point in the history
Use unix_uname_* and `sw_vers -buildVersion` for our data.

Signed-off-by: Ryan Davis <zenspider@chef.io>
  • Loading branch information
zenspider committed Jan 23, 2020
1 parent 827f130 commit 6969bf2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
13 changes: 6 additions & 7 deletions lib/train/platforms/detect/specifications/os.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,18 +373,17 @@ def self.load_bsd

plat.family("darwin").in_family("bsd")
.detect do
# rubocop:disable Layout/ExtraSpacing
# rubocop:disable Layout/SpaceAroundOperators
if unix_uname_s =~ /darwin/i
cmd = unix_file_contents("/usr/bin/sw_vers")

if cmd
@platform[:release] = cmd[/^ProductVersion:\s+(.+)$/, 1]
@platform[:build] = cmd[/^BuildVersion:\s+(.+)$/, 1]
end

@platform[:release] ||= unix_uname_r.lines[0].chomp
@platform[:arch] = unix_uname_m
cmd = @backend.run_command("sw_vers -buildVersion")
@platform[:build] = cmd.stdout.chomp if cmd.exit_status == 0
true
end
# rubocop:enable Layout/ExtraSpacing
# rubocop:enable Layout/SpaceAroundOperators
end

plat.name("mac_os_x").title("macOS X").in_family("darwin")
Expand Down
12 changes: 9 additions & 3 deletions test/unit/platforms/os_detect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ def scan_with_files(uname, files)
mock = Train::Transports::Mock::Connection.new
mock.mock_command("uname -s", uname)
mock.mock_command("uname -r", "test-release")

yield mock if block_given?

files.each do |path, data|
mock.mock_command("test -f #{path}")
mock.mock_command("test -f #{path} && cat #{path}", data)
end

Train::Platforms::Detect.scan(mock)
end

Expand Down Expand Up @@ -101,11 +105,13 @@ def scan_with_windows
files = {
"/usr/bin/sw_vers" => "ProductVersion: 17.0.1\nBuildVersion: alpha.x1",
}
platform = scan_with_files("darwin", files)
platform = scan_with_files("darwin", files) do |mock|
mock.mock_command("sw_vers -buildVersion", "12345\n")
end
_(platform[:name]).must_equal("darwin")
_(platform[:family]).must_equal("darwin")
_(platform[:release]).must_equal("17.0.1")
_(platform[:build]).must_equal("alpha.x1")
_(platform[:release]).must_equal("test-release")
_(platform[:build]).must_equal("12345")
end
end
end
Expand Down

0 comments on commit 6969bf2

Please sign in to comment.