Browse Source

don't reinstall already installed casks

thanks to @ccutrer for the catch; this implementation is based on his PR

refs #233
Paul Hinze 12 years ago
parent
commit
17026b6e93
  1. 10
      lib/cask/cli/install.rb
  2. 19
      lib/cask/exceptions.rb
  3. 6
      lib/cask/installer.rb
  4. 27
      test/cask/installer_test.rb
  5. 20
      test/cli/install_test.rb
  6. 10
      test/support/cleanup.rb
  7. 1
      test/test_helper.rb

10
lib/cask/cli/install.rb

@ -1,16 +1,14 @@
class Cask::CLI::Install
def self.run(*cask_names)
def self.run(*args)
cask_names = args.reject { |a| a == '--force' }
force = args.include? '--force'
cask_names.each do |cask_name|
begin
cask = Cask.load(cask_name)
Cask::Installer.install(cask)
Cask::AppLinker.new(cask).link
Cask::PkgInstaller.new(cask).install
rescue CaskUnavailableError => e
onoe e
rescue ChecksumMissingError => e
onoe e
rescue ChecksumMismatchError => e
rescue CaskError => e
onoe e
end
end

19
lib/cask/exceptions.rb

@ -1,4 +1,6 @@
class CaskNotInstalledError < RuntimeError
class CaskError < RuntimeError; end
class CaskNotInstalledError < CaskError
attr_reader :cask
def initialize cask
@cask = cask
@ -9,7 +11,7 @@ class CaskNotInstalledError < RuntimeError
end
end
class CaskUnavailableError < RuntimeError
class CaskUnavailableError < CaskError
attr_reader :name
def initialize name
@name = name
@ -20,7 +22,7 @@ class CaskUnavailableError < RuntimeError
end
end
class CaskAlreadyCreatedError < RuntimeError
class CaskAlreadyCreatedError < CaskError
attr_reader :name
def initialize name
@name = name
@ -30,3 +32,14 @@ class CaskAlreadyCreatedError < RuntimeError
"Cask for #{name} already exists. Use `brew cask edit #{name}` to see it."
end
end
class CaskAlreadyInstalledError < CaskError
attr_reader :name
def initialize name
@name = name
end
def to_s
"Cask for #{name} is already installed. Use `--force` to install anyways."
end
end

6
lib/cask/installer.rb

@ -2,7 +2,11 @@ require 'digest'
class Cask::Installer
class << self
def install(cask)
def install(cask, force=false)
if cask.installed? && !force
raise CaskAlreadyInstalledError.new(cask)
end
require 'formula_support'
software_spec = SoftwareSpec.new(cask.url.to_s, cask.version)
downloader = CurlDownloadStrategy.new(cask.title, software_spec)

27
test/cask/installer_test.rb

@ -69,6 +69,33 @@ describe Cask::Installer do
end
with_macosx_dir.destination_path.join('__MACOSX').wont_be :directory?
end
it "prevents already installed casks from being installed" do
transmission = Cask.load('local-transmission')
transmission.installed?.must_equal false
shutup do
Cask::Installer.install(transmission)
end
lambda {
shutup do
Cask::Installer.install(transmission)
end
}.must_raise(CaskAlreadyInstalledError)
end
it "allows already installed casks to being installed if force is provided" do
transmission = Cask.load('local-transmission')
transmission.installed?.must_equal false
shutup do
Cask::Installer.install(transmission)
end
shutup do
Cask::Installer.install(transmission, true)
end # wont_raise
end
end
describe "uninstall" do

20
test/cli/install_test.rb

@ -12,6 +12,26 @@ describe Cask::CLI::Install do
Cask.appdir.join('Caffeine.app').must_be :symlink?
end
it "prevents double install" do
shutup do
Cask::CLI::Install.run('local-transmission')
end
TestHelper.must_output(self, lambda {
Cask::CLI::Install.run('local-transmission')
}, 'Error: Cask for local-transmission is already installed. Use `--force` to install anyways.')
end
it "allows double install with --force" do
shutup do
Cask::CLI::Install.run('local-transmission')
end
TestHelper.must_output(self, lambda {
Cask::CLI::Install.run('local-transmission', '--force')
}, 'Error: Cask for local-transmission is already installed. Use `--force` to install anyways.')
end
it "properly handles casks that are not present" do
TestHelper.must_output(self, lambda {
Cask::CLI::Install.run('what-the-balls')

10
test/support/cleanup.rb

@ -0,0 +1,10 @@
module Cask::CleanupHooks
def after_teardown
super
Cask.all.select(&:installed?).each { |c| Cask::Installer.uninstall(c) }
end
end
class MiniTest::Spec
include Cask::CleanupHooks
end

1
test/test_helper.rb

@ -72,6 +72,7 @@ end
require 'support/fake_fetcher'
require 'support/fake_appdir'
require 'support/fake_system_command'
require 'support/cleanup'
# pretend like we installed the cask tap
project_root = Pathname.new(File.expand_path("#{File.dirname(__FILE__)}/../"))

Loading…
Cancel
Save