Skip to content

Commit

Permalink
add brew cask uninstall
Browse files Browse the repository at this point in the history
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
  • Loading branch information
phinze committed Oct 20, 2012
1 parent 9f9eb19 commit ab57da0
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 36 deletions.
3 changes: 1 addition & 2 deletions lib/cask.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'download_strategy'
require 'formula_support'
require 'plist/parser'
require 'uri'

Expand All @@ -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'
Expand Down
4 changes: 0 additions & 4 deletions lib/cask/actions.rb
Original file line number Diff line number Diff line change
@@ -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}"
Expand Down
12 changes: 6 additions & 6 deletions lib/cask/cli/install.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
16 changes: 16 additions & 0 deletions lib/cask/cli/uninstall.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/cask/exceptions.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/cask/installer.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -13,6 +14,15 @@ def install(cask)
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|
Expand Down
2 changes: 1 addition & 1 deletion test/cask/actions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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
Expand Down
35 changes: 35 additions & 0 deletions test/cask/installer_test.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 0 additions & 15 deletions test/cask_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@
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)
Expand Down
18 changes: 10 additions & 8 deletions test/cli/install_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

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
26 changes: 26 additions & 0 deletions test/cli/uninstall_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ab57da0

Please sign in to comment.