Merge pull request #4207 from GauthamGoli/switch-constraints

cli_parser: Add depends_on, required_for and conflicts for switch options
This commit is contained in:
Gautham Goli 2018-05-25 14:28:00 +05:30 committed by GitHub
commit 67a1235487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 20 deletions

View File

@ -19,7 +19,7 @@ module Homebrew
instance_eval(&block) instance_eval(&block)
end end
def switch(*names, description: nil, env: nil) def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil)
description = option_to_description(*names) if description.nil? description = option_to_description(*names) if description.nil?
global_switch = names.first.is_a?(Symbol) global_switch = names.first.is_a?(Symbol)
names, env = common_switch(*names) if global_switch names, env = common_switch(*names) if global_switch
@ -28,6 +28,10 @@ module Homebrew
end end
enable_switch(*names, global_switch) if !env.nil? && enable_switch(*names, global_switch) if !env.nil? &&
!ENV["HOMEBREW_#{env.to_s.upcase}"].nil? !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
names.each do |name|
set_constraints(name, required_for: required_for, depends_on: depends_on)
end
end end
def comma_array(name, description: nil) def comma_array(name, description: nil)

View File

@ -71,48 +71,48 @@ describe Homebrew::CLI::Parser do
end end
end end
describe "test constraints" do describe "test constraints for flag options" do
subject(:parser) { subject(:parser) {
described_class.new do described_class.new do
flag "--flag1" flag "--flag1="
flag "--flag3" flag "--flag3="
flag "--flag2", required_for: "--flag1" flag "--flag2=", required_for: "--flag1="
flag "--flag4", depends_on: "--flag3" flag "--flag4=", depends_on: "--flag3="
conflicts "--flag1", "--flag3" conflicts "--flag1=", "--flag3="
end end
} }
it "raises exception on depends mandatory constraint violation" do it "raises exception on required_for constraint violation" do
expect { parser.parse(["--flag1"]) }.to raise_error(Homebrew::CLI::OptionConstraintError) expect { parser.parse(["--flag1=flag1"]) }.to raise_error(Homebrew::CLI::OptionConstraintError)
end end
it "raises exception on depends constraint violation" do it "raises exception on depends_on constraint violation" do
expect { parser.parse(["--flag2"]) }.to raise_error(Homebrew::CLI::OptionConstraintError) expect { parser.parse(["--flag2=flag2"]) }.to raise_error(Homebrew::CLI::OptionConstraintError)
end end
it "raises exception for conflict violation" do it "raises exception for conflict violation" do
expect { parser.parse(["--flag1", "--flag3"]) }.to raise_error(Homebrew::CLI::OptionConflictError) expect { parser.parse(["--flag1=flag1", "--flag3=flag3"]) }.to raise_error(Homebrew::CLI::OptionConflictError)
end end
it "raises no exception" do it "raises no exception" do
args = parser.parse(["--flag1", "--flag2"]) args = parser.parse(["--flag1=flag1", "--flag2=flag2"])
expect(args.flag1).to be true expect(args.flag1).to eq "flag1"
expect(args.flag2).to be true expect(args.flag2).to eq "flag2"
end end
it "raises no exception for optional dependency" do it "raises no exception for optional dependency" do
args = parser.parse(["--flag3"]) args = parser.parse(["--flag3=flag3"])
expect(args.flag3).to be true expect(args.flag3).to eq "flag3"
end end
end end
describe "test invalid constraints" do describe "test invalid constraints" do
subject(:parser) { subject(:parser) {
described_class.new do described_class.new do
flag "--flag1" flag "--flag1="
flag "--flag2", depends_on: "--flag1" flag "--flag2=", depends_on: "--flag1="
conflicts "--flag1", "--flag2" conflicts "--flag1=", "--flag2="
end end
} }
@ -120,4 +120,40 @@ describe Homebrew::CLI::Parser do
expect { parser.parse([]) }.to raise_error(Homebrew::CLI::InvalidConstraintError) expect { parser.parse([]) }.to raise_error(Homebrew::CLI::InvalidConstraintError)
end end
end end
describe "test constraints for switch options" do
subject(:parser) {
described_class.new do
switch "-a", "--switch-a"
switch "-b", "--switch-b"
switch "--switch-c", required_for: "--switch-a"
switch "--switch-d", depends_on: "--switch-b"
conflicts "--switch-a", "--switch-b"
end
}
it "raises exception on required_for constraint violation" do
expect { parser.parse(["--switch-a"]) }.to raise_error(Homebrew::CLI::OptionConstraintError)
end
it "raises exception on depends_on constraint violation" do
expect { parser.parse(["--switch-c"]) }.to raise_error(Homebrew::CLI::OptionConstraintError)
end
it "raises exception for conflict violation" do
expect { parser.parse(["-ab"]) }.to raise_error(Homebrew::CLI::OptionConflictError)
end
it "raises no exception" do
args = parser.parse(["--switch-a", "--switch-c"])
expect(args.switch_a?).to be true
expect(args.switch_c?).to be true
end
it "raises no exception for optional dependency" do
args = parser.parse(["--switch-b"])
expect(args.switch_b?).to be true
end
end
end end