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 MAXIMUM_STRING_MATCHES = 100
ALLOWABLE_HOMEBREW_REPOSITORY_LINKS = [ ALLOWABLE_HOMEBREW_REPOSITORY_LINKS = [
%r{#{HOMEBREW_LIBRARY}/Homebrew/os/(mac|linux)/pkgconfig}, %r{#{Regexp.escape(HOMEBREW_LIBRARY)}/Homebrew/os/(mac|linux)/pkgconfig},
].freeze ].freeze
module Homebrew module Homebrew

View File

@ -131,10 +131,9 @@ class Keg
old_prefix, new_prefix = relocation.replacement_pair_for(:prefix) old_prefix, new_prefix = relocation.replacement_pair_for(:prefix)
old_cellar, new_cellar = relocation.replacement_pair_for(:cellar) old_cellar, new_cellar = relocation.replacement_pair_for(:cellar)
case old_name if old_name.start_with? old_cellar
when /^#{old_cellar}/
old_name.sub(old_cellar, new_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) old_name.sub(old_prefix, new_prefix)
end end
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 } 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) 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] @replacement_map[key] = [old_value, new_value]
end end
@ -49,15 +49,15 @@ class Keg
any_changed any_changed
end end
sig { params(value: T.any(String, Regexp)).returns(Regexp) } sig { params(path: T.any(String, Regexp)).returns(Regexp) }
def self.path_regex(value) def self.path_to_regex(path)
value = case value path = case path
when String when String
Regexp.escape(value) Regexp.escape(path)
when Regexp when Regexp
value.source path.source
end end
Regexp.new(RELOCATABLE_PATH_REGEX_PREFIX.source + value) Regexp.new(RELOCATABLE_PATH_REGEX_PREFIX.source + path)
end end
end end
@ -244,7 +244,7 @@ class Keg
def self.text_matches_in_file(file, string, ignores, linked_libraries, formula_and_runtime_deps_names) def self.text_matches_in_file(file, string, ignores, linked_libraries, formula_and_runtime_deps_names)
text_matches = [] 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| Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io|
until io.eof? until io.eof?
str = io.readline.chomp str = io.readline.chomp

View File

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