From a8384c0ce77b012738611db206d7c0a2c0261d7d Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sat, 31 Dec 2022 11:17:17 -0800 Subject: [PATCH 1/3] 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. --- Library/Homebrew/cli/parser.rb | 2 +- Library/Homebrew/extend/os/linux/parser.rb | 4 ++- Library/Homebrew/test/cli/parser_spec.rb | 33 ++++++++++++++++++---- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 897d56449d..f2d58d1126 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -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 diff --git a/Library/Homebrew/extend/os/linux/parser.rb b/Library/Homebrew/extend/os/linux/parser.rb index 0c55c5862f..0195c1f67b 100644 --- a/Library/Homebrew/extend/os/linux/parser.rb +++ b/Library/Homebrew/extend/os/linux/parser.rb @@ -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 diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index b2983a09bb..51ab257815 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -564,14 +564,37 @@ 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 raise_error RuntimeError, "Invalid usage: Casks are not supported on Linux" 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 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 From d291117468575ad8d54f1992eb9f3b130fb7a431 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sat, 31 Dec 2022 15:52:05 -0800 Subject: [PATCH 2/3] Improve install cmd error msg It now specifies casks, formulae or both in theh output based upon the user args given. --- Library/Homebrew/cmd/install.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index 8a67db1249..c2911d502f 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -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 From d8c2e311ab03eb43dee91240aaea9eb09faa3b14 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Mon, 2 Jan 2023 10:59:08 -0800 Subject: [PATCH 3/3] Use odie instead of bare exception The reason that odie works well here is that it doesn't print a stacktrace or get caught and print the help page. --- Library/Homebrew/extend/os/linux/parser.rb | 6 +++--- Library/Homebrew/test/cli/parser_spec.rb | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/extend/os/linux/parser.rb b/Library/Homebrew/extend/os/linux/parser.rb index 0195c1f67b..fcbeb67f0b 100644 --- a/Library/Homebrew/extend/os/linux/parser.rb +++ b/Library/Homebrew/extend/os/linux/parser.rb @@ -15,9 +15,9 @@ module Homebrew return unless @args.respond_to?(:cask?) return unless @args.cask? - # 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" + # 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 diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index 51ab257815..8711931858 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -573,7 +573,9 @@ describe Homebrew::CLI::Parser do it "throws an error when defined" do expect { parser.parse(["--cask"]) } - .to raise_error RuntimeError, "Invalid usage: Casks are not supported on Linux" + .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 @@ -588,12 +590,16 @@ describe Homebrew::CLI::Parser do it "throws an error when --cask defined" do expect { parser.parse(["--cask"]) } - .to raise_error RuntimeError, "Invalid usage: Casks are not supported on Linux" + .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 raise_error RuntimeError, "Invalid usage: Casks are not supported on Linux" + .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