diff --git a/Library/Homebrew/cask/artifact/moved.rb b/Library/Homebrew/cask/artifact/moved.rb index 48861a0f61..ee878de0a5 100644 --- a/Library/Homebrew/cask/artifact/moved.rb +++ b/Library/Homebrew/cask/artifact/moved.rb @@ -94,8 +94,10 @@ module Cask if target.writable? source.children.each { |child| FileUtils.move(child, target + child.basename) } else - command.run!("/bin/cp", args: ["-pR", "#{source}/*", "#{source}/.*", "#{target}/"], - sudo: true) + source.children.each do |child| + command.run!("/bin/cp", args: ["-pR", child, target + child.basename], + sudo: true) + end end Quarantine.copy_xattrs(source, target) source.rmtree diff --git a/Library/Homebrew/test/cask/artifact/app_spec.rb b/Library/Homebrew/test/cask/artifact/app_spec.rb index 796f03f19f..da92098599 100644 --- a/Library/Homebrew/test/cask/artifact/app_spec.rb +++ b/Library/Homebrew/test/cask/artifact/app_spec.rb @@ -334,5 +334,26 @@ describe Cask::Artifact::App, :cask do expect(contents_path).to exist end + + it "properly handles non-writable directories" do + install_phase + + contents_path = target_path.join("Contents") + + allow(target_path).to receive(:writable?).and_return false + allow(command).to receive(:run!).and_call_original + allow(command).to receive(:run!) + .with("/bin/cp", args: ["-pR", source_path.join("Contents"), contents_path], + sudo: true) + .and_wrap_original do |original_method, *args, **kwargs| + original_method.call(*args, sudo_as_root: false, **kwargs) + end + + app.uninstall_phase(command: command, force: force, successor: cask) + expect(contents_path).not_to exist + + app.install_phase(command: command, adopt: adopt, force: force, predecessor: cask) + expect(contents_path).to exist + end end end