2018-02-04 22:09:35 +05:30
|
|
|
require_relative "../cli_parser"
|
|
|
|
|
|
|
|
describe Homebrew::CLI::Parser do
|
|
|
|
describe "test switch options" do
|
2018-03-29 03:20:14 +05:30
|
|
|
before do
|
|
|
|
allow(ENV).to receive(:[]).with("HOMEBREW_PRY").and_return("1")
|
|
|
|
allow(ENV).to receive(:[]).with("HOMEBREW_VERBOSE")
|
|
|
|
end
|
|
|
|
|
2018-02-04 22:09:35 +05:30
|
|
|
subject(:parser) {
|
|
|
|
described_class.new do
|
2018-03-29 03:20:14 +05:30
|
|
|
switch :verbose, description: "Flag for verbosity"
|
|
|
|
switch "--more-verbose", description: "Flag for higher verbosity"
|
2018-03-25 11:04:18 +05:30
|
|
|
switch "--pry", env: :pry
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
it "parses short option" do
|
2018-04-01 16:47:30 +05:30
|
|
|
parser.parse(["-v"])
|
|
|
|
expect(Homebrew.args).to be_verbose
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "parses a single valid option" do
|
2018-04-01 16:47:30 +05:30
|
|
|
parser.parse(["--verbose"])
|
|
|
|
expect(Homebrew.args).to be_verbose
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "parses a valid option along with few unnamed args" do
|
|
|
|
args = %w[--verbose unnamed args]
|
|
|
|
parser.parse(args)
|
2018-04-01 16:47:30 +05:30
|
|
|
expect(Homebrew.args).to be_verbose
|
|
|
|
expect(args).to eq %w[--verbose unnamed args]
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "parses a single option and checks other options to be nil" do
|
|
|
|
args = parser.parse(["--verbose"])
|
2018-04-01 16:47:30 +05:30
|
|
|
expect(Homebrew.args).to be_verbose
|
2018-02-04 22:09:35 +05:30
|
|
|
expect(args.more_verbose?).to be nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an exception when an invalid option is passed" do
|
|
|
|
expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/)
|
|
|
|
end
|
2018-03-25 11:04:18 +05:30
|
|
|
|
|
|
|
it "maps environment var to an option" do
|
|
|
|
args = parser.parse([])
|
|
|
|
expect(args.pry?).to be true
|
|
|
|
end
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe "test long flag options" do
|
|
|
|
subject(:parser) {
|
|
|
|
described_class.new do
|
2018-04-14 19:04:24 +05:30
|
|
|
flag "--filename=", description: "Name of the file"
|
|
|
|
comma_array "--files", description: "Comma separated filenames"
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|
|
|
|
}
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2018-02-04 22:09:35 +05:30
|
|
|
it "parses a long flag option with its argument" do
|
|
|
|
args = parser.parse(["--filename=random.txt"])
|
|
|
|
expect(args.filename).to eq "random.txt"
|
|
|
|
end
|
|
|
|
|
2018-04-14 19:04:24 +05:30
|
|
|
it "raises an exception when a flag's required value is not passed" do
|
2018-02-04 22:09:35 +05:30
|
|
|
expect { parser.parse(["--filename"]) }.to raise_error(OptionParser::MissingArgument, /--filename/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses a comma array flag option" do
|
|
|
|
args = parser.parse(["--files=random1.txt,random2.txt"])
|
|
|
|
expect(args.files).to eq %w[random1.txt random2.txt]
|
|
|
|
end
|
|
|
|
end
|
2018-04-01 22:01:06 +05:30
|
|
|
|
|
|
|
describe "test constraints" do
|
|
|
|
subject(:parser) {
|
|
|
|
described_class.new do
|
2018-04-14 16:17:14 +05:30
|
|
|
flag "--flag1"
|
|
|
|
flag "--flag3"
|
|
|
|
flag "--flag2", required_for: "--flag1"
|
|
|
|
flag "--flag4", depends_on: "--flag3"
|
|
|
|
|
|
|
|
conflicts "--flag1", "--flag3"
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
it "raises exception on depends mandatory constraint violation" do
|
2018-04-14 16:17:14 +05:30
|
|
|
expect { parser.parse(["--flag1"]) }.to raise_error(Homebrew::CLI::OptionConstraintError)
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "raises exception on depends constraint violation" do
|
2018-04-14 16:17:14 +05:30
|
|
|
expect { parser.parse(["--flag2"]) }.to raise_error(Homebrew::CLI::OptionConstraintError)
|
2018-04-01 22:01:06 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "raises exception for conflict violation" do
|
|
|
|
expect { parser.parse(["--flag1", "--flag3"]) }.to raise_error(Homebrew::CLI::OptionConflictError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises no exception" do
|
|
|
|
args = parser.parse(["--flag1", "--flag2"])
|
|
|
|
expect(args.flag1).to be true
|
|
|
|
expect(args.flag2).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises no exception for optional dependency" do
|
|
|
|
args = parser.parse(["--flag3"])
|
|
|
|
expect(args.flag3).to be true
|
|
|
|
end
|
|
|
|
end
|
2018-04-14 16:17:14 +05:30
|
|
|
|
|
|
|
describe "test invalid constraints" do
|
|
|
|
subject(:parser) {
|
|
|
|
described_class.new do
|
|
|
|
flag "--flag1"
|
|
|
|
flag "--flag2", depends_on: "--flag1"
|
|
|
|
conflicts "--flag1", "--flag2"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
it "raises exception due to invalid constraints" do
|
|
|
|
expect { parser.parse([]) }.to raise_error(Homebrew::CLI::InvalidConstraintError)
|
|
|
|
end
|
|
|
|
end
|
2018-02-04 22:09:35 +05:30
|
|
|
end
|