Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/beaker/options/presets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Presets
:test_tag_or => ['BEAKER_TEST_TAG_OR'],
:test_tag_exclude => %w[BEAKER_EXCLUDE_TAG BEAKER_TEST_TAG_EXCLUDE],
:run_in_parallel => ['BEAKER_RUN_IN_PARALLEL'],
:color => 'BEAKER_COLOR',
}

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

# NO_COLOR is handled manually since it has inverted logic (presence means no color)
no_color_val = ENV['NO_COLOR']
beaker_color_val = found_env_vars[:color]

if beaker_color_val && !beaker_color_val.empty?
found_env_vars[:color] = !/^(no|false)$/i.match?(beaker_color_val)
elsif no_color_val && !no_color_val.empty?
found_env_vars[:color] = false
else
found_env_vars.delete(:color)
end

found_env_vars[:pe_version_file_win] = found_env_vars[:pe_version_file]
found_env_vars
end
Expand All @@ -98,7 +111,7 @@ def calculate_env_vars
found = found.merge(format_found_env_vars(collect_env_vars(ENVIRONMENT_SPEC)))
found[:answers] = select_env_by_regex('\\Aq_')

found.delete_if { |_key, value| value.nil? or value.empty? }
found.delete_if { |_key, value| value.nil? or (value.respond_to?(:empty?) && value.empty?) }
found
end

Expand Down
50 changes: 50 additions & 0 deletions spec/beaker/options/presets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,56 @@ module Options
expect(env[:run_in_parallel]).to eq(%w[install configure])
end

describe "color environment variables" do
after do
ENV.delete('NO_COLOR')
ENV.delete('BEAKER_COLOR')
end

it "disables color when NO_COLOR is set to any non-empty value" do
ENV['NO_COLOR'] = '1'
env = presets.env_vars
expect(env[:color]).to be false
end

it "does not set color when NO_COLOR is set to an empty string" do
ENV['NO_COLOR'] = ''
env = presets.env_vars
expect(env).not_to have_key(:color)
end

it "disables color when BEAKER_COLOR is set to 'no'" do
ENV['BEAKER_COLOR'] = 'no'
env = presets.env_vars
expect(env[:color]).to be false
end

it "disables color when BEAKER_COLOR is set to 'false'" do
ENV['BEAKER_COLOR'] = 'false'
env = presets.env_vars
expect(env[:color]).to be false
end

it "enables color when BEAKER_COLOR is set to 'yes'" do
ENV['BEAKER_COLOR'] = 'yes'
env = presets.env_vars
expect(env[:color]).to be true
end

it "enables color when BEAKER_COLOR is set to 'true'" do
ENV['BEAKER_COLOR'] = 'true'
env = presets.env_vars
expect(env[:color]).to be true
end

it "prioritizes BEAKER_COLOR over NO_COLOR" do
ENV['NO_COLOR'] = '1'
ENV['BEAKER_COLOR'] = 'yes'
env = presets.env_vars
expect(env[:color]).to be true
end
end

it "removes all empty/nil entries in env_vars" do
expect(presets.env_vars.has_value?(nil)).to be === false
expect(presets.env_vars.has_value?({})).to be === false
Expand Down
Loading