Merge pull request #5530 from inspec/nm/detect-no-color

Bugfix for `inspec detect --no-color` to not return colourful output
This commit is contained in:
Clinton Wolfe 2021-05-20 14:52:20 -04:00 committed by GitHub
commit ceeb6c7d3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View file

@ -181,7 +181,7 @@ module Inspec
puts " Patents: chef.io/patents\n\n"
end
def self.format_platform_info(params: {}, indent: 0, color: 39)
def self.format_platform_info(params: {}, indent: 0, color: 39, enable_color: true)
str = ""
params.each do |item, info|
data = info
@ -192,7 +192,7 @@ module Inspec
# Do not output fields of data is missing ('unknown' is fine)
next if data.nil?
data = "\e[1m\e[#{color}m#{data}\e[0m"
data = "\e[1m\e[#{color}m#{data}\e[0m" if enable_color
str << format("#{" " * indent}%-10s %s\n", item.to_s.capitalize + ":", data)
end
str

View file

@ -305,7 +305,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
puts res.to_json
else
ui.headline("Platform Details")
ui.plain Inspec::BaseCLI.format_platform_info(params: res, indent: 0, color: 36)
ui.plain Inspec::BaseCLI.format_platform_info(params: res, indent: 0, color: 36, enable_color: ui.color?)
end
rescue ArgumentError, RuntimeError, Train::UserError => e
$stderr.puts e.message

View file

@ -100,6 +100,15 @@ describe "InSpec UI behavior" do
assert_exit_code 0, run_result
end
end
describe "detect command" do
let(:result) { inspec("detect") }
it "has a colorful output" do
_(result.stdout).must_include("\e[")
assert_exit_code 0, result
end
end
end
describe "with --no-color option" do
@ -130,6 +139,15 @@ describe "InSpec UI behavior" do
assert_exit_code 0, run_result
end
end
describe "detect command" do
let(:result) { inspec("detect --no-color") }
it "has no color in the output" do
_(result.stdout).wont_include("\e[")
assert_exit_code 0, result
end
end
end
describe "exit codes" do