diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 4fc804ad65..a507451f25 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -341,14 +341,20 @@ module Homebrew @args_parsed = true - if !ignore_invalid_options && @args.help? - puts generate_help_text - exit + unless ignore_invalid_options + if @args.help? + puts generate_help_text + exit + end + + validate_options end @args end + def validate_options; end + def generate_help_text Formatter.format_help_text(@parser.to_s, width: COMMAND_DESC_WIDTH) .gsub(/\n.*?@@HIDDEN@@.*?(?=\n)/, "") @@ -701,3 +707,5 @@ module Homebrew end end end + +require "extend/os/parser" diff --git a/Library/Homebrew/extend/os/linux/parser.rb b/Library/Homebrew/extend/os/linux/parser.rb new file mode 100644 index 0000000000..939585859d --- /dev/null +++ b/Library/Homebrew/extend/os/linux/parser.rb @@ -0,0 +1,20 @@ +# typed: false +# frozen_string_literal: true + +module Homebrew + module CLI + class Parser + undef validate_options + + def validate_options + return unless @args.respond_to?(:cask?) + return unless @args.cask? + + msg = "Casks are not supported on Linux" + raise UsageError, msg unless Homebrew::EnvConfig.developer? + + opoo msg unless @args.quiet? + end + end + end +end diff --git a/Library/Homebrew/extend/os/parser.rb b/Library/Homebrew/extend/os/parser.rb new file mode 100644 index 0000000000..90b0d42da7 --- /dev/null +++ b/Library/Homebrew/extend/os/parser.rb @@ -0,0 +1,4 @@ +# typed: false +# frozen_string_literal: true + +require "extend/os/linux/parser" if OS.linux? diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index 5aaab4683a..bfe0ae35c0 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -562,4 +562,22 @@ describe Homebrew::CLI::Parser do expect { parser.parse(["--not-a-command"]) }.to raise_error(OptionParser::InvalidOption, /--not-a-command/) end end + + describe "--cask on linux", :needs_linux do + subject(:parser) do + described_class.new do + switch "--cask" + end + end + + it "throws an error for normal users" do + allow(Homebrew::EnvConfig).to receive(:developer?).and_return(false) + expect { parser.parse(["--cask"]) }.to raise_error UsageError, /Casks are not supported on Linux/ + end + + it "only warns developers" do + allow(Homebrew::EnvConfig).to receive(:developer?).and_return(true) + expect { parser.parse(["--cask"]) }.not_to raise_error + end + end end diff --git a/Library/Homebrew/test/cmd/--cache_spec.rb b/Library/Homebrew/test/cmd/--cache_spec.rb index 410ca72f18..fbca7fddf9 100644 --- a/Library/Homebrew/test/cmd/--cache_spec.rb +++ b/Library/Homebrew/test/cmd/--cache_spec.rb @@ -17,7 +17,7 @@ describe "brew --cache" do .and be_a_success end - it "prints the cache files for a given Cask", :integration_test do + it "prints the cache files for a given Cask", :integration_test, :needs_macos do expect { brew "--cache", cask_path("local-caffeine") } .to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip}o).to_stdout .and output(/Treating #{Regexp.escape(cask_path("local-caffeine"))} as a cask/).to_stderr diff --git a/Library/Homebrew/test/cmd/home_spec.rb b/Library/Homebrew/test/cmd/home_spec.rb index 063846c334..d5908b20b5 100644 --- a/Library/Homebrew/test/cmd/home_spec.rb +++ b/Library/Homebrew/test/cmd/home_spec.rb @@ -34,7 +34,7 @@ describe "brew home" do .and be_a_success end - it "opens the homepage for a given Cask", :integration_test do + it "opens the homepage for a given Cask", :integration_test, :needs_macos do expect { brew "home", local_caffeine_path, "HOMEBREW_BROWSER" => "echo" } .to output(/#{local_caffeine_homepage}/).to_stdout .and output(/Treating #{Regexp.escape(local_caffeine_path)} as a cask/).to_stderr