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:
		
							parent
							
								
									8ef7a9dbd4
								
							
						
					
					
						commit
						3e413b4521
					
				@ -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"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								Library/Homebrew/extend/os/cask/artifact/relocated.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Library/Homebrew/extend/os/cask/artifact/relocated.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
# typed: strict
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require "extend/os/linux/cask/artifact/relocated" if OS.linux?
 | 
			
		||||
							
								
								
									
										23
									
								
								Library/Homebrew/extend/os/linux/cask/artifact/relocated.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Library/Homebrew/extend/os/linux/cask/artifact/relocated.rb
									
									
									
									
									
										Normal 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)
 | 
			
		||||
							
								
								
									
										52
									
								
								Library/Homebrew/test/cask/artifact/relocated_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								Library/Homebrew/test/cask/artifact/relocated_spec.rb
									
									
									
									
									
										Normal 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
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user