From 33a2f410afa1629fff2c435eaf885120db137368 Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Fri, 22 Aug 2025 01:59:54 +0800 Subject: [PATCH 1/2] cli/parser: convert switch env values to boolean Since switch values are boolean, we should be returning the environment variable's presence instead of its string value. Fixes #20531. --- Library/Homebrew/cli/parser.rb | 3 ++- Library/Homebrew/test/cli/parser_spec.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 7ffe3e0c72..b464755e05 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -231,7 +231,8 @@ module Homebrew end env_value = value_for_env(env) - set_switch(*names, value: env_value, from: :env) unless env_value.nil? + value = env_value&.present? + set_switch(*names, value:, from: :env) unless value.nil? end alias switch_option switch diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index d4d5edb786..c2b23fd81c 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -10,12 +10,15 @@ RSpec.describe Homebrew::CLI::Parser do described_class.new(Cmd) do switch "--more-verbose", description: "Flag for higher verbosity" switch "--pry", env: :pry + switch "--bar", env: :bar switch "--hidden", hidden: true end end before do allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true) + allow(ENV).to receive(:fetch).and_call_original + allow(ENV).to receive(:fetch).with("HOMEBREW_BAR", nil).and_return("1") end context "when using binary options" do @@ -113,6 +116,7 @@ RSpec.describe Homebrew::CLI::Parser do it "maps environment var to an option" do args = parser.parse([]) expect(args.pry?).to be true + expect(args.bar?).to be true end end From 625a6c08ebbeb4408432faddc93f11d95b73ff8f Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Fri, 22 Aug 2025 02:04:55 +0800 Subject: [PATCH 2/2] test/cli/parser: add one more test --- Library/Homebrew/test/cli/parser_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index c2b23fd81c..33497a3685 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -10,6 +10,7 @@ RSpec.describe Homebrew::CLI::Parser do described_class.new(Cmd) do switch "--more-verbose", description: "Flag for higher verbosity" switch "--pry", env: :pry + switch "--foo", env: :foo switch "--bar", env: :bar switch "--hidden", hidden: true end @@ -18,6 +19,7 @@ RSpec.describe Homebrew::CLI::Parser do before do allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true) allow(ENV).to receive(:fetch).and_call_original + allow(ENV).to receive(:fetch).with("HOMEBREW_FOO", nil).and_return("") allow(ENV).to receive(:fetch).with("HOMEBREW_BAR", nil).and_return("1") end @@ -116,6 +118,7 @@ RSpec.describe Homebrew::CLI::Parser do it "maps environment var to an option" do args = parser.parse([]) expect(args.pry?).to be true + expect(args.foo?).to be false expect(args.bar?).to be true end end