Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize CLI arguments over env vars when they conflict #5738

Merged
merged 5 commits into from
Feb 16, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions Library/Homebrew/cli_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize(&block)
Homebrew.args.instance_eval { undef tap }
@constraints = []
@conflicts = []
@switch_sources = {}
@processed_options = []
@desc_line_length = 43
@hide_from_man_page = false
Expand All @@ -51,14 +52,14 @@ def switch(*names, description: nil, env: nil, required_for: nil, depends_on: ni
end
process_option(*names, description)
@parser.on(*names, *wrap_option_desc(description)) do
enable_switch(*names)
enable_switch(*names, from: :args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, much prefer this, thanks again!

end

names.each do |name|
set_constraints(name, required_for: required_for, depends_on: depends_on)
end

enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
enable_switch(*names, from: :env) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
end
alias switch_option switch

Expand Down Expand Up @@ -172,12 +173,19 @@ def hide_from_man_page!

private

def enable_switch(*names)
def enable_switch(*names, from:)
names.each do |name|
@switch_sources[option_to_name(name)] = from
Homebrew.args["#{option_to_name(name)}?"] = true
end
end

def disable_switch(*names)
names.each do |name|
Homebrew.args.delete_field("#{option_to_name(name)}?")
end
end

# These are common/global switches accessible throughout Homebrew
def common_switch(name)
Homebrew::CLI::Parser.global_options.fetch(name, name)
Expand Down Expand Up @@ -225,7 +233,13 @@ def check_conflicts

next if violations.count < 2

raise OptionConflictError, violations.map(&method(:name_to_option))
env_var_options = violations.select do |option|
@switch_sources[option_to_name(option)] == :env_var
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:env_var should be :env?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds right. Has this been fixed yet? I should also probably re-write the tests to catch this bug in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the tests should ideally catch this failure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, will take care of this today

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, looks like the issue with the test is that stubbing env vars isn't working at all. My tests unfortunately were structured in a way that they passed regardless of that. For example, this fails:

allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_A").and_return("1")
expect(ENV["HOMEBREW_SWITCH_A"]).to eq("1") # actually is nil

Any idea how to fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the issue, I had my stubs in the wrong order. Pushing a change now, sorry for the bugs!

end

select_cli_arg = violations.count - env_var_options.count == 1
raise OptionConflictError, violations.map(&method(:name_to_option)) unless select_cli_arg
env_var_options.each(&method(:disable_switch))
end
end

Expand Down
20 changes: 18 additions & 2 deletions Library/Homebrew/test/cli_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@
describe "test constraints for switch options" do
subject(:parser) {
described_class.new do
switch "-a", "--switch-a"
switch "-b", "--switch-b"
switch "-a", "--switch-a", env: "switch_a"
switch "-b", "--switch-b", env: "switch_b"
switch "--switch-c", required_for: "--switch-a"
switch "--switch-d", depends_on: "--switch-b"

Expand Down Expand Up @@ -177,6 +177,22 @@
parser.parse(["--switch-b"])
expect(Homebrew.args.switch_b?).to be true
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(:[])
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(:[]).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)
end
end

describe "test immutability of args" do
Expand Down