diff --git a/Library/Homebrew/cask/artifact/relocated.rb b/Library/Homebrew/cask/artifact/relocated.rb index e33c6431b2..95600a7059 100644 --- a/Library/Homebrew/cask/artifact/relocated.rb +++ b/Library/Homebrew/cask/artifact/relocated.rb @@ -79,8 +79,9 @@ module Cask # Try to make the asset searchable under the target name. Spotlight # respects this attribute for many filetypes, but ignores it for App # bundles. Alfred 2.2 respects it even for App bundles. - def add_altname_metadata(file, altname, command: nil) - return if altname.to_s.casecmp(file.basename.to_s).zero? + sig { params(file: Pathname, altname: Pathname, command: T.class_of(SystemCommand)).returns(T.nilable(SystemCommand::Result)) } + def add_altname_metadata(file, altname, command:) + return if altname.to_s.casecmp(file.basename.to_s)&.zero? odebug "Adding #{ALT_NAME_ATTRIBUTE} metadata" altnames = command.run("/usr/bin/xattr", @@ -108,3 +109,5 @@ module Cask end end end + +require "extend/os/cask/artifact/relocated" diff --git a/Library/Homebrew/extend/os/cask/artifact/relocated.rb b/Library/Homebrew/extend/os/cask/artifact/relocated.rb new file mode 100644 index 0000000000..7d324a42eb --- /dev/null +++ b/Library/Homebrew/extend/os/cask/artifact/relocated.rb @@ -0,0 +1,4 @@ +# typed: strict +# frozen_string_literal: true + +require "extend/os/linux/cask/artifact/relocated" if OS.linux? diff --git a/Library/Homebrew/extend/os/linux/cask/artifact/relocated.rb b/Library/Homebrew/extend/os/linux/cask/artifact/relocated.rb new file mode 100644 index 0000000000..768cd30d77 --- /dev/null +++ b/Library/Homebrew/extend/os/linux/cask/artifact/relocated.rb @@ -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) diff --git a/Library/Homebrew/test/cask/artifact/relocated_spec.rb b/Library/Homebrew/test/cask/artifact/relocated_spec.rb new file mode 100644 index 0000000000..3f317ab34b --- /dev/null +++ b/Library/Homebrew/test/cask/artifact/relocated_spec.rb @@ -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