Skip to content

Commit 5f4e42a

Browse files
authored
Merge pull request #20656 from Homebrew/copilot/fix-cask-rename-issue-linux
Fix Cask artifact rename operation on Linux by making xattr metadata no-op
2 parents 852574d + 3e413b4 commit 5f4e42a

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

Library/Homebrew/cask/artifact/relocated.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ def summarize
7979
# Try to make the asset searchable under the target name. Spotlight
8080
# respects this attribute for many filetypes, but ignores it for App
8181
# bundles. Alfred 2.2 respects it even for App bundles.
82-
def add_altname_metadata(file, altname, command: nil)
83-
return if altname.to_s.casecmp(file.basename.to_s).zero?
82+
sig { params(file: Pathname, altname: Pathname, command: T.class_of(SystemCommand)).returns(T.nilable(SystemCommand::Result)) }
83+
def add_altname_metadata(file, altname, command:)
84+
return if altname.to_s.casecmp(file.basename.to_s)&.zero?
8485

8586
odebug "Adding #{ALT_NAME_ATTRIBUTE} metadata"
8687
altnames = command.run("/usr/bin/xattr",
@@ -108,3 +109,5 @@ def printable_target
108109
end
109110
end
110111
end
112+
113+
require "extend/os/cask/artifact/relocated"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
require "extend/os/linux/cask/artifact/relocated" if OS.linux?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module OS
5+
module Linux
6+
module Cask
7+
module Artifact
8+
module Relocated
9+
extend T::Helpers
10+
11+
requires_ancestor { ::Cask::Artifact::Relocated }
12+
13+
sig { params(file: Pathname, altname: Pathname, command: T.class_of(SystemCommand)).returns(T.nilable(SystemCommand::Result)) }
14+
def add_altname_metadata(file, altname, command:)
15+
# no-op on Linux: /usr/bin/xattr for setting extended attributes is not available there.
16+
end
17+
end
18+
end
19+
end
20+
end
21+
end
22+
23+
Cask::Artifact::Relocated.prepend(OS::Linux::Cask::Artifact::Relocated)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
require "cask/artifact/relocated"
4+
5+
RSpec.describe Cask::Artifact::Relocated, :cask do
6+
let(:cask) do
7+
Cask::Cask.new("test-cask") do
8+
url "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip"
9+
homepage "https://brew.sh/"
10+
version "1.0"
11+
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
12+
end
13+
end
14+
15+
let(:command) { NeverSudoSystemCommand }
16+
let(:artifact) { described_class.new(cask, "test_file.txt") }
17+
18+
describe "#add_altname_metadata" do
19+
let(:file) { Pathname("/tmp/test_file.txt") }
20+
let(:altname) { Pathname("alternate_name.txt") }
21+
22+
before do
23+
allow(file).to receive_messages(basename: Pathname("test_file.txt"), writable?: true, realpath: file)
24+
end
25+
26+
context "when running on Linux", :needs_linux do
27+
it "is a no-op and does not call xattr commands" do
28+
expect(command).not_to receive(:run)
29+
expect(command).not_to receive(:run!)
30+
31+
artifact.send(:add_altname_metadata, file, altname, command: command)
32+
end
33+
end
34+
35+
context "when running on macOS", :needs_macos do
36+
before do
37+
stdout_double = instance_double(SystemCommand::Result, stdout: "")
38+
allow(command).to receive(:run).and_return(stdout_double)
39+
allow(command).to receive(:run!)
40+
end
41+
42+
it "calls xattr commands to set metadata" do
43+
expect(command).to receive(:run).with("/usr/bin/xattr",
44+
args: ["-p", "com.apple.metadata:kMDItemAlternateNames", file],
45+
print_stderr: false)
46+
expect(command).to receive(:run!).twice
47+
48+
artifact.send(:add_altname_metadata, file, altname, command: command)
49+
end
50+
end
51+
end
52+
end

0 commit comments

Comments
 (0)