Merge pull request #14315 from apainintheneck/better-error-cask-on-linux

cli/parser: Better error message for cask on linux
This commit is contained in:
Kevin 2023-01-02 13:21:56 -08:00 committed by GitHub
commit 2e0b18f762
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 9 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

@ -297,7 +297,12 @@ module Homebrew
return if name.include?("/")
require "search"
ohai "Searching for similarly named formulae and casks..."
package_types = []
package_types << "formulae" unless args.cask?
package_types << "casks" unless args.formula?
ohai "Searching for similarly named #{package_types.join(" and ")}..."
# Don't treat formula/cask name as a regex
query = string_or_regex = name
@ -324,6 +329,6 @@ module Homebrew
end
return if all_formulae.any? || all_casks.any?
odie "No formulae or casks found for #{name}."
odie "No #{package_types.join(" or ")} found for #{name}."
end
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 an error here because we don't want
# to print the help page or a stack trace.
odie "Invalid `--cask` usage: Casks do not work on Linux"
end
end
end

View File

@ -564,14 +564,43 @@ describe Homebrew::CLI::Parser do
end
describe "--cask on linux", :needs_linux do
subject(:parser) do
described_class.new do
switch "--cask"
context "without --formula switch" do
subject(:parser) do
described_class.new do
switch "--cask"
end
end
it "throws an error when defined" do
expect { parser.parse(["--cask"]) }
.to output("Error: Invalid `--cask` usage: Casks do not work on Linux\n").to_stderr
.and not_to_output.to_stdout
.and raise_exception SystemExit
end
end
it "throws an error when defined" do
expect { parser.parse(["--cask"]) }.to raise_error UsageError, /Casks are not supported on Linux/
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 output("Error: Invalid `--cask` usage: Casks do not work on Linux\n").to_stderr
.and not_to_output.to_stdout
.and raise_exception SystemExit
end
it "throws an error when both defined" do
expect { parser.parse(["--cask", "--formula"]) }
.to output("Error: Invalid `--cask` usage: Casks do not work on Linux\n").to_stderr
.and not_to_output.to_stdout
.and raise_exception SystemExit
end
end
end