Skip to content

Commit ac2d888

Browse files
CopilotMikeMcQuaid
andcommitted
Simplify keg relocation fix with minimal changes
Co-authored-by: MikeMcQuaid <[email protected]>
1 parent a62d3b5 commit ac2d888

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Library/Homebrew/keg_relocate.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ def new_usr_local_replacement_pairs
128128
}
129129
end
130130

131+
sig { params(file: Pathname).returns(T::Boolean) }
132+
def homebrew_created_file?(file)
133+
basename = file.basename.to_s
134+
basename.start_with?("homebrew.") && %w[.plist .service .timer].include?(file.extname)
135+
end
136+
131137
sig { returns(Relocation) }
132138
def prepare_relocation_to_placeholders
133139
relocation = Relocation.new
@@ -251,7 +257,17 @@ def replace_text_in_files(relocation, files: nil)
251257
regular_files.each do |first, *rest|
252258
s = first.open("rb", &:read)
253259

254-
next unless relocation.replace_text!(s)
260+
# Use full prefix replacement for Homebrew-created files when using selective relocation
261+
current_relocation = if new_usr_local_relocation? && homebrew_created_file?(first)
262+
full_relocation = Relocation.new
263+
full_relocation.add_replacement_pair(:prefix, HOMEBREW_PREFIX.to_s, PREFIX_PLACEHOLDER, path: true)
264+
full_relocation.add_replacement_pair(:cellar, HOMEBREW_CELLAR.to_s, CELLAR_PLACEHOLDER, path: true)
265+
full_relocation
266+
else
267+
relocation
268+
end
269+
270+
next unless current_relocation.replace_text!(s)
255271

256272
changed_files += [first, *rest].map { |file| file.relative_path_from(path) }
257273

Library/Homebrew/test/keg_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# typed: strict
12
# frozen_string_literal: true
23

34
require "keg"
@@ -6,7 +7,7 @@
67
RSpec.describe Keg do
78
include FileUtils
89

9-
def setup_test_keg(name, version)
10+
define_method(:setup_test_keg) do |name, version|
1011
path = HOMEBREW_CELLAR/name/version
1112
(path/"bin").mkpath
1213

@@ -319,6 +320,29 @@ def setup_test_keg(name, version)
319320
end
320321
end
321322

323+
describe "#homebrew_created_file?" do
324+
it "identifies Homebrew service files" do
325+
plist_file = instance_double(Pathname, extname: ".plist", basename: Pathname.new("homebrew.foo.plist"))
326+
service_file = instance_double(Pathname, extname: ".service", basename: Pathname.new("homebrew.foo.service"))
327+
timer_file = instance_double(Pathname, extname: ".timer", basename: Pathname.new("homebrew.foo.timer"))
328+
regular_file = instance_double(Pathname, extname: ".txt", basename: Pathname.new("readme.txt"))
329+
non_homebrew_plist = instance_double(Pathname, extname: ".plist",
330+
basename: Pathname.new("com.example.foo.plist"))
331+
332+
allow(plist_file.basename).to receive(:to_s).and_return("homebrew.foo.plist")
333+
allow(service_file.basename).to receive(:to_s).and_return("homebrew.foo.service")
334+
allow(timer_file.basename).to receive(:to_s).and_return("homebrew.foo.timer")
335+
allow(regular_file.basename).to receive(:to_s).and_return("readme.txt")
336+
allow(non_homebrew_plist.basename).to receive(:to_s).and_return("com.example.foo.plist")
337+
338+
expect(keg.homebrew_created_file?(plist_file)).to be true
339+
expect(keg.homebrew_created_file?(service_file)).to be true
340+
expect(keg.homebrew_created_file?(timer_file)).to be true
341+
expect(keg.homebrew_created_file?(regular_file)).to be false
342+
expect(keg.homebrew_created_file?(non_homebrew_plist)).to be false
343+
end
344+
end
345+
322346
specify "#link and #unlink" do
323347
expect(keg).not_to be_linked
324348
keg.link

0 commit comments

Comments
 (0)