cmd/install: upgrade already installed casks

Previously, the behavior was to warn users that a cask was already
installed and then skip modifying the installed version. This is
different to how we handled things with formulas. For them we would
upgrade any already installed formulas. This just brings casks in line
with what we already do with formulas.

Changes:
- cmd/install: Upgrade already installed casks if HOMEBREW_NO_INSTALL_UPGRADE
  is not set
- env_config: Update wording of HOMEBREW_NO_INSTALL_UPGRADE to include casks
- remove error that was only used to alert about already installed casks

Note:
- The upgrade command for casks defaults to --greedy when you pass named casks
  to the command which means that this will always default to that behavior
  since you must specify the name of the cask when installing.
This commit is contained in:
apainintheneck 2023-07-23 18:21:15 -07:00
parent 0e8d1e0bec
commit c9dea04bd4
5 changed files with 30 additions and 49 deletions

View File

@ -132,21 +132,6 @@ module Cask
end end
end end
# Error when a cask is already installed.
#
# @api private
class CaskAlreadyInstalledError < AbstractCaskErrorWithToken
sig { returns(String) }
def to_s
<<~EOS
Cask '#{token}' is already installed.
To re-install #{token}, run:
#{Formatter.identifier("brew reinstall --cask #{token}")}
EOS
end
end
# Error when there is a cyclic cask dependency. # Error when there is a cyclic cask dependency.
# #
# @api private # @api private

View File

@ -91,11 +91,6 @@ module Cask
Migrator.migrate_if_needed(@cask) Migrator.migrate_if_needed(@cask)
old_config = @cask.config old_config = @cask.config
if @cask.installed? && !force? && !reinstall? && !upgrade?
return if quiet?
raise CaskAlreadyInstalledError, @cask
end
predecessor = @cask if reinstall? && @cask.installed? predecessor = @cask if reinstall? && @cask.installed?
check_conflicts check_conflicts

View File

@ -228,18 +228,34 @@ module Homebrew
require "cask/installer" require "cask/installer"
casks.each do |cask| installed_casks, new_casks = casks.partition(&:installed?)
Cask::Installer.new(cask,
binaries: args.binaries?, new_casks.each do |cask|
verbose: args.verbose?, Cask::Installer.new(
force: args.force?, cask,
adopt: args.adopt?, binaries: args.binaries?,
require_sha: args.require_sha?, verbose: args.verbose?,
skip_cask_deps: args.skip_cask_deps?, force: args.force?,
quarantine: args.quarantine?, adopt: args.adopt?,
quiet: args.quiet?).install require_sha: args.require_sha?,
rescue Cask::CaskAlreadyInstalledError => e skip_cask_deps: args.skip_cask_deps?,
opoo e.message quarantine: args.quarantine?,
quiet: args.quiet?,
).install
end
if !Homebrew::EnvConfig.no_install_upgrade? && installed_casks.any?
Cask::Upgrade.upgrade_casks(
*installed_casks,
force: args.force?,
dry_run: args.dry_run?,
binaries: args.binaries?,
quarantine: args.quarantine?,
require_sha: args.require_sha?,
skip_cask_deps: args.skip_cask_deps?,
verbose: args.verbose?,
args: args,
)
end end
end end

View File

@ -315,7 +315,7 @@ module Homebrew
boolean: true, boolean: true,
}, },
HOMEBREW_NO_INSTALL_UPGRADE: { HOMEBREW_NO_INSTALL_UPGRADE: {
description: "If set, `brew install` <formula> will not upgrade <formula> if it is installed but " \ description: "If set, `brew install` <formula/cask> will not upgrade <formula/cask> if it is installed but " \
"outdated.", "outdated.",
boolean: true, boolean: true,
}, },

View File

@ -141,21 +141,6 @@ describe Cask::Installer, :cask do
end.not_to raise_error end.not_to raise_error
end end
# unlike the CLI, the internal interface throws exception on double-install
it "installer method raises an exception when already-installed Casks are attempted" do
transmission = Cask::CaskLoader.load(cask_path("local-transmission"))
expect(transmission).not_to be_installed
installer = described_class.new(transmission)
installer.install
expect do
installer.install
end.to raise_error(Cask::CaskAlreadyInstalledError)
end
it "allows already-installed Casks to be installed if force is provided" do it "allows already-installed Casks to be installed if force is provided" do
transmission = Cask::CaskLoader.load(cask_path("local-transmission")) transmission = Cask::CaskLoader.load(cask_path("local-transmission"))
@ -313,7 +298,7 @@ describe Cask::Installer, :cask do
caffeine = Cask::CaskLoader.load(path) caffeine = Cask::CaskLoader.load(path)
expect(caffeine).to receive(:loaded_from_api?).twice.and_return(true) expect(caffeine).to receive(:loaded_from_api?).twice.and_return(true)
expect(caffeine).to receive(:caskfile_only?).twice.and_return(true) expect(caffeine).to receive(:caskfile_only?).twice.and_return(true)
expect(caffeine).to receive(:installed_caskfile).twice.and_return(invalid_path) expect(caffeine).to receive(:installed_caskfile).once.and_return(invalid_path)
described_class.new(caffeine).install described_class.new(caffeine).install
expect(Cask::CaskLoader.load(path)).to be_installed expect(Cask::CaskLoader.load(path)).to be_installed