brew/Library/Homebrew/test/cask/cmd/uninstall_spec.rb

180 lines
5.5 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2017-10-03 10:49:58 +02:00
require_relative "shared_examples/requires_cask_token"
require_relative "shared_examples/invalid_option"
2018-09-06 08:29:14 +02:00
describe Cask::Cmd::Uninstall, :cask do
2017-10-03 10:49:58 +02:00
it_behaves_like "a command that requires a Cask token"
it_behaves_like "a command that handles invalid options"
it "displays the uninstallation progress" do
2018-09-06 08:29:14 +02:00
caffeine = Cask::CaskLoader.load(cask_path("local-caffeine"))
2018-09-06 08:29:14 +02:00
Cask::Installer.new(caffeine).install
2017-10-15 02:28:32 +02:00
output = Regexp.new <<~EOS
==> Uninstalling Cask local-caffeine
2018-02-14 07:56:59 +10:00
==> Backing App 'Caffeine.app' up to '.*Caffeine.app'.
2018-01-21 19:10:30 +10:00
==> Removing App '.*Caffeine.app'.
2017-11-10 10:05:18 -03:00
==> Purging files for version 1.2.3 of Cask local-caffeine
EOS
expect {
2017-10-03 10:49:58 +02:00
described_class.run("local-caffeine")
}.to output(output).to_stdout
end
2016-08-18 22:11:42 +03:00
it "shows an error when a bad Cask is provided" do
2017-10-03 10:49:58 +02:00
expect { described_class.run("notacask") }
2018-09-06 08:29:14 +02:00
.to raise_error(Cask::CaskUnavailableError, /is unavailable/)
2016-08-18 22:11:42 +03:00
end
it "shows an error when a Cask is provided that's not installed" do
2017-10-03 10:49:58 +02:00
expect { described_class.run("local-caffeine") }
2018-09-06 08:29:14 +02:00
.to raise_error(Cask::CaskNotInstalledError, /is not installed/)
2016-08-18 22:11:42 +03:00
end
it "tries anyway on a non-present Cask when --force is given" do
2017-02-08 13:40:31 +01:00
expect {
2017-10-03 10:49:58 +02:00
described_class.run("local-caffeine", "--force")
2017-02-08 13:40:31 +01:00
}.not_to raise_error
2016-08-18 22:11:42 +03:00
end
it "can uninstall and unlink multiple Casks at once" do
2018-09-06 08:29:14 +02:00
caffeine = Cask::CaskLoader.load(cask_path("local-caffeine"))
transmission = Cask::CaskLoader.load(cask_path("local-transmission"))
2016-08-18 22:11:42 +03:00
2018-09-06 08:29:14 +02:00
Cask::Installer.new(caffeine).install
Cask::Installer.new(transmission).install
2016-08-18 22:11:42 +03:00
2017-02-08 13:40:31 +01:00
expect(caffeine).to be_installed
expect(transmission).to be_installed
2016-08-18 22:11:42 +03:00
2017-10-03 10:49:58 +02:00
described_class.run("local-caffeine", "local-transmission")
2016-08-18 22:11:42 +03:00
2017-02-08 13:40:31 +01:00
expect(caffeine).not_to be_installed
2019-02-02 17:11:37 +01:00
expect(caffeine.config.appdir.join("Transmission.app")).not_to exist
2017-02-08 13:40:31 +01:00
expect(transmission).not_to be_installed
2019-02-02 17:11:37 +01:00
expect(transmission.config.appdir.join("Caffeine.app")).not_to exist
end
it "calls `uninstall` before removing artifacts" do
2018-09-06 08:29:14 +02:00
cask = Cask::CaskLoader.load(cask_path("with-uninstall-script-app"))
2018-09-06 08:29:14 +02:00
Cask::Installer.new(cask).install
expect(cask).to be_installed
2020-09-29 23:46:30 +02:00
expect(cask.config.appdir.join("MyFancyApp.app")).to exist
expect {
2017-10-03 10:49:58 +02:00
described_class.run("with-uninstall-script-app")
}.not_to raise_error
expect(cask).not_to be_installed
2019-02-02 17:11:37 +01:00
expect(cask.config.appdir.join("MyFancyApp.app")).not_to exist
2016-08-18 22:11:42 +03:00
end
it "can uninstall Casks when the uninstall script is missing, but only when using `--force`" do
2018-09-06 08:29:14 +02:00
cask = Cask::CaskLoader.load(cask_path("with-uninstall-script-app"))
2018-09-06 08:29:14 +02:00
Cask::Installer.new(cask).install
expect(cask).to be_installed
2019-02-02 17:11:37 +01:00
cask.config.appdir.join("MyFancyApp.app").rmtree
2017-10-03 10:49:58 +02:00
expect { described_class.run("with-uninstall-script-app") }
2018-09-06 08:29:14 +02:00
.to raise_error(Cask::CaskError, /uninstall script .* does not exist/)
expect(cask).to be_installed
expect {
2017-10-03 10:49:58 +02:00
described_class.run("with-uninstall-script-app", "--force")
}.not_to raise_error
expect(cask).not_to be_installed
end
2016-08-18 22:11:42 +03:00
describe "when multiple versions of a cask are installed" do
let(:token) { "versioned-cask" }
let(:first_installed_version) { "1.2.3" }
let(:last_installed_version) { "4.5.6" }
let(:timestamped_versions) {
[
[first_installed_version, "123000"],
[last_installed_version, "456000"],
]
}
2018-09-06 08:29:14 +02:00
let(:caskroom_path) { Cask::Caskroom.path.join(token).tap(&:mkpath) }
2016-08-18 22:11:42 +03:00
before do
2016-08-18 22:11:42 +03:00
timestamped_versions.each do |timestamped_version|
caskroom_path.join(".metadata", *timestamped_version, "Casks").tap(&:mkpath)
.join("#{token}.rb").open("w") do |caskfile|
2018-07-11 15:17:40 +02:00
caskfile.puts <<~RUBY
2016-08-18 22:11:42 +03:00
cask '#{token}' do
version '#{timestamped_version[0]}'
end
2018-07-11 15:17:40 +02:00
RUBY
2016-08-18 22:11:42 +03:00
end
caskroom_path.join(timestamped_version[0]).mkpath
end
end
it "uninstalls one version at a time" do
2017-10-03 10:49:58 +02:00
described_class.run("versioned-cask")
2016-08-18 22:11:42 +03:00
2017-02-08 13:40:31 +01:00
expect(caskroom_path.join(first_installed_version)).to exist
expect(caskroom_path.join(last_installed_version)).not_to exist
expect(caskroom_path).to exist
2016-08-18 22:11:42 +03:00
2017-10-03 10:49:58 +02:00
described_class.run("versioned-cask")
2016-08-18 22:11:42 +03:00
2017-02-08 13:40:31 +01:00
expect(caskroom_path.join(first_installed_version)).not_to exist
expect(caskroom_path).not_to exist
2016-08-18 22:11:42 +03:00
end
it "displays a message when versions remain installed" do
2017-02-08 13:40:31 +01:00
expect {
expect {
2017-10-03 10:49:58 +02:00
described_class.run("versioned-cask")
2017-02-08 13:40:31 +01:00
}.not_to output.to_stderr
}.to output(/#{token} #{first_installed_version} is still installed./).to_stdout
2016-08-18 22:11:42 +03:00
end
end
2020-09-29 23:46:30 +02:00
context "when Casks in Taps have been renamed or removed" do
let(:app) { Cask::Config.new.appdir.join("ive-been-renamed.app") }
2018-09-06 08:29:14 +02:00
let(:caskroom_path) { Cask::Caskroom.path.join("ive-been-renamed").tap(&:mkpath) }
let(:saved_caskfile) {
caskroom_path.join(".metadata", "latest", "timestamp", "Casks").join("ive-been-renamed.rb")
}
2016-08-18 22:11:42 +03:00
before do
app.tap(&:mkpath)
.join("Contents").tap(&:mkpath)
.join("Info.plist").tap(&FileUtils.method(:touch))
caskroom_path.mkpath
saved_caskfile.dirname.mkpath
2018-07-11 15:17:40 +02:00
IO.write saved_caskfile, <<~RUBY
2016-08-18 22:11:42 +03:00
cask 'ive-been-renamed' do
version :latest
app 'ive-been-renamed.app'
end
2018-07-11 15:17:40 +02:00
RUBY
2016-08-18 22:11:42 +03:00
end
2020-09-29 23:46:30 +02:00
it "can still uninstall them" do
2017-10-03 10:49:58 +02:00
described_class.run("ive-been-renamed")
2016-08-18 22:11:42 +03:00
2017-02-08 13:40:31 +01:00
expect(app).not_to exist
expect(caskroom_path).not_to exist
2016-08-18 22:11:42 +03:00
end
end
end