Merge pull request #5805 from BenMusch/handle-conflicts

Fix bug in CLI arg/env var prioritization + corresponding tests
This commit is contained in:
Mike McQuaid 2019-02-26 07:55:19 +00:00 committed by GitHub
commit d1f755959a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -234,7 +234,7 @@ module Homebrew
next if violations.count < 2
env_var_options = violations.select do |option|
@switch_sources[option_to_name(option)] == :env_var
@switch_sources[option_to_name(option)] == :env
end
select_cli_arg = violations.count - env_var_options.count == 1

View File

@ -179,19 +179,19 @@ describe Homebrew::CLI::Parser do
end
it "prioritizes cli arguments over env vars when they conflict" do
allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_A").and_return("1")
allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_B").and_return("0")
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_A").and_return("1")
allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_B").and_return(nil)
parser.parse(["--switch-b"])
expect(Homebrew.args.switch_a).to be_falsy
expect(Homebrew.args).to be_switch_b
end
it "raises an exception on constraint violation when both are env vars" do
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_A").and_return("1")
allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_B").and_return("1")
allow(ENV).to receive(:[])
expect { parser.parse(["--switch-a", "--switch-b"]) }.to raise_error(Homebrew::CLI::OptionConflictError)
expect { parser.parse([]) }.to raise_error(Homebrew::CLI::OptionConflictError)
end
end