Cleanup using suggestions from code review

This commit is contained in:
Rylan Polster 2021-05-13 11:02:39 -04:00
parent 309e9e1c52
commit 4457dc904b
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
4 changed files with 16 additions and 17 deletions

View File

@ -32,7 +32,7 @@ EOS
MAXIMUM_STRING_MATCHES = 100
ALLOWABLE_HOMEBREW_REPOSITORY_LINKS = [
%r{#{HOMEBREW_LIBRARY}/Homebrew/os/(mac|linux)/pkgconfig},
%r{#{Regexp.escape(HOMEBREW_LIBRARY)}/Homebrew/os/(mac|linux)/pkgconfig},
].freeze
module Homebrew

View File

@ -131,10 +131,9 @@ class Keg
old_prefix, new_prefix = relocation.replacement_pair_for(:prefix)
old_cellar, new_cellar = relocation.replacement_pair_for(:cellar)
case old_name
when /^#{old_cellar}/
if old_name.start_with? old_cellar
old_name.sub(old_cellar, new_cellar)
when /^#{old_prefix}/
elsif old_name.start_with? old_prefix
old_name.sub(old_prefix, new_prefix)
end
end

View File

@ -24,7 +24,7 @@ class Keg
sig { params(key: Symbol, old_value: T.any(String, Regexp), new_value: String, path: T::Boolean).void }
def add_replacement_pair(key, old_value, new_value, path: false)
old_value = self.class.path_regex(old_value) if path
old_value = self.class.path_to_regex(old_value) if path
@replacement_map[key] = [old_value, new_value]
end
@ -49,15 +49,15 @@ class Keg
any_changed
end
sig { params(value: T.any(String, Regexp)).returns(Regexp) }
def self.path_regex(value)
value = case value
sig { params(path: T.any(String, Regexp)).returns(Regexp) }
def self.path_to_regex(path)
path = case path
when String
Regexp.escape(value)
Regexp.escape(path)
when Regexp
value.source
path.source
end
Regexp.new(RELOCATABLE_PATH_REGEX_PREFIX.source + value)
Regexp.new(RELOCATABLE_PATH_REGEX_PREFIX.source + path)
end
end
@ -244,7 +244,7 @@ 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)
path_regex = Relocation.path_to_regex(string)
Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io|
until io.eof?
str = io.readline.chomp

View File

@ -59,10 +59,10 @@ describe Keg::Relocation do
REPLACED
end
specify "::path_regex" do
expect(described_class.path_regex(prefix)).to eq escaped_prefix
expect(described_class.path_regex("foo.bar")).to eq(/(?<![a-zA-Z0-9])foo\.bar/)
expect(described_class.path_regex(/#{cellar}/o)).to eq escaped_cellar
expect(described_class.path_regex(/foo.bar/)).to eq(/(?<![a-zA-Z0-9])foo.bar/)
specify "::path_to_regex" do
expect(described_class.path_to_regex(prefix)).to eq escaped_prefix
expect(described_class.path_to_regex("foo.bar")).to eq(/(?<![a-zA-Z0-9])foo\.bar/)
expect(described_class.path_to_regex(/#{cellar}/o)).to eq escaped_cellar
expect(described_class.path_to_regex(/foo.bar/)).to eq(/(?<![a-zA-Z0-9])foo.bar/)
end
end