aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMarkus Reiter2017-06-14 12:18:38 +0200
committerGitHub2017-06-14 12:18:38 +0200
commitbfcb3928f86ea7e4b9274d9f85a502a3b38ac2d4 (patch)
tree413c4d77a58c9cab4e57e5155bb5022d5d248e4d /Library
parent054ed10cb1ea2d64c39ba2a3208c14a33d31274d (diff)
parent9c8f7138f35625878e017559c896441fda6f357c (diff)
downloadbrew-bfcb3928f86ea7e4b9274d9f85a502a3b38ac2d4.tar.bz2
Merge pull request #2771 from reitermarkus/cask-exceptions
🔨 Refactor Cask exceptions.
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/cask/lib/hbc/cask_loader.rb19
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb37
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/audit.rb7
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/cat.rb10
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/cleanup.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/edit.rb10
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/fetch.rb9
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/home.rb4
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/info.rb6
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/install.rb38
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb18
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb35
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/list.rb44
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/outdated.rb4
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/reinstall.rb27
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/style.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/uninstall.rb13
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/zap.rb9
-rw-r--r--Library/Homebrew/cask/lib/hbc/exceptions.rb97
-rw-r--r--Library/Homebrew/cask/lib/hbc/installer.rb3
-rw-r--r--Library/Homebrew/cask/lib/hbc/verify/checksum.rb4
-rw-r--r--Library/Homebrew/test/cask/cli/audit_spec.rb2
-rw-r--r--Library/Homebrew/test/cask/cli/cat_spec.rb6
-rw-r--r--Library/Homebrew/test/cask/cli/fetch_spec.rb2
-rw-r--r--Library/Homebrew/test/cask/cli/install_spec.rb8
-rw-r--r--Library/Homebrew/test/cask/cli/style_spec.rb2
-rw-r--r--Library/Homebrew/test/cask/cli/uninstall_spec.rb20
-rw-r--r--Library/Homebrew/test/cask/cli/zap_spec.rb6
-rw-r--r--Library/Homebrew/test/cask/dsl_spec.rb2
-rw-r--r--Library/Homebrew/test/cask/installer_spec.rb16
-rw-r--r--Library/Homebrew/test/cask/verify/checksum_spec.rb2
32 files changed, 189 insertions, 277 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/cask_loader.rb b/Library/Homebrew/cask/lib/hbc/cask_loader.rb
index a0c44d9b4..43fd9080d 100644
--- a/Library/Homebrew/cask/lib/hbc/cask_loader.rb
+++ b/Library/Homebrew/cask/lib/hbc/cask_loader.rb
@@ -32,9 +32,9 @@ module Hbc
end
def load
- raise CaskError, "'#{@path}' does not exist." unless @path.exist?
- raise CaskError, "'#{@path}' is not readable." unless @path.readable?
- raise CaskError, "'#{@path}' is not a file." unless @path.file?
+ raise CaskUnavailableError.new(@token, "'#{@path}' does not exist.") unless @path.exist?
+ raise CaskUnavailableError.new(@token, "'#{@path}' is not readable.") unless @path.readable?
+ raise CaskUnavailableError.new(@token, "'#{@path}' is not a file.") unless @path.file?
@content = IO.read(@path)
@@ -45,7 +45,7 @@ module Hbc
def cask(header_token, &block)
if @token != header_token
- raise CaskTokenDoesNotMatchError.new(@token, header_token)
+ raise CaskTokenMismatchError.new(@token, header_token)
end
Cask.new(header_token, sourcefile_path: @path, &block)
@@ -57,10 +57,11 @@ module Hbc
ref.to_s.match?(::URI.regexp)
end
+ attr_reader :url
+
def initialize(url)
- @url = url
- uri = URI(url)
- super Hbc.cache/File.basename(uri.path)
+ @url = URI(url)
+ super Hbc.cache/File.basename(@url.path)
end
def load
@@ -71,7 +72,7 @@ module Hbc
ohai "Downloading #{@url}."
curl @url, "-o", @path
rescue ErrorDuringExecution
- raise CaskUnavailableError, @url
+ raise CaskUnavailableError.new(@token, "Failed to download #{Formatter.url(@url)}.")
end
super
@@ -108,7 +109,7 @@ module Hbc
end
def load
- raise CaskUnavailableError, @token
+ raise CaskUnavailableError.new(@token, "No Cask with this name exists.")
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb
index eec2788ec..04a414904 100644
--- a/Library/Homebrew/cask/lib/hbc/cli.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli.rb
@@ -166,7 +166,7 @@ module Hbc
Hbc.default_tap.install unless Hbc.default_tap.installed?
Hbc.init if self.class.should_init?(command)
self.class.run_command(command, *args)
- rescue CaskError, CaskSha256MismatchError, ArgumentError, OptionParser::InvalidOption => e
+ rescue CaskError, ArgumentError, OptionParser::InvalidOption => e
msg = e.message
msg << e.backtrace.join("\n").prepend("\n") if ARGV.debug?
onoe msg
diff --git a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb
index c83fc3e42..77f85301e 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb
@@ -41,6 +41,43 @@ module Hbc
def initialize(*args)
@args = process_arguments(*args)
end
+
+ def self.warn_unavailable_with_suggestion(cask_token, e)
+ exact_match, partial_matches = Search.search(cask_token)
+ error_message = e.message
+ if exact_match
+ error_message.concat(" Did you mean:\n#{exact_match}")
+ elsif !partial_matches.empty?
+ error_message.concat(" Did you mean one of:\n")
+ .concat(Formatter.columns(partial_matches.take(20)))
+ end
+ onoe error_message
+ end
+
+ private
+
+ def casks(alternative: -> { [] })
+ return to_enum(:casks, alternative: alternative) unless block_given?
+
+ count = 0
+
+ casks = args.empty? ? alternative.call : args
+
+ casks.each do |cask_or_token|
+ begin
+ yield cask_or_token.respond_to?(:token) ? cask_or_token : CaskLoader.load(cask_or_token)
+ count += 1
+ rescue CaskUnavailableError => e
+ cask_token = cask_or_token
+ self.class.warn_unavailable_with_suggestion cask_token, e
+ rescue CaskError => e
+ onoe e.message
+ end
+ end
+
+ return :empty if casks.length.zero?
+ (count == casks.length) ? :complete : :incomplete
+ end
end
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/cli/audit.rb b/Library/Homebrew/cask/lib/hbc/cli/audit.rb
index 74d1ebfa7..35d82800c 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb
@@ -9,11 +9,8 @@ module Hbc
end
def run
- casks_to_audit = args.empty? ? Hbc.all : args.map(&CaskLoader.public_method(:load))
-
- failed_casks = casks_to_audit.reject do |cask|
- audit(cask)
- end
+ failed_casks = casks(alternative: -> { Hbc.all })
+ .reject { |cask| audit(cask) }
return if failed_casks.empty?
raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}"
diff --git a/Library/Homebrew/cask/lib/hbc/cli/cat.rb b/Library/Homebrew/cask/lib/hbc/cli/cat.rb
index e68481b46..d08c87bea 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/cat.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/cat.rb
@@ -7,10 +7,12 @@ module Hbc
end
def run
- args.each do |cask_token|
- cask_path = CaskLoader.path(cask_token)
- raise CaskUnavailableError, cask_token.to_s unless cask_path.exist?
- puts File.open(cask_path, &:read)
+ raise CaskError, "Cat incomplete." if cat_casks == :incomplete
+ end
+
+ def cat_casks
+ casks.each do |cask|
+ puts File.open(cask.sourcefile_path, &:read)
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb
index 40b37dd5d..356162db5 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb
@@ -20,7 +20,7 @@ module Hbc
end
def run
- remove_cache_files(*@args)
+ remove_cache_files(*args)
end
def cache_files
diff --git a/Library/Homebrew/cask/lib/hbc/cli/edit.rb b/Library/Homebrew/cask/lib/hbc/cli/edit.rb
index dec0fe36b..b9485886c 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/edit.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/edit.rb
@@ -9,10 +9,12 @@ module Hbc
def run
cask_token = args.first
- cask_path = CaskLoader.path(cask_token)
-
- unless cask_path.exist?
- raise CaskUnavailableError, %Q(#{cask_token}, run "brew cask create #{cask_token}" to create a new Cask)
+ cask_path = begin
+ CaskLoader.load(cask_token).sourcefile_path
+ rescue CaskUnavailableError => e
+ reason = e.reason.empty? ? "" : "#{e.reason} "
+ reason.concat("Run #{Formatter.identifier("brew cask create #{e.token}")} to create a new Cask.")
+ raise e.class.new(e.token, reason)
end
odebug "Opening editor for Cask #{cask_token}"
diff --git a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb
index 2c1cc5f66..e31b1a17c 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb
@@ -9,9 +9,12 @@ module Hbc
end
def run
- args.each do |cask_token|
- ohai "Downloading external files for Cask #{cask_token}"
- cask = CaskLoader.load(cask_token)
+ raise CaskError, "Fetch incomplete." if fetch_casks == :incomplete
+ end
+
+ def fetch_casks
+ casks.each do |cask|
+ ohai "Downloading external files for Cask #{cask}"
downloaded_path = Download.new(cask, force: force?).perform
Verify.all(cask, downloaded_path)
ohai "Success! Downloaded to -> #{downloaded_path}"
diff --git a/Library/Homebrew/cask/lib/hbc/cli/home.rb b/Library/Homebrew/cask/lib/hbc/cli/home.rb
index 009bc1e3e..d496e309e 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/home.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/home.rb
@@ -2,9 +2,7 @@ module Hbc
class CLI
class Home < AbstractCommand
def run
- casks = args.map(&CaskLoader.public_method(:load))
-
- if casks.empty?
+ if casks.none?
odebug "Opening project homepage"
self.class.open_url "https://caskroom.github.io/"
else
diff --git a/Library/Homebrew/cask/lib/hbc/cli/info.rb b/Library/Homebrew/cask/lib/hbc/cli/info.rb
index 623c4b737..d26747e17 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/info.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/info.rb
@@ -7,10 +7,8 @@ module Hbc
end
def run
- args.each do |cask_token|
- odebug "Getting info for Cask #{cask_token}"
- cask = CaskLoader.load(cask_token)
-
+ casks.each do |cask|
+ odebug "Getting info for Cask #{cask}"
self.class.info(cask)
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb
index 4d0abc677..0f1a5dd34 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/install.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb
@@ -10,53 +10,21 @@ module Hbc
end
def run
- retval = install_casks
- # retval is ternary: true/false/nil
-
- raise CaskError, "nothing to install" if retval.nil?
- raise CaskError, "install incomplete" unless retval
+ raise CaskError, "Install incomplete." if install_casks == :incomplete
end
def install_casks
- count = 0
- args.each do |cask_token|
+ casks.each do |cask|
begin
- cask = CaskLoader.load(cask_token)
Installer.new(cask, binaries: binaries?,
- verbose: verbose?,
+ verbose: verbose?,
force: force?,
skip_cask_deps: skip_cask_deps?,
require_sha: require_sha?).install
- count += 1
rescue CaskAlreadyInstalledError => e
opoo e.message
- count += 1
- rescue CaskAlreadyInstalledAutoUpdatesError => e
- opoo e.message
- count += 1
- rescue CaskUnavailableError => e
- self.class.warn_unavailable_with_suggestion cask_token, e
- rescue CaskNoShasumError => e
- opoo e.message
- count += 1
- rescue CaskError => e
- onoe e.message
end
end
-
- count.zero? ? nil : count == args.length
- end
-
- def self.warn_unavailable_with_suggestion(cask_token, e)
- exact_match, partial_matches = Search.search(cask_token)
- error_message = e.message
- if exact_match
- error_message.concat(". Did you mean:\n#{exact_match}")
- elsif !partial_matches.empty?
- error_message.concat(". Did you mean one of:\n")
- .concat(Formatter.columns(partial_matches.take(20)))
- end
- onoe error_message
end
def self.help
diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb
index 78dbf1622..e21ce86b6 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb
@@ -7,25 +7,11 @@ module Hbc
end
def run
- retval = dump_casks
- # retval is ternary: true/false/nil
-
- raise CaskError, "nothing to dump" if retval.nil?
- raise CaskError, "dump incomplete" unless retval
+ raise CaskError, "Dump incomplete." if dump_casks == :incomplet
end
def dump_casks
- count = 0
- args.each do |cask_token|
- begin
- cask = CaskLoader.load(cask_token)
- count += 1
- cask.dumpcask
- rescue StandardError => e
- opoo "#{cask_token} was not found or would not load: #{e}"
- end
- end
- count.zero? ? nil : count == args.length
+ casks.each(&:dumpcask)
end
def self.help
diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb
index 86dee7c9c..4515fe931 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb
@@ -72,38 +72,22 @@ module Hbc
end
def run
- retval = print_stanzas
- # retval is ternary: true/false/nil
- if retval.nil?
- exit 1 if quiet?
- raise CaskError, "nothing to print"
- elsif !retval
- exit 1 if quiet?
- raise CaskError, "print incomplete"
- end
+ return unless print_stanzas == :incomplete
+ exit 1 if quiet?
+ raise CaskError, "Print incomplete."
end
def print_stanzas
- count = 0
if ARTIFACTS.include?(stanza)
artifact_name = stanza
@stanza = :artifacts
end
- cask_tokens = args.empty? ? Hbc.all_tokens : args
- cask_tokens.each do |cask_token|
- print "#{cask_token}\t" if table?
-
- begin
- cask = CaskLoader.load(cask_token)
- rescue StandardError
- opoo "Cask '#{cask_token}' was not found" unless quiet?
- puts ""
- next
- end
+ casks(alternative: -> { Hbc.all }).each do |cask|
+ print "#{cask}\t" if table?
unless cask.respond_to?(stanza)
- opoo "no such stanza '#{stanza}' on Cask '#{cask_token}'" unless quiet?
+ opoo "no such stanza '#{stanza}' on Cask '#{cask}'" unless quiet?
puts ""
next
end
@@ -111,13 +95,13 @@ module Hbc
begin
value = cask.send(@stanza)
rescue StandardError
- opoo "failure calling '#{stanza}' on Cask '#{cask_token}'" unless quiet?
+ opoo "failure calling '#{stanza}' on Cask '#{cask}'" unless quiet?
puts ""
next
end
if artifact_name && !value.key?(artifact_name)
- opoo "no such stanza '#{artifact_name}' on Cask '#{cask_token}'" unless quiet?
+ opoo "no such stanza '#{artifact_name}' on Cask '#{cask}'" unless quiet?
puts ""
next
end
@@ -131,10 +115,7 @@ module Hbc
else
puts value.to_s
end
-
- count += 1
end
- count.zero? ? nil : count == cask_tokens.length
end
def self.help
diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb
index 9d2ded4be..9d978360e 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/list.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb
@@ -11,44 +11,22 @@ module Hbc
def run
retval = args.any? ? list : list_installed
- # retval is ternary: true/false/nil
- if retval.nil? && args.none?
- opoo "nothing to list" # special case: avoid exit code
- elsif retval.nil?
- raise CaskError, "nothing to list"
- elsif !retval
- raise CaskError, "listing incomplete"
- end
+ raise CaskError, "Listing incomplete." if retval == :incomplete
end
def list
- count = 0
-
- args.each do |cask_token|
- odebug "Listing files for Cask #{cask_token}"
- begin
- cask = CaskLoader.load(cask_token)
+ casks.each do |cask|
+ raise CaskNotInstalledError, cask unless cask.installed?
- if cask.installed?
- if one?
- puts cask.token
- elsif versions?
- puts self.class.format_versioned(cask)
- else
- cask = CaskLoader.load_from_file(cask.installed_caskfile)
- self.class.list_artifacts(cask)
- end
-
- count += 1
- else
- opoo "#{cask} is not installed"
- end
- rescue CaskUnavailableError => e
- onoe e
+ if one?
+ puts cask.token
+ elsif versions?
+ puts self.class.format_versioned(cask)
+ else
+ cask = CaskLoader.load_from_file(cask.installed_caskfile)
+ self.class.list_artifacts(cask)
end
end
-
- count.zero? ? nil : count == args.length
end
def self.list_artifacts(cask)
@@ -69,7 +47,7 @@ module Hbc
puts Formatter.columns(installed_casks.map(&:to_s))
end
- installed_casks.empty? ? nil : true
+ installed_casks.empty? ? :empty : :complete
end
def self.format_versioned(cask)
diff --git a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb
index 7877ead05..b0a84e8d2 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb
@@ -10,9 +10,7 @@ module Hbc
end
def run
- casks_to_check = args.empty? ? Hbc.installed : args.map(&CaskLoader.public_method(:load))
-
- casks_to_check.each do |cask|
+ casks(alternative: -> { Hbc.installed }).each do |cask|
odebug "Checking update info of Cask #{cask}"
self.class.list_if_outdated(cask, greedy?, verbose?)
end
diff --git a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb
index eb5f45c90..337a2eb9d 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb
@@ -2,28 +2,13 @@ module Hbc
class CLI
class Reinstall < Install
def install_casks
- count = 0
- args.each do |cask_token|
- begin
- cask = CaskLoader.load(cask_token)
-
- Installer.new(cask,
- binaries: binaries?,
- verbose: verbose?,
- force: force?,
- skip_cask_deps: skip_cask_deps?,
- require_sha: require_sha?).reinstall
-
- count += 1
- rescue CaskUnavailableError => e
- self.class.warn_unavailable_with_suggestion cask_token, e
- rescue CaskNoShasumError => e
- opoo e.message
- count += 1
- end
+ casks.each do |cask|
+ Installer.new(cask, binaries: binaries?,
+ verbose: verbose?,
+ force: force?,
+ skip_cask_deps: skip_cask_deps?,
+ require_sha: require_sha?).reinstall
end
-
- count.zero? ? nil : count == args.length
end
def self.help
diff --git a/Library/Homebrew/cask/lib/hbc/cli/style.rb b/Library/Homebrew/cask/lib/hbc/cli/style.rb
index c9417f509..86fc98eaa 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/style.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/style.rb
@@ -31,7 +31,7 @@ module Hbc
elsif args.any? { |file| File.exist?(file) }
args
else
- args.map { |token| CaskLoader.path(token) }
+ casks.map(&:sourcefile_path)
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb
index 33ee5afa9..c0697c808 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb
@@ -9,9 +9,12 @@ module Hbc
end
def run
- args.each do |cask_token|
- odebug "Uninstalling Cask #{cask_token}"
- cask = CaskLoader.load(cask_token)
+ raise CaskError, "Uninstall incomplete." if uninstall_casks == :incomplete
+ end
+
+ def uninstall_casks
+ casks.each do |cask|
+ odebug "Uninstalling Cask #{cask}"
raise CaskNotInstalledError, cask unless cask.installed? || force?
@@ -27,8 +30,8 @@ module Hbc
single = versions.count == 1
puts <<-EOS.undent
- #{cask_token} #{versions.join(", ")} #{single ? "is" : "are"} still installed.
- Remove #{single ? "it" : "them all"} with `brew cask uninstall --force #{cask_token}`.
+ #{cask} #{versions.join(", ")} #{single ? "is" : "are"} still installed.
+ Remove #{single ? "it" : "them all"} with `brew cask uninstall --force #{cask}`.
EOS
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/cli/zap.rb b/Library/Homebrew/cask/lib/hbc/cli/zap.rb
index 3c07ff9e8..d12943106 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/zap.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/zap.rb
@@ -7,9 +7,12 @@ module Hbc
end
def run
- args.each do |cask_token|
- odebug "Zapping Cask #{cask_token}"
- cask = CaskLoader.load(cask_token)
+ raise CaskError, "Zap incomplete." if zap_casks == :incomplete
+ end
+
+ def zap_casks
+ casks.each do |cask|
+ odebug "Zapping Cask #{cask}"
Installer.new(cask, verbose: verbose?).zap
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/exceptions.rb b/Library/Homebrew/cask/lib/hbc/exceptions.rb
index b9d305a9b..d9e1b07db 100644
--- a/Library/Homebrew/cask/lib/hbc/exceptions.rb
+++ b/Library/Homebrew/cask/lib/hbc/exceptions.rb
@@ -3,56 +3,40 @@ module Hbc
class AbstractCaskErrorWithToken < CaskError
attr_reader :token
+ attr_reader :reason
- def initialize(token)
+ def initialize(token, reason = nil)
@token = token
+ @reason = reason.to_s
end
end
class CaskNotInstalledError < AbstractCaskErrorWithToken
def to_s
- "#{token} is not installed"
+ "Cask '#{token}' is not installed."
end
end
class CaskUnavailableError < AbstractCaskErrorWithToken
def to_s
- "No available Cask for #{token}"
+ "Cask '#{token}' is unavailable" << (reason.empty? ? "." : ": #{reason}")
end
end
class CaskAlreadyCreatedError < AbstractCaskErrorWithToken
def to_s
- %Q(A Cask for #{token} already exists. Run "brew cask cat #{token}" to see it.)
+ %Q(Cask '#{token}' already exists. Run #{Formatter.identifier("brew cask cat #{token}")} to edit it.)
end
end
class CaskAlreadyInstalledError < AbstractCaskErrorWithToken
def to_s
- s = <<-EOS.undent
- A Cask for #{token} is already installed.
- EOS
-
- s.concat("\n").concat(reinstall_message)
- end
-
- private
-
- def reinstall_message
<<-EOS.undent
- To re-install #{token}, run:
- brew cask reinstall #{token}
- EOS
- end
- end
+ Cask '#{token}' is already installed.
- class CaskAlreadyInstalledAutoUpdatesError < CaskAlreadyInstalledError
- def to_s
- s = <<-EOS.undent
- A Cask for #{token} is already installed and using auto-updates.
+ To re-install #{token}, run:
+ #{Formatter.identifier("brew cask reinstall #{token}")}
EOS
-
- s.concat("\n").concat(reinstall_message)
end
end
@@ -84,8 +68,8 @@ module Hbc
class CaskX11DependencyError < AbstractCaskErrorWithToken
def to_s
<<-EOS.undent
- #{token} requires XQuartz/X11, which can be installed using Homebrew-Cask by running
- brew cask install xquartz
+ Cask '#{token}' requires XQuartz/X11, which can be installed using Homebrew-Cask by running
+ #{Formatter.identifier("brew cask install xquartz")}
or manually, by downloading the package from
#{Formatter.url("https://www.xquartz.org/")}
@@ -101,60 +85,67 @@ module Hbc
class CaskUnspecifiedError < CaskError
def to_s
- "This command requires a Cask token"
+ "This command requires a Cask token."
end
end
class CaskInvalidError < AbstractCaskErrorWithToken
- attr_reader :submsg
- def initialize(token, *submsg)
- super(token)
- @submsg = submsg.join(" ")
- end
-
def to_s
- "Cask '#{token}' definition is invalid#{": #{submsg}" unless submsg.empty?}"
+ "Cask '#{token}' definition is invalid" << (reason.empty? ? ".": ": #{reason}")
end
end
- class CaskTokenDoesNotMatchError < CaskInvalidError
+ class CaskTokenMismatchError < CaskInvalidError
def initialize(token, header_token)
- super(token, "Bad header line: '#{header_token}' does not match file name")
+ super(token, "Token '#{header_token}' in header line does not match the file name.")
end
end
- class CaskSha256MissingError < ArgumentError
- end
+ class CaskSha256Error < AbstractCaskErrorWithToken
+ attr_reader :expected, :actual
- class CaskSha256MismatchError < RuntimeError
- attr_reader :path, :expected, :actual
- def initialize(path, expected, actual)
- @path = path
+ def initialize(token, expected = nil, actual = nil)
+ super(token)
@expected = expected
@actual = actual
end
+ end
+ class CaskSha256MissingError < CaskSha256Error
def to_s
<<-EOS.undent
- sha256 mismatch
- Expected: #{expected}
- Actual: #{actual}
- File: #{path}
- To retry an incomplete download, remove the file above.
+ Cask '#{token}' requires a checksum:
+ #{Formatter.identifier("sha256 '#{actual}'")}
EOS
end
end
- class CaskNoShasumError < CaskError
- attr_reader :token
- def initialize(token)
- @token = token
+ class CaskSha256MismatchError < CaskSha256Error
+ attr_reader :path
+
+ def initialize(token, expected, actual, path)
+ super(token, expected, actual)
+ @path = path
+ end
+
+ def to_s
+ <<-EOS.undent
+ Checksum for Cask '#{token}' does not match.
+
+ Expected: #{Formatter.success(expected.to_s)}
+ Actual: #{Formatter.error(actual.to_s)}
+ File: #{path}
+
+ To retry an incomplete download, remove the file above.
+ EOS
end
+ end
+ class CaskNoShasumError < CaskSha256Error
def to_s
<<-EOS.undent
Cask '#{token}' does not have a sha256 checksum defined and was not installed.
- This means you have the "--require-sha" option set, perhaps in your HOMEBREW_CASK_OPTS.
+ This means you have the #{Formatter.identifier("--require-sha")} option set, perhaps in your HOMEBREW_CASK_OPTS.
EOS
end
end
diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb
index 7da9731e5..aa6d600f7 100644
--- a/Library/Homebrew/cask/lib/hbc/installer.rb
+++ b/Library/Homebrew/cask/lib/hbc/installer.rb
@@ -94,7 +94,6 @@ module Hbc
odebug "Hbc::Installer#install"
if @cask.installed? && !force? && !@reinstall
- raise CaskAlreadyInstalledAutoUpdatesError, @cask if @cask.auto_updates
raise CaskAlreadyInstalledError, @cask
end
@@ -143,7 +142,7 @@ module Hbc
def verify_has_sha
odebug "Checking cask has checksum"
return unless @cask.sha256 == :no_check
- raise CaskNoShasumError, @cask
+ raise CaskNoShasumError, @cask.token
end
def verify
diff --git a/Library/Homebrew/cask/lib/hbc/verify/checksum.rb b/Library/Homebrew/cask/lib/hbc/verify/checksum.rb
index d079a4446..620703d31 100644
--- a/Library/Homebrew/cask/lib/hbc/verify/checksum.rb
+++ b/Library/Homebrew/cask/lib/hbc/verify/checksum.rb
@@ -33,13 +33,13 @@ module Hbc
end
def verify_checksum
- raise CaskSha256MissingError, "sha256 required: sha256 '#{computed}'" if expected.nil? || expected.empty?
+ raise CaskSha256MissingError.new(cask.token, expected, computed) if expected.nil? || expected.empty?
if expected == computed
odebug "SHA256 checksums match"
else
ohai 'Note: running "brew update" may fix sha256 checksum errors'
- raise CaskSha256MismatchError.new(downloaded_path, expected, computed)
+ raise CaskSha256MismatchError.new(cask.token, expected, computed, downloaded_path)
end
end
end
diff --git a/Library/Homebrew/test/cask/cli/audit_spec.rb b/Library/Homebrew/test/cask/cli/audit_spec.rb
index 412db1481..01f506c8c 100644
--- a/Library/Homebrew/test/cask/cli/audit_spec.rb
+++ b/Library/Homebrew/test/cask/cli/audit_spec.rb
@@ -1,5 +1,5 @@
describe Hbc::CLI::Audit, :cask do
- let(:cask) { double }
+ let(:cask) { double("cask", token: nil) }
describe "selection of Casks to audit" do
it "audits all Casks if no tokens are given" do
diff --git a/Library/Homebrew/test/cask/cli/cat_spec.rb b/Library/Homebrew/test/cask/cli/cat_spec.rb
index 28089b2f1..b726a0b36 100644
--- a/Library/Homebrew/test/cask/cli/cat_spec.rb
+++ b/Library/Homebrew/test/cask/cli/cat_spec.rb
@@ -34,9 +34,9 @@ describe Hbc::CLI::Cat, :cask do
end
it "raises an exception when the Cask does not exist" do
- expect {
- Hbc::CLI::Cat.run("notacask")
- }.to raise_error(Hbc::CaskUnavailableError)
+ expect { Hbc::CLI::Cat.run("notacask") }
+ .to output(/is unavailable/).to_stderr
+ .and raise_error(Hbc::CaskError, "Cat incomplete.")
end
describe "when no Cask is specified" do
diff --git a/Library/Homebrew/test/cask/cli/fetch_spec.rb b/Library/Homebrew/test/cask/cli/fetch_spec.rb
index 9f3056631..54bdfc0c8 100644
--- a/Library/Homebrew/test/cask/cli/fetch_spec.rb
+++ b/Library/Homebrew/test/cask/cli/fetch_spec.rb
@@ -54,7 +54,7 @@ describe Hbc::CLI::Fetch, :cask do
shutup do
Hbc::CLI::Fetch.run("notacask")
end
- }.to raise_error(Hbc::CaskUnavailableError)
+ }.to raise_error(Hbc::CaskError, "Fetch incomplete.")
end
describe "when no Cask is specified" do
diff --git a/Library/Homebrew/test/cask/cli/install_spec.rb b/Library/Homebrew/test/cask/cli/install_spec.rb
index b1b26c867..cf69b5d86 100644
--- a/Library/Homebrew/test/cask/cli/install_spec.rb
+++ b/Library/Homebrew/test/cask/cli/install_spec.rb
@@ -41,7 +41,7 @@ describe Hbc::CLI::Install, :cask do
expect {
Hbc::CLI::Install.run("local-transmission")
- }.to output(/Warning: A Cask for local-transmission is already installed./).to_stderr
+ }.to output(/Warning: Cask 'local-transmission' is already installed./).to_stderr
end
it "allows double install with --force" do
@@ -70,7 +70,7 @@ describe Hbc::CLI::Install, :cask do
shutup do
Hbc::CLI::Install.run("notacask")
end
- }.to raise_error(Hbc::CaskError)
+ }.to raise_error(Hbc::CaskError, "Install incomplete.")
end
it "returns a suggestion for a misspelled Cask" do
@@ -80,7 +80,7 @@ describe Hbc::CLI::Install, :cask do
rescue Hbc::CaskError
nil
end
- }.to output(/No available Cask for localcaffeine\. Did you mean:\nlocal-caffeine/).to_stderr
+ }.to output(/Cask 'localcaffeine' is unavailable: No Cask with this name exists\. Did you mean:\nlocal-caffeine/).to_stderr
end
it "returns multiple suggestions for a Cask fragment" do
@@ -90,7 +90,7 @@ describe Hbc::CLI::Install, :cask do
rescue Hbc::CaskError
nil
end
- }.to output(/No available Cask for local-caf\. Did you mean one of:\nlocal-caffeine/).to_stderr
+ }.to output(/Cask 'local-caf' is unavailable: No Cask with this name exists\. Did you mean one of:\nlocal-caffeine/).to_stderr
end
describe "when no Cask is specified" do
diff --git a/Library/Homebrew/test/cask/cli/style_spec.rb b/Library/Homebrew/test/cask/cli/style_spec.rb
index a5dcf6f6f..640eca5f4 100644
--- a/Library/Homebrew/test/cask/cli/style_spec.rb
+++ b/Library/Homebrew/test/cask/cli/style_spec.rb
@@ -107,7 +107,7 @@ describe Hbc::CLI::Style, :cask do
end
it "tries to find paths for all tokens" do
- expect(Hbc::CaskLoader).to receive(:path).twice
+ expect(Hbc::CaskLoader).to receive(:load).twice.and_return(double("cask", sourcefile_path: nil))
subject
end
end
diff --git a/Library/Homebrew/test/cask/cli/uninstall_spec.rb b/Library/Homebrew/test/cask/cli/uninstall_spec.rb
index bc1f52613..cc640fad7 100644
--- a/Library/Homebrew/test/cask/cli/uninstall_spec.rb
+++ b/Library/Homebrew/test/cask/cli/uninstall_spec.rb
@@ -17,15 +17,15 @@ describe Hbc::CLI::Uninstall, :cask do
end
it "shows an error when a bad Cask is provided" do
- expect {
- Hbc::CLI::Uninstall.run("notacask")
- }.to raise_error(Hbc::CaskUnavailableError)
+ expect { Hbc::CLI::Uninstall.run("notacask") }
+ .to output(/is unavailable/).to_stderr
+ .and raise_error(Hbc::CaskError, "Uninstall incomplete.")
end
it "shows an error when a Cask is provided that's not installed" do
- expect {
- Hbc::CLI::Uninstall.run("local-caffeine")
- }.to raise_error(Hbc::CaskNotInstalledError)
+ expect { Hbc::CLI::Uninstall.run("local-caffeine") }
+ .to output(/is not installed/).to_stderr
+ .and raise_error(Hbc::CaskError, "Uninstall incomplete.")
end
it "tries anyway on a non-present Cask when --force is given" do
@@ -89,11 +89,9 @@ describe Hbc::CLI::Uninstall, :cask do
Hbc.appdir.join("MyFancyApp.app").rmtree
- expect {
- shutup do
- Hbc::CLI::Uninstall.run("with-uninstall-script-app")
- end
- }.to raise_error(Hbc::CaskError, /does not exist/)
+ expect { shutup { Hbc::CLI::Uninstall.run("with-uninstall-script-app") } }
+ .to output(/does not exist/).to_stderr
+ .and raise_error(Hbc::CaskError, "Uninstall incomplete.")
expect(cask).to be_installed
diff --git a/Library/Homebrew/test/cask/cli/zap_spec.rb b/Library/Homebrew/test/cask/cli/zap_spec.rb
index f3af0e66f..e39ca61f8 100644
--- a/Library/Homebrew/test/cask/cli/zap_spec.rb
+++ b/Library/Homebrew/test/cask/cli/zap_spec.rb
@@ -1,8 +1,8 @@
describe Hbc::CLI::Zap, :cask do
it "shows an error when a bad Cask is provided" do
- expect {
- Hbc::CLI::Zap.run("notacask")
- }.to raise_error(Hbc::CaskUnavailableError)
+ expect { Hbc::CLI::Zap.run("notacask") }
+ .to output(/is unavailable/).to_stderr
+ .and raise_error(Hbc::CaskError, "Zap incomplete.")
end
it "can zap and unlink multiple Casks at once" do
diff --git a/Library/Homebrew/test/cask/dsl_spec.rb b/Library/Homebrew/test/cask/dsl_spec.rb
index 7eeabcf49..5849f581b 100644
--- a/Library/Homebrew/test/cask/dsl_spec.rb
+++ b/Library/Homebrew/test/cask/dsl_spec.rb
@@ -57,7 +57,7 @@ describe Hbc::DSL, :cask do
it "raises an error" do
expect {
cask
- }.to raise_error(Hbc::CaskTokenDoesNotMatchError, /Bad header line:.*does not match file name/)
+ }.to raise_error(Hbc::CaskTokenMismatchError, /header line does not match the file name/)
end
end
diff --git a/Library/Homebrew/test/cask/installer_spec.rb b/Library/Homebrew/test/cask/installer_spec.rb
index a32f71cab..8ef82c1d5 100644
--- a/Library/Homebrew/test/cask/installer_spec.rb
+++ b/Library/Homebrew/test/cask/installer_spec.rb
@@ -145,22 +145,6 @@ describe Hbc::Installer, :cask do
expect(with_macosx_dir.staged_path.join("__MACOSX")).not_to be_a_directory
end
- it "installer method raises an exception when already-installed Casks which auto-update are attempted" do
- with_auto_updates = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/auto-updates.rb")
-
- expect(with_auto_updates).not_to be_installed
-
- installer = Hbc::Installer.new(with_auto_updates)
-
- shutup do
- installer.install
- end
-
- expect {
- installer.install
- }.to raise_error(Hbc::CaskAlreadyInstalledAutoUpdatesError)
- end
-
it "allows already-installed Casks which auto-update to be installed if force is provided" do
with_auto_updates = Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/auto-updates.rb")
diff --git a/Library/Homebrew/test/cask/verify/checksum_spec.rb b/Library/Homebrew/test/cask/verify/checksum_spec.rb
index 55c0e5f44..4b8543d2b 100644
--- a/Library/Homebrew/test/cask/verify/checksum_spec.rb
+++ b/Library/Homebrew/test/cask/verify/checksum_spec.rb
@@ -1,5 +1,5 @@
describe Hbc::Verify::Checksum, :cask do
- let(:cask) { double("cask") }
+ let(:cask) { double("cask", token: "cask") }
let(:downloaded_path) { double("downloaded_path") }
let(:verification) { described_class.new(cask, downloaded_path) }