Add more keg relocation tests

This commit is contained in:
Rylan Polster 2021-05-11 11:38:26 -04:00
parent f13f7b9326
commit 875282bc06
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
3 changed files with 84 additions and 2 deletions

View File

@ -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)

View File

@ -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