From 2f111dfaf7c0ab71cb9aee4adb52e77caa28dfbe Mon Sep 17 00:00:00 2001 From: Brian Witt Date: Sun, 4 Jan 2026 10:55:45 -0800 Subject: [PATCH] respect no_color --- lib/beaker/options/presets.rb | 15 ++++++++- spec/beaker/options/presets_spec.rb | 50 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/beaker/options/presets.rb b/lib/beaker/options/presets.rb index fd1d17813..ad7521856 100644 --- a/lib/beaker/options/presets.rb +++ b/lib/beaker/options/presets.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/beaker/options/presets_spec.rb b/spec/beaker/options/presets_spec.rb index 643c38524..f2757cfcb 100644 --- a/spec/beaker/options/presets_spec.rb +++ b/spec/beaker/options/presets_spec.rb @@ -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