Skip to content

Commit 6c723f4

Browse files
authored
Merge pull request #1980 from bwitt/no-color
Respect no_color
2 parents fe971ea + 2f111df commit 6c723f4

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

lib/beaker/options/presets.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Presets
3737
:test_tag_or => ['BEAKER_TEST_TAG_OR'],
3838
:test_tag_exclude => %w[BEAKER_EXCLUDE_TAG BEAKER_TEST_TAG_EXCLUDE],
3939
:run_in_parallel => ['BEAKER_RUN_IN_PARALLEL'],
40+
:color => 'BEAKER_COLOR',
4041
}
4142

4243
# Select all environment variables whose name matches provided regex
@@ -85,6 +86,18 @@ def format_found_env_vars(found_env_vars)
8586
end
8687
found_env_vars[:run_in_parallel] = found_env_vars[:run_in_parallel].split(',') if found_env_vars[:run_in_parallel]
8788

89+
# NO_COLOR is handled manually since it has inverted logic (presence means no color)
90+
no_color_val = ENV['NO_COLOR']
91+
beaker_color_val = found_env_vars[:color]
92+
93+
if beaker_color_val && !beaker_color_val.empty?
94+
found_env_vars[:color] = !/^(no|false)$/i.match?(beaker_color_val)
95+
elsif no_color_val && !no_color_val.empty?
96+
found_env_vars[:color] = false
97+
else
98+
found_env_vars.delete(:color)
99+
end
100+
88101
found_env_vars[:pe_version_file_win] = found_env_vars[:pe_version_file]
89102
found_env_vars
90103
end
@@ -98,7 +111,7 @@ def calculate_env_vars
98111
found = found.merge(format_found_env_vars(collect_env_vars(ENVIRONMENT_SPEC)))
99112
found[:answers] = select_env_by_regex('\\Aq_')
100113

101-
found.delete_if { |_key, value| value.nil? or value.empty? }
114+
found.delete_if { |_key, value| value.nil? or (value.respond_to?(:empty?) && value.empty?) }
102115
found
103116
end
104117

spec/beaker/options/presets_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,56 @@ module Options
2323
expect(env[:run_in_parallel]).to eq(%w[install configure])
2424
end
2525

26+
describe "color environment variables" do
27+
after do
28+
ENV.delete('NO_COLOR')
29+
ENV.delete('BEAKER_COLOR')
30+
end
31+
32+
it "disables color when NO_COLOR is set to any non-empty value" do
33+
ENV['NO_COLOR'] = '1'
34+
env = presets.env_vars
35+
expect(env[:color]).to be false
36+
end
37+
38+
it "does not set color when NO_COLOR is set to an empty string" do
39+
ENV['NO_COLOR'] = ''
40+
env = presets.env_vars
41+
expect(env).not_to have_key(:color)
42+
end
43+
44+
it "disables color when BEAKER_COLOR is set to 'no'" do
45+
ENV['BEAKER_COLOR'] = 'no'
46+
env = presets.env_vars
47+
expect(env[:color]).to be false
48+
end
49+
50+
it "disables color when BEAKER_COLOR is set to 'false'" do
51+
ENV['BEAKER_COLOR'] = 'false'
52+
env = presets.env_vars
53+
expect(env[:color]).to be false
54+
end
55+
56+
it "enables color when BEAKER_COLOR is set to 'yes'" do
57+
ENV['BEAKER_COLOR'] = 'yes'
58+
env = presets.env_vars
59+
expect(env[:color]).to be true
60+
end
61+
62+
it "enables color when BEAKER_COLOR is set to 'true'" do
63+
ENV['BEAKER_COLOR'] = 'true'
64+
env = presets.env_vars
65+
expect(env[:color]).to be true
66+
end
67+
68+
it "prioritizes BEAKER_COLOR over NO_COLOR" do
69+
ENV['NO_COLOR'] = '1'
70+
ENV['BEAKER_COLOR'] = 'yes'
71+
env = presets.env_vars
72+
expect(env[:color]).to be true
73+
end
74+
end
75+
2676
it "removes all empty/nil entries in env_vars" do
2777
expect(presets.env_vars.has_value?(nil)).to be === false
2878
expect(presets.env_vars.has_value?({})).to be === false

0 commit comments

Comments
 (0)