diff --git a/Library/Homebrew/cask/artifact/abstract_artifact.rb b/Library/Homebrew/cask/artifact/abstract_artifact.rb index 9d6e5d1381..5c1abb3ffd 100644 --- a/Library/Homebrew/cask/artifact/abstract_artifact.rb +++ b/Library/Homebrew/cask/artifact/abstract_artifact.rb @@ -30,6 +30,7 @@ module Cask def staged_path_join_executable(path) path = Pathname(path) + path = path.expand_path if path.to_s.start_with?("~") absolute_path = if path.absolute? path diff --git a/Library/Homebrew/test/cask/cmd/uninstall_spec.rb b/Library/Homebrew/test/cask/cmd/uninstall_spec.rb index 8af07bd9b7..3b7868d189 100644 --- a/Library/Homebrew/test/cask/cmd/uninstall_spec.rb +++ b/Library/Homebrew/test/cask/cmd/uninstall_spec.rb @@ -90,6 +90,30 @@ describe Cask::Cmd::Uninstall, :cask do expect(cask).not_to be_installed end + context "when Casks use script path with `~` as `HOME`" do + let(:home_dir) { mktmpdir } + let(:app) { Pathname.new("#{home_dir}/MyFancyApp.app") } + let(:cask) { Cask::CaskLoader.load(cask_path("with-uninstall-script-user-relative")) } + + before do + ENV["HOME"] = home_dir + end + + it "can still uninstall them" do + Cask::Installer.new(cask).install + + expect(cask).to be_installed + expect(app).to exist + + expect { + described_class.run("with-uninstall-script-user-relative") + }.not_to raise_error + + expect(cask).not_to be_installed + expect(app).not_to exist + end + end + describe "when multiple versions of a cask are installed" do let(:token) { "versioned-cask" } let(:first_installed_version) { "1.2.3" } diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-script-user-relative.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-script-user-relative.rb new file mode 100644 index 0000000000..fbe786abb3 --- /dev/null +++ b/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-script-user-relative.rb @@ -0,0 +1,21 @@ +cask "with-uninstall-script-user-relative" do + version "1.2.3" + sha256 "5633c3a0f2e572cbf021507dec78c50998b398c343232bdfc7e26221d0a5db4d" + + url "file://#{TEST_FIXTURE_DIR}/cask/MyFancyApp.zip" + homepage "https://brew.sh/MyFancyApp" + + app "MyFancyApp/MyFancyApp.app", target: "~/MyFancyApp.app" + + postflight do + IO.write "#{ENV["HOME"]}/MyFancyApp.app/uninstall.sh", <<~SH + #!/bin/sh + /bin/rm -r "#{ENV["HOME"]}/MyFancyApp.app" + SH + end + + uninstall script: { + executable: "~/MyFancyApp.app/uninstall.sh", + sudo: false, + } +end