Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Library/Homebrew/keg_relocate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ def new_usr_local_replacement_pairs
}
end

sig { returns(Relocation) }
def prepare_relocation_to_placeholders
sig { params(new_usr_local_relocation: T::Boolean).returns(Relocation) }
def prepare_relocation_to_placeholders(new_usr_local_relocation: new_usr_local_relocation?)
relocation = Relocation.new

# Use selective HOMEBREW_PREFIX replacement when HOMEBREW_PREFIX=/usr/local
# This avoids overzealous replacement of system paths when a script refers to e.g. /usr/local/bin
if new_usr_local_relocation?
if new_usr_local_relocation
new_usr_local_replacement_pairs.each do |key, value|
relocation.add_replacement_pair(key, value.fetch(:old), value.fetch(:new), path: true)
end
Expand Down Expand Up @@ -195,6 +195,13 @@ def openjdk_dep_name_if_applicable
dep_names.find { |d| d.match? Version.formula_optionally_versioned_regex(:openjdk) }
end

sig { params(file: Pathname).returns(T::Boolean) }
def homebrew_created_file?(file)
return false unless file.basename.to_s.start_with?("homebrew.")

%w[.plist .service .timer].include?(file.extname)
end

sig { params(relocation: Relocation, files: T.nilable(T::Array[Pathname])).returns(T::Array[Pathname]) }
def replace_text_in_files(relocation, files: nil)
files ||= text_files | libtool_files
Expand All @@ -203,7 +210,13 @@ def replace_text_in_files(relocation, files: nil)
files.map { path.join(_1) }.group_by { |f| f.stat.ino }.each_value do |first, *rest|
s = first.open("rb", &:read)

next unless relocation.replace_text!(s)
# Use full prefix replacement for Homebrew-created files when using selective relocation
file_relocation = if new_usr_local_relocation? && homebrew_created_file?(first)
prepare_relocation_to_placeholders(new_usr_local_relocation: false)
else
relocation
end
next unless file_relocation.replace_text!(s)

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

Expand Down
23 changes: 23 additions & 0 deletions Library/Homebrew/test/keg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,29 @@ def setup_test_keg(name, version)
end
end

describe "#homebrew_created_file?" do
it "identifies Homebrew service files" do
plist_file = instance_double(Pathname, extname: ".plist", basename: Pathname.new("homebrew.foo.plist"))
service_file = instance_double(Pathname, extname: ".service", basename: Pathname.new("homebrew.foo.service"))
timer_file = instance_double(Pathname, extname: ".timer", basename: Pathname.new("homebrew.foo.timer"))
regular_file = instance_double(Pathname, extname: ".txt", basename: Pathname.new("readme.txt"))
non_homebrew_plist = instance_double(Pathname, extname: ".plist",
basename: Pathname.new("com.example.foo.plist"))

allow(plist_file.basename).to receive(:to_s).and_return("homebrew.foo.plist")
allow(service_file.basename).to receive(:to_s).and_return("homebrew.foo.service")
allow(timer_file.basename).to receive(:to_s).and_return("homebrew.foo.timer")
allow(regular_file.basename).to receive(:to_s).and_return("readme.txt")
allow(non_homebrew_plist.basename).to receive(:to_s).and_return("com.example.foo.plist")

expect(keg.homebrew_created_file?(plist_file)).to be true
expect(keg.homebrew_created_file?(service_file)).to be true
expect(keg.homebrew_created_file?(timer_file)).to be true
expect(keg.homebrew_created_file?(regular_file)).to be false
expect(keg.homebrew_created_file?(non_homebrew_plist)).to be false
end
end

specify "#link and #unlink" do
expect(keg).not_to be_linked
keg.link
Expand Down