cli/parser: Better error message for cask on linux

1. Validate options before constraint violations. This allows
us to error out when --cask is passed on Linux before getting
a constraint violation when --cask and --formula are set.
2. Skip printing the help page when --cask is passed on Linux.
This commit is contained in:
apainintheneck 2022-12-31 11:17:17 -08:00
parent e76c55e184
commit a8384c0ce7
3 changed files with 32 additions and 7 deletions

View File

@ -332,8 +332,8 @@ module Homebrew
set_default_options
unless ignore_invalid_options
check_constraint_violations
validate_options
check_constraint_violations
check_named_args(named_args)
end

View File

@ -15,7 +15,9 @@ module Homebrew
return unless @args.respond_to?(:cask?)
return unless @args.cask?
raise UsageError, "Casks are not supported on Linux"
# NOTE: We don't raise a UsageError here because
# we don't want to print the help page.
raise "Invalid usage: Casks are not supported on Linux"
end
end
end

View File

@ -564,6 +564,7 @@ describe Homebrew::CLI::Parser do
end
describe "--cask on linux", :needs_linux do
context "without --formula switch" do
subject(:parser) do
described_class.new do
switch "--cask"
@ -571,7 +572,29 @@ describe Homebrew::CLI::Parser do
end
it "throws an error when defined" do
expect { parser.parse(["--cask"]) }.to raise_error UsageError, /Casks are not supported on Linux/
expect { parser.parse(["--cask"]) }
.to raise_error RuntimeError, "Invalid usage: Casks are not supported on Linux"
end
end
context "with conflicting --formula switch" do
subject(:parser) do
described_class.new do
switch "--cask"
switch "--formula"
conflicts "--cask", "--formula"
end
end
it "throws an error when --cask defined" do
expect { parser.parse(["--cask"]) }
.to raise_error RuntimeError, "Invalid usage: Casks are not supported on Linux"
end
it "throws an error when both defined" do
expect { parser.parse(["--cask", "--formula"]) }
.to raise_error RuntimeError, "Invalid usage: Casks are not supported on Linux"
end
end
end