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 end
def install_phase(**options) def install_phase(**options)
move(source, target, **options) move(**options)
end end
def uninstall_phase(**options) def uninstall_phase(**options)
move(target, source, **options) move_back(**options)
end end
def summarize_installed def summarize_installed
@ -25,7 +25,7 @@ module Hbc
private private
def move(source, target, skip: false, force: false, command: nil, **options) def move(force: false, command: nil, **options)
if Utils.path_occupied?(target) if Utils.path_occupied?(target)
message = "It seems there is already #{self.class.english_article} #{self.class.english_name} at '#{target}'" message = "It seems there is already #{self.class.english_article} #{self.class.english_name} at '#{target}'"
raise CaskError, "#{message}." unless force raise CaskError, "#{message}." unless force
@ -34,8 +34,6 @@ module Hbc
end end
unless source.exist? unless source.exist?
return if skip
raise CaskError, "It seems the #{self.class.english_name} source '#{source}' is not there." raise CaskError, "It seems the #{self.class.english_name} source '#{source}' is not there."
end end
@ -51,6 +49,31 @@ module Hbc
add_altname_metadata(target, source.basename, command: command) add_altname_metadata(target, source.basename, command: command)
end 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, **_) def delete(target, force: false, command: nil, **_)
ohai "Removing #{self.class.english_name} '#{target}'." ohai "Removing #{self.class.english_name} '#{target}'."
raise CaskError, "Cannot remove undeletable #{self.class.english_name}." if MacOS.undeletable?(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 "--greedy", :greedy, false
option "--quiet", :quiet, false option "--quiet", :quiet, false
option "--force", :force, false option "--force", :force, false
option "--force-update", :force_update, false
option "--skip-cask-deps", :skip_cask_deps, false option "--skip-cask-deps", :skip_cask_deps, false
def initialize(*) def initialize(*)
@ -24,10 +23,9 @@ module Hbc
odebug "Started upgrade process for Cask #{old_cask}" odebug "Started upgrade process for Cask #{old_cask}"
raise CaskNotInstalledError, old_cask unless old_cask.installed? || force? raise CaskNotInstalledError, old_cask unless old_cask.installed? || force?
unless old_cask.installed_caskfile.nil? raise CaskUnavailableError.new(old_cask, "The Caskfile is missing!") if 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? old_cask = CaskLoader.load(old_cask.installed_caskfile)
end
old_cask_installer = Installer.new(old_cask, binaries: binaries?, verbose: verbose?, force: force?, upgrade: true) 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 # If successful, wipe the old Cask from staging
old_cask_installer.finalize_upgrade old_cask_installer.finalize_upgrade
rescue CaskError => e rescue CaskError => e
opoo e.message
new_cask_installer.uninstall_artifacts if new_artifacts_installed new_cask_installer.uninstall_artifacts if new_artifacts_installed
new_cask_installer.purge_versioned_files new_cask_installer.purge_versioned_files
old_cask_installer.revert_upgrade if started_upgrade old_cask_installer.revert_upgrade if started_upgrade
raise e
end end
end end
end end

View File

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

View File

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

View File

@ -12,7 +12,7 @@ describe Hbc::CLI::Uninstall, :cask do
output = Regexp.new <<~EOS output = Regexp.new <<~EOS
==> Uninstalling 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 ==> Purging files for version 1.2.3 of Cask local-caffeine
EOS EOS

View File

@ -158,7 +158,9 @@ describe Hbc::CLI::Upgrade, :cask do
expect(Hbc::CaskLoader.load("will-fail-if-upgraded").versions).to include("1.2.2") expect(Hbc::CaskLoader.load("will-fail-if-upgraded").versions).to include("1.2.2")
expect { expect {
described_class.run("will-fail-if-upgraded") expect {
described_class.run("will-fail-if-upgraded")
}.to raise_error(Hbc::CaskError)
}.to output(output_reverted).to_stderr }.to output(output_reverted).to_stderr
expect(Hbc::CaskLoader.load("will-fail-if-upgraded")).to be_installed expect(Hbc::CaskLoader.load("will-fail-if-upgraded")).to be_installed
@ -173,7 +175,9 @@ describe Hbc::CLI::Upgrade, :cask do
expect(Hbc::CaskLoader.load("bad-checksum").versions).to include("1.2.2") expect(Hbc::CaskLoader.load("bad-checksum").versions).to include("1.2.2")
expect { expect {
described_class.run("bad-checksum") expect {
described_class.run("bad-checksum")
}.to raise_error(Hbc::CaskSha256MismatchError)
}.to_not output(output_reverted).to_stderr }.to_not output(output_reverted).to_stderr
expect(Hbc::CaskLoader.load("bad-checksum")).to be_installed expect(Hbc::CaskLoader.load("bad-checksum")).to be_installed