From 875282bc06580e3810fda049a0db8502e47b666c Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Tue, 11 May 2021 11:38:26 -0400 Subject: [PATCH] Add more keg relocation tests --- Library/Homebrew/keg_relocate.rb | 3 +- .../relocation_spec.rb} | 0 .../Homebrew/test/keg_relocate/text_spec.rb | 83 +++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) rename Library/Homebrew/test/{keg_relocate_spec.rb => keg_relocate/relocation_spec.rb} (100%) create mode 100644 Library/Homebrew/test/keg_relocate/text_spec.rb diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index 37aa2f8e0d..4d30c4c149 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -248,12 +248,11 @@ class Keg def self.text_matches_in_file(file, string, ignores, linked_libraries, formula_and_runtime_deps_names) text_matches = [] + path_regex = Relocation.path_regex(string) Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io| until io.eof? str = io.readline.chomp next if ignores.any? { |i| i =~ str } - - path_regex = Relocation.path_regex(string) next unless str.match? path_regex offset, match = str.split(" ", 2) diff --git a/Library/Homebrew/test/keg_relocate_spec.rb b/Library/Homebrew/test/keg_relocate/relocation_spec.rb similarity index 100% rename from Library/Homebrew/test/keg_relocate_spec.rb rename to Library/Homebrew/test/keg_relocate/relocation_spec.rb diff --git a/Library/Homebrew/test/keg_relocate/text_spec.rb b/Library/Homebrew/test/keg_relocate/text_spec.rb new file mode 100644 index 0000000000..71e5ccdd6e --- /dev/null +++ b/Library/Homebrew/test/keg_relocate/text_spec.rb @@ -0,0 +1,83 @@ +# typed: false +# frozen_string_literal: true + +require "keg_relocate" + +describe Keg do + subject(:keg) { described_class.new(HOMEBREW_CELLAR/"foo/1.0.0") } + + let(:dir) { mktmpdir } + let(:file) { dir/"file.txt" } + let(:placeholder) { "@@PLACEHOLDER@@" } + + before do + (HOMEBREW_CELLAR/"foo/1.0.0").mkpath + end + + def setup_file(placeholders: false) + path = placeholders ? placeholder : dir + file.atomic_write <<~EOS + #{path}/file.txt + /foo#{path}/file.txt + foo/bar:#{path}/file.txt + foo/bar:/foo#{path}/file.txt + #{path}/bar.txt:#{path}/baz.txt + EOS + end + + def setup_relocation(placeholders: false) + relocation = described_class::Relocation.new + + if placeholders + relocation.add_replacement_pair :dir, placeholder, dir.to_s + else + relocation.add_replacement_pair :dir, dir.to_s, placeholder, path: true + end + + relocation + end + + specify "::text_matches_in_file" do + setup_file + + result = described_class.text_matches_in_file(file, placeholder, [], [], nil) + expect(result.count).to eq 0 + + result = described_class.text_matches_in_file(file, dir.to_s, [], [], nil) + expect(result.count).to eq 2 + end + + describe "#replace_text_in_files" do + specify "with paths" do + setup_file + relocation = setup_relocation + + keg.replace_text_in_files(relocation, files: [file]) + contents = File.read file + + expect(contents).to eq <<~EOS + #{placeholder}/file.txt + /foo#{dir}/file.txt + foo/bar:#{placeholder}/file.txt + foo/bar:/foo#{dir}/file.txt + #{placeholder}/bar.txt:#{placeholder}/baz.txt + EOS + end + + specify "with placeholders" do + setup_file placeholders: true + relocation = setup_relocation placeholders: true + + keg.replace_text_in_files(relocation, files: [file]) + contents = File.read file + + expect(contents).to eq <<~EOS + #{dir}/file.txt + /foo#{dir}/file.txt + foo/bar:#{dir}/file.txt + foo/bar:/foo#{dir}/file.txt + #{dir}/bar.txt:#{dir}/baz.txt + EOS + end + end +end