Implement @reitermarkus's comments

- Split move into a move_back (and clarify when it is used)
- Remove unused flags
- Raise error if installed Caskfile not found
- Error out if an upgrade fails
- Remove some defensive programming checks
This commit is contained in:
L. E. Segovia 2017-11-16 10:40:32 -03:00
parent 36fe355159
commit 8ee6ac2613
6 changed files with 42 additions and 20 deletions

View File

@ -8,11 +8,11 @@ module Hbc
end
def install_phase(**options)
move(source, target, **options)
move(**options)
end
def uninstall_phase(**options)
move(target, source, **options)
move_back(**options)
end
def summarize_installed
@ -25,7 +25,7 @@ module Hbc
private
def move(source, target, skip: false, force: false, command: nil, **options)
def move(force: false, command: nil, **options)
if Utils.path_occupied?(target)
message = "It seems there is already #{self.class.english_article} #{self.class.english_name} at '#{target}'"
raise CaskError, "#{message}." unless force
@ -34,8 +34,6 @@ module Hbc
end
unless source.exist?
return if skip
raise CaskError, "It seems the #{self.class.english_name} source '#{source}' is not there."
end
@ -51,6 +49,31 @@ module Hbc
add_altname_metadata(target, source.basename, command: command)
end
def move_back(skip: false, force: false, command: nil, **options)
if Utils.path_occupied?(source)
message = "It seems there is already #{self.class.english_article} #{self.class.english_name} at '#{source}'"
raise CaskError, "#{message}." unless force
opoo "#{message}; overwriting."
delete(source, force: force, command: command, **options)
end
unless target.exist?
return if skip
raise CaskError, "It seems the #{self.class.english_name} source '#{target}' is not there."
end
ohai "Moving #{self.class.english_name} '#{target.basename}' back to '#{source}'."
source.dirname.mkpath
if source.parent.writable?
FileUtils.move(target, source)
else
command.run("/bin/mv", args: [target, source], sudo: true)
end
add_altname_metadata(target, source.basename, command: command)
end
def delete(target, force: false, command: nil, **_)
ohai "Removing #{self.class.english_name} '#{target}'."
raise CaskError, "Cannot remove undeletable #{self.class.english_name}." if MacOS.undeletable?(target)

View File

@ -4,7 +4,6 @@ module Hbc
option "--greedy", :greedy, false
option "--quiet", :quiet, false
option "--force", :force, false
option "--force-update", :force_update, false
option "--skip-cask-deps", :skip_cask_deps, false
def initialize(*)
@ -24,10 +23,9 @@ module Hbc
odebug "Started upgrade process for Cask #{old_cask}"
raise CaskNotInstalledError, old_cask unless old_cask.installed? || force?
unless old_cask.installed_caskfile.nil?
# use the same cask file that was used for installation, if possible
old_cask = CaskLoader.load(old_cask.installed_caskfile) if old_cask.installed_caskfile.exist?
end
raise CaskUnavailableError.new(old_cask, "The Caskfile is missing!") if old_cask.installed_caskfile.nil?
old_cask = CaskLoader.load(old_cask.installed_caskfile)
old_cask_installer = Installer.new(old_cask, binaries: binaries?, verbose: verbose?, force: force?, upgrade: true)
@ -66,10 +64,10 @@ module Hbc
# If successful, wipe the old Cask from staging
old_cask_installer.finalize_upgrade
rescue CaskError => e
opoo e.message
new_cask_installer.uninstall_artifacts if new_artifacts_installed
new_cask_installer.purge_versioned_files
old_cask_installer.revert_upgrade if started_upgrade
raise e
end
end
end

View File

@ -83,8 +83,8 @@ module Hbc
def install
odebug "Hbc::Installer#install"
if @cask.installed? && !force? && !@reinstall
raise CaskAlreadyInstalledError, @cask unless upgrade?
if @cask.installed? && !force? && !@reinstall && !upgrade?
raise CaskAlreadyInstalledError, @cask
end
check_conflicts
@ -374,7 +374,6 @@ module Hbc
end
def start_upgrade
return unless upgrade?
oh1 "Starting upgrade for Cask #{@cask}"
disable_accessibility_access
@ -382,14 +381,12 @@ module Hbc
end
def revert_upgrade
return unless upgrade?
opoo "Reverting upgrade for Cask #{@cask}"
install_artifacts
enable_accessibility_access
end
def finalize_upgrade
return unless upgrade?
purge_versioned_files
puts summary

View File

@ -13,7 +13,7 @@ describe Hbc::CLI::Reinstall, :cask do
Already downloaded: .*local-caffeine--1.2.3.zip
==> Verifying checksum for Cask local-caffeine
==> Uninstalling Cask local-caffeine
==> Moving App 'Caffeine.app' to '.*Caffeine.app'.
==> Moving App 'Caffeine.app' back to '.*Caffeine.app'.
==> Purging files for version 1.2.3 of Cask local-caffeine
==> Installing Cask local-caffeine
==> Moving App 'Caffeine.app' to '.*Caffeine.app'.

View File

@ -12,7 +12,7 @@ describe Hbc::CLI::Uninstall, :cask do
output = Regexp.new <<~EOS
==> Uninstalling Cask local-caffeine
==> Moving App 'Caffeine.app' to '.*Caffeine.app'.
==> Moving App 'Caffeine.app' back to '.*Caffeine.app'.
==> Purging files for version 1.2.3 of Cask local-caffeine
EOS

View File

@ -157,8 +157,10 @@ describe Hbc::CLI::Upgrade, :cask do
expect(Hbc.appdir.join("container")).to be_a_file
expect(Hbc::CaskLoader.load("will-fail-if-upgraded").versions).to include("1.2.2")
expect {
expect {
described_class.run("will-fail-if-upgraded")
}.to raise_error(Hbc::CaskError)
}.to output(output_reverted).to_stderr
expect(Hbc::CaskLoader.load("will-fail-if-upgraded")).to be_installed
@ -172,8 +174,10 @@ describe Hbc::CLI::Upgrade, :cask do
expect(Hbc.appdir.join("Caffeine.app")).to be_a_directory
expect(Hbc::CaskLoader.load("bad-checksum").versions).to include("1.2.2")
expect {
expect {
described_class.run("bad-checksum")
}.to raise_error(Hbc::CaskSha256MismatchError)
}.to_not output(output_reverted).to_stderr
expect(Hbc::CaskLoader.load("bad-checksum")).to be_installed