Fix Cask artifact rename operation on Linux by making xattr metadata no-op

Co-authored-by: MikeMcQuaid <125011+MikeMcQuaid@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-09-12 09:30:17 +01:00 committed by Mike McQuaid
parent 8ef7a9dbd4
commit 3e413b4521
No known key found for this signature in database
4 changed files with 84 additions and 2 deletions

View File

@ -79,8 +79,9 @@ module Cask
# Try to make the asset searchable under the target name. Spotlight # Try to make the asset searchable under the target name. Spotlight
# respects this attribute for many filetypes, but ignores it for App # respects this attribute for many filetypes, but ignores it for App
# bundles. Alfred 2.2 respects it even for App bundles. # bundles. Alfred 2.2 respects it even for App bundles.
def add_altname_metadata(file, altname, command: nil) sig { params(file: Pathname, altname: Pathname, command: T.class_of(SystemCommand)).returns(T.nilable(SystemCommand::Result)) }
return if altname.to_s.casecmp(file.basename.to_s).zero? def add_altname_metadata(file, altname, command:)
return if altname.to_s.casecmp(file.basename.to_s)&.zero?
odebug "Adding #{ALT_NAME_ATTRIBUTE} metadata" odebug "Adding #{ALT_NAME_ATTRIBUTE} metadata"
altnames = command.run("/usr/bin/xattr", altnames = command.run("/usr/bin/xattr",
@ -108,3 +109,5 @@ module Cask
end end
end end
end end
require "extend/os/cask/artifact/relocated"

View File

@ -0,0 +1,4 @@
# typed: strict
# frozen_string_literal: true
require "extend/os/linux/cask/artifact/relocated" if OS.linux?

View File

@ -0,0 +1,23 @@
# typed: strict
# frozen_string_literal: true
module OS
module Linux
module Cask
module Artifact
module Relocated
extend T::Helpers
requires_ancestor { ::Cask::Artifact::Relocated }
sig { params(file: Pathname, altname: Pathname, command: T.class_of(SystemCommand)).returns(T.nilable(SystemCommand::Result)) }
def add_altname_metadata(file, altname, command:)
# no-op on Linux: /usr/bin/xattr for setting extended attributes is not available there.
end
end
end
end
end
end
Cask::Artifact::Relocated.prepend(OS::Linux::Cask::Artifact::Relocated)

View File

@ -0,0 +1,52 @@
# frozen_string_literal: true
require "cask/artifact/relocated"
RSpec.describe Cask::Artifact::Relocated, :cask do
let(:cask) do
Cask::Cask.new("test-cask") do
url "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip"
homepage "https://brew.sh/"
version "1.0"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
end
end
let(:command) { NeverSudoSystemCommand }
let(:artifact) { described_class.new(cask, "test_file.txt") }
describe "#add_altname_metadata" do
let(:file) { Pathname("/tmp/test_file.txt") }
let(:altname) { Pathname("alternate_name.txt") }
before do
allow(file).to receive_messages(basename: Pathname("test_file.txt"), writable?: true, realpath: file)
end
context "when running on Linux", :needs_linux do
it "is a no-op and does not call xattr commands" do
expect(command).not_to receive(:run)
expect(command).not_to receive(:run!)
artifact.send(:add_altname_metadata, file, altname, command: command)
end
end
context "when running on macOS", :needs_macos do
before do
stdout_double = instance_double(SystemCommand::Result, stdout: "")
allow(command).to receive(:run).and_return(stdout_double)
allow(command).to receive(:run!)
end
it "calls xattr commands to set metadata" do
expect(command).to receive(:run).with("/usr/bin/xattr",
args: ["-p", "com.apple.metadata:kMDItemAlternateNames", file],
print_stderr: false)
expect(command).to receive(:run!).twice
artifact.send(:add_altname_metadata, file, altname, command: command)
end
end
end
end