| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require_relative "../../cli/parser" | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | describe Homebrew::CLI::Parser do | 
					
						
							|  |  |  |   describe "test switch options" do | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |     before do | 
					
						
							| 
									
										
										
										
											2019-01-12 19:17:07 +00:00
										 |  |  |       allow(ENV).to receive(:[]) | 
					
						
							| 
									
										
										
										
											2018-09-20 09:07:56 +01:00
										 |  |  |       allow(ENV).to receive(:[]).with("HOMEBREW_PRY").and_return("1") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse(["--verbose"]) | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |       expect(Homebrew.args).to be_verbose | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       expect(Homebrew.args.more_verbose?).to be nil | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 19:17:07 +00:00
										 |  |  |     it "raises an exception and outputs help text when an invalid option is passed" do | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |       expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/) | 
					
						
							| 
									
										
										
										
											2019-01-12 19:17:07 +00:00
										 |  |  |                                            .and output(/Usage: brew/).to_stderr | 
					
						
							| 
									
										
										
										
											2019-01-12 18:52:07 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |     it "maps environment var to an option" do | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse([]) | 
					
						
							|  |  |  |       expect(Homebrew.args.pry?).to be true | 
					
						
							| 
									
										
										
										
											2018-03-25 11:04:18 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-10-24 17:10:13 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     it ":verbose with custom description" do | 
					
						
							|  |  |  |       _, _, _, desc = parser.processed_options.find { |short, _| short == "-v" } | 
					
						
							|  |  |  |       expect(desc).to eq "Flag for verbosity" | 
					
						
							|  |  |  |     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 | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse(["--filename=random.txt"]) | 
					
						
							|  |  |  |       expect(Homebrew.args.filename).to eq "random.txt" | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse(["--files=random1.txt,random2.txt"]) | 
					
						
							|  |  |  |       expect(Homebrew.args.files).to eq %w[random1.txt random2.txt] | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 21:43:49 +05:30
										 |  |  |   describe "test short flag options" do | 
					
						
							|  |  |  |     subject(:parser) { | 
					
						
							|  |  |  |       described_class.new do | 
					
						
							|  |  |  |         flag "-f", "--filename=", description: "Name of the file" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "parses a short flag option with its argument" do | 
					
						
							|  |  |  |       parser.parse(["--filename=random.txt"]) | 
					
						
							|  |  |  |       expect(Homebrew.args.filename).to eq "random.txt" | 
					
						
							|  |  |  |       expect(Homebrew.args.f).to eq "random.txt" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |   describe "test constraints for flag options" do | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |     subject(:parser) { | 
					
						
							|  |  |  |       described_class.new do | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |         flag      "--flag1=" | 
					
						
							|  |  |  |         flag      "--flag3=" | 
					
						
							|  |  |  |         flag      "--flag2=", required_for: "--flag1=" | 
					
						
							|  |  |  |         flag      "--flag4=", depends_on: "--flag3=" | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |         conflicts "--flag1=", "--flag3=" | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |     it "raises exception on required_for constraint violation" do | 
					
						
							|  |  |  |       expect { parser.parse(["--flag1=flag1"]) }.to raise_error(Homebrew::CLI::OptionConstraintError) | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |     it "raises exception on depends_on constraint violation" do | 
					
						
							|  |  |  |       expect { parser.parse(["--flag2=flag2"]) }.to raise_error(Homebrew::CLI::OptionConstraintError) | 
					
						
							| 
									
										
										
										
											2018-10-06 22:52:47 -04:00
										 |  |  |       expect { parser.parse(["--flag4=flag4"]) }.to raise_error(Homebrew::CLI::OptionConstraintError) | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises exception for conflict violation" do | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |       expect { parser.parse(["--flag1=flag1", "--flag3=flag3"]) }.to raise_error(Homebrew::CLI::OptionConflictError) | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises no exception" do | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse(["--flag1=flag1", "--flag2=flag2"]) | 
					
						
							|  |  |  |       expect(Homebrew.args.flag1).to eq "flag1" | 
					
						
							|  |  |  |       expect(Homebrew.args.flag2).to eq "flag2" | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises no exception for optional dependency" do | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse(["--flag3=flag3"]) | 
					
						
							|  |  |  |       expect(Homebrew.args.flag3).to eq "flag3" | 
					
						
							| 
									
										
										
										
											2018-04-01 22:01:06 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "test invalid constraints" do | 
					
						
							|  |  |  |     subject(:parser) { | 
					
						
							|  |  |  |       described_class.new do | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |         flag      "--flag1=" | 
					
						
							|  |  |  |         flag      "--flag2=", depends_on: "--flag1=" | 
					
						
							|  |  |  |         conflicts "--flag1=", "--flag2=" | 
					
						
							| 
									
										
										
										
											2018-04-14 16:17:14 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises exception due to invalid constraints" do | 
					
						
							|  |  |  |       expect { parser.parse([]) }.to raise_error(Homebrew::CLI::InvalidConstraintError) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "test constraints for switch options" do | 
					
						
							|  |  |  |     subject(:parser) { | 
					
						
							|  |  |  |       described_class.new do | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |         switch      "-a", "--switch-a", env: "switch_a" | 
					
						
							|  |  |  |         switch      "-b", "--switch-b", env: "switch_b" | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2018-10-06 22:52:47 -04:00
										 |  |  |       expect { parser.parse(["--switch-d"]) }.to raise_error(Homebrew::CLI::OptionConstraintError) | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises exception for conflict violation" do | 
					
						
							|  |  |  |       expect { parser.parse(["-ab"]) }.to raise_error(Homebrew::CLI::OptionConflictError) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises no exception" do | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse(["--switch-a", "--switch-c"]) | 
					
						
							|  |  |  |       expect(Homebrew.args.switch_a?).to be true | 
					
						
							|  |  |  |       expect(Homebrew.args.switch_c?).to be true | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises no exception for optional dependency" do | 
					
						
							| 
									
										
										
										
											2018-05-05 18:40:01 +05:30
										 |  |  |       parser.parse(["--switch-b"]) | 
					
						
							|  |  |  |       expect(Homebrew.args.switch_b?).to be true | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it "prioritizes cli arguments over env vars when they conflict" do | 
					
						
							| 
									
										
										
										
											2019-02-15 14:15:34 -05:00
										 |  |  |       allow(ENV).to receive(:[]) | 
					
						
							| 
									
										
										
										
											2019-02-25 14:54:31 -05:00
										 |  |  |       allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_A").and_return("1") | 
					
						
							|  |  |  |       allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_B").and_return(nil) | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |       parser.parse(["--switch-b"]) | 
					
						
							| 
									
										
										
										
											2019-02-16 07:39:48 -05:00
										 |  |  |       expect(Homebrew.args.switch_a).to be_falsy | 
					
						
							|  |  |  |       expect(Homebrew.args).to be_switch_b | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an exception on constraint violation when both are env vars" do | 
					
						
							| 
									
										
										
										
											2019-02-25 14:54:31 -05:00
										 |  |  |       allow(ENV).to receive(:[]) | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |       allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_A").and_return("1") | 
					
						
							|  |  |  |       allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_B").and_return("1") | 
					
						
							| 
									
										
										
										
											2019-02-25 14:54:31 -05:00
										 |  |  |       expect { parser.parse([]) }.to raise_error(Homebrew::CLI::OptionConflictError) | 
					
						
							| 
									
										
										
										
											2019-02-15 00:31:13 -05:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-05-25 04:05:45 +05:30
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-10-29 02:27:00 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "test immutability of args" do | 
					
						
							|  |  |  |     subject(:parser) { | 
					
						
							|  |  |  |       described_class.new do | 
					
						
							|  |  |  |         switch "-a", "--switch-a" | 
					
						
							|  |  |  |         switch "-b", "--switch-b" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises exception upon Homebrew.args mutation" do | 
					
						
							|  |  |  |       parser.parse(["--switch-a"]) | 
					
						
							| 
									
										
										
										
											2019-04-17 19:43:06 +09:00
										 |  |  |       expect { parser.parse(["--switch-b"]) }.to raise_error(RuntimeError, /Arguments were already parsed!/) | 
					
						
							| 
									
										
										
										
											2018-10-29 02:27:00 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-02-04 22:09:35 +05:30
										 |  |  | end |