From a8384c0ce77b012738611db206d7c0a2c0261d7d Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sat, 31 Dec 2022 11:17:17 -0800 Subject: [PATCH] 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