diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index 3176489cb3..27bea06ef1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -19,6 +19,7 @@ require "hbc/cli/reinstall" require "hbc/cli/search" require "hbc/cli/style" require "hbc/cli/uninstall" +require "hbc/cli/--version" require "hbc/cli/zap" require "hbc/cli/internal_use_base" @@ -116,7 +117,7 @@ module Hbc elsif command.to_s.include?("/") && require?(command.to_s) # external command as Ruby library with literal path, useful # for development and troubleshooting - sym = Pathname.new(command.to_s).basename(".rb").to_s.capitalize + sym = File.basename(command.to_s, ".rb").capitalize klass = begin const_get(sym) rescue NameError @@ -247,16 +248,14 @@ module Hbc @attempted_verb = attempted_verb end - def run(*args) - if args.include?("--version") || @attempted_verb == "--version" - puts Hbc.full_version - else - purpose - usage - unless @attempted_verb.to_s.strip.empty? || @attempted_verb == "help" - raise CaskError, "Unknown command: #{@attempted_verb}" - end - end + def run(*_args) + purpose + usage + + return if @attempted_verb.to_s.strip.empty? + return if @attempted_verb == "help" + + raise ArgumentError, "Unknown command: #{@attempted_verb}" end def purpose diff --git a/Library/Homebrew/cask/lib/hbc/cli/--version.rb b/Library/Homebrew/cask/lib/hbc/cli/--version.rb new file mode 100644 index 0000000000..bbc719c3b6 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/cli/--version.rb @@ -0,0 +1,18 @@ +module Hbc + class CLI + class Version < Base + def self.command_name + "--#{super}" + end + + def self.run(*args) + raise ArgumentError, "#{command_name} does not take arguments." unless args.empty? + puts Hbc.full_version + end + + def self.help + "displays the Homebrew-Cask version" + end + end + end +end diff --git a/Library/Homebrew/test/cask/cli/--version_spec.rb b/Library/Homebrew/test/cask/cli/--version_spec.rb new file mode 100644 index 0000000000..2e5737c9fd --- /dev/null +++ b/Library/Homebrew/test/cask/cli/--version_spec.rb @@ -0,0 +1,13 @@ +describe Hbc::CLI::Version, :cask do + describe "::run" do + it "outputs the current Hombrew-Cask version" do + expect { described_class.run } + .to output(/\AHomebrew-Cask.*\d+\.\d+\.\d+/).to_stdout + .and not_to_output.to_stderr + end + + it "does not support arguments" do + expect { described_class.run(:foo, :bar) }.to raise_error(ArgumentError) + end + end +end diff --git a/Library/Homebrew/test/cask/cli/version_spec.rb b/Library/Homebrew/test/cask/cli/version_spec.rb deleted file mode 100644 index 2091496fc9..0000000000 --- a/Library/Homebrew/test/cask/cli/version_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -describe "brew cask --version", :cask do - it "respects the --version argument" do - expect { - expect { - Hbc::CLI::NullCommand.new("--version").run - }.not_to output.to_stderr - }.to output(Hbc.full_version).to_stdout - end -end diff --git a/Library/Homebrew/test/cask/cli_spec.rb b/Library/Homebrew/test/cask/cli_spec.rb index 0a4559ff21..0dac89b0e8 100644 --- a/Library/Homebrew/test/cask/cli_spec.rb +++ b/Library/Homebrew/test/cask/cli_spec.rb @@ -27,8 +27,10 @@ describe Hbc::CLI, :cask do end it "passes `--version` along to the subcommand" do - expect(described_class).to receive(:run_command).with(noop_command, "--version") - described_class.process(%w[noop --version]) + version_command = double("CLI::Version") + allow(described_class).to receive(:lookup_command).with("--version").and_return(version_command) + expect(described_class).to receive(:run_command).with(version_command) + described_class.process(["--version"]) end it "prints help output when subcommand receives `--help` flag" do diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index c8e5cc0f89..2f6274fd1d 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -112,4 +112,5 @@ RSpec.configure do |config| end end +RSpec::Matchers.define_negated_matcher :not_to_output, :output RSpec::Matchers.alias_matcher :have_failed, :be_failed diff --git a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb index a82098a0e1..b037068d2b 100644 --- a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb +++ b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb @@ -1,6 +1,5 @@ require "open3" -RSpec::Matchers.define_negated_matcher :not_to_output, :output RSpec::Matchers.define_negated_matcher :be_a_failure, :be_a_success RSpec.shared_context "integration test" do