From 696169fbd8b07b90de55721e1da32ff7c76319f3 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Mon, 5 Jan 2015 17:21:21 -0500 Subject: [PATCH] Add `brew cask --prefix [cask]` command --- lib/hbc/cli.rb | 1 + lib/hbc/cli/prefix.rb | 29 +++++++++++++++++ test/cask/cli/prefix_test.rb | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 lib/hbc/cli/prefix.rb create mode 100644 test/cask/cli/prefix_test.rb diff --git a/lib/hbc/cli.rb b/lib/hbc/cli.rb index 7b870e297ae7..5d839d69b3e0 100644 --- a/lib/hbc/cli.rb +++ b/lib/hbc/cli.rb @@ -16,6 +16,7 @@ class Hbc::CLI; end require 'hbc/cli/info' require 'hbc/cli/install' require 'hbc/cli/list' +require 'hbc/cli/prefix' require 'hbc/cli/search' require 'hbc/cli/uninstall' require 'hbc/cli/update' diff --git a/lib/hbc/cli/prefix.rb b/lib/hbc/cli/prefix.rb new file mode 100644 index 000000000000..83dabd576ed2 --- /dev/null +++ b/lib/hbc/cli/prefix.rb @@ -0,0 +1,29 @@ +class Hbc::CLI::Prefix < Hbc::CLI::Base + def self.run(*args) + cask_tokens = cask_tokens_from(args) + if cask_tokens.empty? + puts install_prefix + else + cask_tokens.each { |cask_token| puts cask_prefix(cask_token) } + end + end + + def self.install_prefix + @install_prefix ||= Pathname.new(HOMEBREW_PREFIX).join('opt', 'brew-cask') + end + + def self.cask_prefix(cask_token) + cask = Hbc.load(cask_token) + raise Hbc::CaskNotInstalledError.new(cask) unless cask.installed? + cask.staged_path + end + + def self.command_name + @command_name ||= '--prefix' + end + + def self.help + "with no args, displays Homebrew-cask's install path; given installed #{ + }Casks, lists the locations in the Caskroom where the Casks are installed" + end +end diff --git a/test/cask/cli/prefix_test.rb b/test/cask/cli/prefix_test.rb new file mode 100644 index 000000000000..35d8c3f9a00e --- /dev/null +++ b/test/cask/cli/prefix_test.rb @@ -0,0 +1,61 @@ +require 'test_helper' + +describe Hbc::CLI::Prefix do + describe 'given no args' do + before do + @expected_output = "#{HOMEBREW_PREFIX}/opt/brew-cask\n" + end + + it 'displays the install location for Homebrew-cask' do + lambda { + Hbc::CLI::Prefix.run + }.must_output(@expected_output) + end + end + + describe 'given one or more Casks' do + before do + @caffeine = Hbc.load('local-caffeine') + @transmission = Hbc.load('local-transmission') + + shutup do + Hbc::Installer.new(@caffeine).install + Hbc::Installer.new(@transmission).install + end + end + + after do + shutup do + Hbc::CLI::Uninstall.run('local-caffeine', 'local-transmission') + end + end + + it 'displays the install location for an installed Cask' do + lambda { + Hbc::CLI::Prefix.run('local-caffeine') + }.must_output("#{@caffeine.staged_path}\n") + + lambda { + Hbc::CLI::Prefix.run('local-transmission') + }.must_output("#{@transmission.staged_path}\n") + end + + it 'displays the install locations for multiple installed Casks' do + lambda { + Hbc::CLI::Prefix.run('local-caffeine', 'local-transmission') + }.must_output("#{@caffeine.staged_path}\n#{@transmission.staged_path}\n") + end + + it 'shows an error when a bad Cask is provided' do + lambda { + Hbc::CLI::Prefix.run('notacask') + }.must_raise Hbc::CaskUnavailableError + end + + it "shows an error when a Cask is provided that's not installed" do + lambda { + Hbc::CLI::Prefix.run('anvil') + }.must_raise Hbc::CaskNotInstalledError + end + end +end