From ab57da07ea747f72ea9184c41ffc33aa6c0b0fbd Mon Sep 17 00:00:00 2001 From: phinze Date: Sat, 20 Oct 2012 13:00:20 -0500 Subject: [PATCH] add brew cask uninstall this delegates to homebrew's uninstall to get its work done. vanilla `brew uninstall` actually works, but this gives us a more consistent interface. as discussed in #47 --- lib/cask.rb | 3 +-- lib/cask/actions.rb | 4 ---- lib/cask/cli/install.rb | 12 ++++++------ lib/cask/cli/uninstall.rb | 16 ++++++++++++++++ lib/cask/exceptions.rb | 11 +++++++++++ lib/cask/installer.rb | 10 ++++++++++ test/cask/actions_test.rb | 2 +- test/cask/installer_test.rb | 35 +++++++++++++++++++++++++++++++++++ test/cask_test.rb | 15 --------------- test/cli/install_test.rb | 18 ++++++++++-------- test/cli/uninstall_test.rb | 26 ++++++++++++++++++++++++++ 11 files changed, 116 insertions(+), 36 deletions(-) create mode 100644 lib/cask/cli/uninstall.rb create mode 100644 test/cask/installer_test.rb create mode 100644 test/cli/uninstall_test.rb diff --git a/lib/cask.rb b/lib/cask.rb index 3c1fcf341..56acd1af2 100644 --- a/lib/cask.rb +++ b/lib/cask.rb @@ -1,5 +1,3 @@ -require 'download_strategy' -require 'formula_support' require 'plist/parser' require 'uri' @@ -8,6 +6,7 @@ class Cask; end require 'cask/cli' require 'cask/cli/edit' require 'cask/cli/install' +require 'cask/cli/uninstall' require 'cask/cli/linkapps' require 'cask/cli/list' require 'cask/cli/search' diff --git a/lib/cask/actions.rb b/lib/cask/actions.rb index 5653ff3c6..50f59bf86 100644 --- a/lib/cask/actions.rb +++ b/lib/cask/actions.rb @@ -1,8 +1,4 @@ module Cask::Actions - def install - Cask::Installer.install(self) - end - def linkapps puts "looking in #{destination_path}/**/*.app" puts "found #{Pathname.glob("#{destination_path}/**/*.app").inspect}" diff --git a/lib/cask/cli/install.rb b/lib/cask/cli/install.rb index cd84675a6..1bfad31b2 100644 --- a/lib/cask/cli/install.rb +++ b/lib/cask/cli/install.rb @@ -1,12 +1,12 @@ class Cask::CLI::Install def self.run(*cask_names) cask_names.each do |cask_name| - cask = begin - Cask.load(cask_name) - rescue CaskUnavailableError => e - onoe e - end - cask.install if cask + begin + cask = Cask.load(cask_name) + Cask::Installer.install(cask) + rescue CaskUnavailableError => e + onoe e + end end end diff --git a/lib/cask/cli/uninstall.rb b/lib/cask/cli/uninstall.rb new file mode 100644 index 000000000..b7711df1a --- /dev/null +++ b/lib/cask/cli/uninstall.rb @@ -0,0 +1,16 @@ +class Cask::CLI::Uninstall + def self.run(*cask_names) + cask_names.each do |cask_name| + begin + cask = Cask.load(cask_name) + Cask::Installer.uninstall(cask) + rescue CaskUnavailableError,CaskNotInstalledError => e + onoe e + end + end + end + + def self.help + "uninstalls the cask of the given name" + end +end diff --git a/lib/cask/exceptions.rb b/lib/cask/exceptions.rb index cd34b8708..3f68370f6 100644 --- a/lib/cask/exceptions.rb +++ b/lib/cask/exceptions.rb @@ -1,3 +1,14 @@ +class CaskNotInstalledError < RuntimeError + attr_reader :cask + def initialize cask + @cask = cask + end + + def to_s + "#{cask} is not installed" + end +end + class CaskUnavailableError < RuntimeError attr_reader :name def initialize name diff --git a/lib/cask/installer.rb b/lib/cask/installer.rb index 021597436..609ee6c7a 100644 --- a/lib/cask/installer.rb +++ b/lib/cask/installer.rb @@ -1,6 +1,7 @@ class Cask::Installer class << self def install(cask) + require 'formula_support' downloader = CurlDownloadStrategy.new(cask.title, SoftwareSpec.new(cask.url.to_s, cask.version)) downloaded_path = downloader.fetch @@ -13,6 +14,15 @@ class Cask::Installer ohai "Success! #{cask} installed to #{cask.destination_path}" end + def uninstall(cask) + raise CaskNotInstalledError.new(cask) unless cask.installed? + + require 'cmd/uninstall' + ARGV.clear + ARGV << cask.title + Homebrew.uninstall + end + def _with_extracted_mountpoints(path) if _dmg?(path) File.open(path) do |dmg| diff --git a/test/cask/actions_test.rb b/test/cask/actions_test.rb index 9b244ca04..de74656d7 100644 --- a/test/cask/actions_test.rb +++ b/test/cask/actions_test.rb @@ -9,7 +9,7 @@ describe Cask::Actions do Cask.stubs(:appdir).returns(fake_appdir) @caffeine = Cask.load('caffeine') - shutup { @caffeine.install } + shutup { Cask::Installer.install(@caffeine) } @appdir = HOMEBREW_CELLAR/'caffeine'/@caffeine.version @app = @appdir/'Caffeine.app' end diff --git a/test/cask/installer_test.rb b/test/cask/installer_test.rb new file mode 100644 index 000000000..32b141966 --- /dev/null +++ b/test/cask/installer_test.rb @@ -0,0 +1,35 @@ +require 'test_helper' + +describe Cask::Installer do + describe "install" do + it "downloads and installs a nice fresh Cask" do + caffeine = Cask.load('caffeine') + + shutup do + Cask::Installer.install(caffeine) + end + + dest_path = HOMEBREW_CELLAR/'caffeine'/caffeine.version + dest_path.must_be :directory? + application = dest_path/'Caffeine.app' + application.must_be :directory? + end + end + + describe "uninstall" do + it "fully uninstalls a cask" do + caffeine = Cask.load('caffeine') + + shutup do + Cask::Installer.install(caffeine) + Cask::Installer.uninstall(caffeine) + end + + dest_path = HOMEBREW_CELLAR/'caffeine'/caffeine.version + application = dest_path/'Caffeine.app' + + application.wont_be :directory? + dest_path.wont_be :directory? + end + end +end diff --git a/test/cask_test.rb b/test/cask_test.rb index 57d171894..1d44d1d08 100644 --- a/test/cask_test.rb +++ b/test/cask_test.rb @@ -23,21 +23,6 @@ describe Cask do end end - describe "install" do - it "downloads and installs a nice fresh Cask" do - caffeine = Cask.load('caffeine') - - shutup do - caffeine.install - end - - dest_path = HOMEBREW_CELLAR/'caffeine'/caffeine.version - dest_path.must_be :directory? - application = dest_path/'Caffeine.app' - application.must_be :directory? - end - end - describe "init" do it "sets up dependent directories required for us to properly function" do HOMEBREW_CACHE.stubs(:exist?).returns(false) diff --git a/test/cli/install_test.rb b/test/cli/install_test.rb index 5f667de95..1e3a7d96e 100644 --- a/test/cli/install_test.rb +++ b/test/cli/install_test.rb @@ -2,19 +2,21 @@ require 'test_helper' describe Cask::CLI::Install do it "allows install of multiple casks at once" do - stub_cask = stub(:install => nil) - Cask.expects(:load).with('adium').returns(stub_cask) - Cask.expects(:load).with('google-chrome').returns(stub_cask) + Cask::Installer.stubs(:install) + Cask.expects(:load).with('adium') + Cask.expects(:load).with('google-chrome') Cask::CLI::Install.run('adium', 'google-chrome') end it "properly handles casks that are not present" do - stub_cask = stub(:install => nil) - Cask.expects(:load).with('adium').returns(stub_cask) + Cask::Installer.stubs(:install) + Cask.expects(:load).with('adium') Cask.expects(:load).with('what-the-balls').raises(CaskUnavailableError.new('what-the-balls')) - Cask.expects(:load).with('google-chrome').returns(stub_cask) - shutup do + Cask.expects(:load).with('google-chrome') + lambda { Cask::CLI::Install.run('adium', 'what-the-balls', 'google-chrome') - end + }.must_output <<-OUTPUT.gsub(/^ */, '') + Error: No available cask for what-the-balls + OUTPUT end end diff --git a/test/cli/uninstall_test.rb b/test/cli/uninstall_test.rb new file mode 100644 index 000000000..1e21d7263 --- /dev/null +++ b/test/cli/uninstall_test.rb @@ -0,0 +1,26 @@ +require 'test_helper' + +describe Cask::CLI::Uninstall do + it "shows an error when a bad cask is provided" do + lambda { + Cask::CLI::Uninstall.run('notacask') + }.must_output <<-OUTPUT.gsub(/^ */, '') + Error: No available cask for notacask + OUTPUT + end + + it "shows an error when a cask is provided that's not installed" do + lambda { + Cask::CLI::Uninstall.run('anvil') + }.must_output <<-OUTPUT.gsub(/^ */, '') + Error: anvil is not installed + OUTPUT + end + + it "delegates to the installer to properly uninstall" do + fake_cask = stub('fake-cask') + Cask.stubs(:load).with('fake-cask').returns(fake_cask) + Cask::Installer.expects(:uninstall).with(fake_cask) + Cask::CLI::Uninstall.run('fake-cask') + end +end