brew/Library/Homebrew/test/keg_relocate/relocation_spec.rb

68 lines
2.5 KiB
Ruby
Raw Normal View History

2021-05-11 00:10:06 -04:00
# frozen_string_literal: true
require "keg_relocate"
describe Keg::Relocation do
2021-05-11 10:46:23 -04:00
let(:prefix) { HOMEBREW_PREFIX.to_s }
let(:cellar) { HOMEBREW_CELLAR.to_s }
let(:repository) { HOMEBREW_REPOSITORY.to_s }
let(:library) { HOMEBREW_LIBRARY.to_s }
2021-05-11 00:10:06 -04:00
let(:prefix_placeholder) { "@@HOMEBREW_PREFIX@@" }
let(:cellar_placeholder) { "@@HOMEBREW_CELLAR@@" }
2021-05-11 10:46:23 -04:00
let(:repository_placeholder) { "@@HOMEBREW_REPOSITORY@@" }
let(:library_placeholder) { "@@HOMEBREW_LIBRARY@@" }
2021-11-08 20:18:25 -05:00
let(:escaped_prefix) { /(?:(?<=-F|-I|-L|-isystem)|(?<![a-zA-Z0-9]))#{Regexp.escape(HOMEBREW_PREFIX)}/o }
let(:escaped_cellar) { /(?:(?<=-F|-I|-L|-isystem)|(?<![a-zA-Z0-9]))#{HOMEBREW_CELLAR}/o }
2021-05-11 00:10:06 -04:00
2021-05-11 10:46:23 -04:00
def setup_relocation
2021-05-11 00:10:06 -04:00
relocation = described_class.new
2021-05-11 10:46:23 -04:00
relocation.add_replacement_pair :prefix, prefix, prefix_placeholder, path: true
relocation.add_replacement_pair :cellar, /#{cellar}/o, cellar_placeholder, path: true
relocation.add_replacement_pair :repository_placeholder, repository_placeholder, repository
relocation.add_replacement_pair :library_placeholder, library_placeholder, library
relocation
end
specify "#add_replacement_pair" do
relocation = setup_relocation
2021-05-11 00:10:06 -04:00
expect(relocation.replacement_pair_for(:prefix)).to eq [escaped_prefix, prefix_placeholder]
expect(relocation.replacement_pair_for(:cellar)).to eq [escaped_cellar, cellar_placeholder]
2021-05-11 10:46:23 -04:00
expect(relocation.replacement_pair_for(:repository_placeholder)).to eq [repository_placeholder, repository]
expect(relocation.replacement_pair_for(:library_placeholder)).to eq [library_placeholder, library]
2021-05-11 00:10:06 -04:00
end
specify "#replace_text" do
2021-05-11 10:46:23 -04:00
relocation = setup_relocation
2021-05-11 00:10:06 -04:00
text = +"foo"
relocation.replace_text(text)
expect(text).to eq "foo"
text = +<<~TEXT
#{prefix}/foo
#{cellar}/foo
foo#{prefix}/bar
foo#{cellar}/bar
2021-05-11 10:46:23 -04:00
#{repository_placeholder}/foo
foo#{library_placeholder}/bar
2021-05-11 00:10:06 -04:00
TEXT
relocation.replace_text(text)
expect(text).to eq <<~REPLACED
#{prefix_placeholder}/foo
#{cellar_placeholder}/foo
foo#{prefix}/bar
foo#{cellar}/bar
2021-05-11 10:46:23 -04:00
#{repository}/foo
foo#{library}/bar
2021-05-11 00:10:06 -04:00
REPLACED
end
specify "::path_to_regex" do
expect(described_class.path_to_regex(prefix)).to eq escaped_prefix
2021-11-08 20:18:25 -05:00
expect(described_class.path_to_regex("foo.bar")).to eq(/(?:(?<=-F|-I|-L|-isystem)|(?<![a-zA-Z0-9]))foo\.bar/)
expect(described_class.path_to_regex(/#{cellar}/o)).to eq escaped_cellar
2021-11-08 20:18:25 -05:00
expect(described_class.path_to_regex(/foo.bar/)).to eq(/(?:(?<=-F|-I|-L|-isystem)|(?<![a-zA-Z0-9]))foo.bar/)
2021-05-11 00:10:06 -04:00
end
end