Merge pull request #7125 from Bo98/make-regex

extend/string: match multiline make variables
This commit is contained in:
Mike McQuaid 2020-03-05 09:29:52 +00:00 committed by GitHub
commit 42477a1aaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 3 deletions

View File

@ -41,7 +41,7 @@ module StringInreplaceExtension
# Looks for Makefile style variable definitions and replaces the
# value with "new_value", or removes the definition entirely.
def change_make_var!(flag, new_value)
return if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=[ \t]*(.*)$/, "#{flag}=#{new_value}", false)
return if gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=[ \t]*((?:.*\\\n)*.*)$/, "#{flag}=#{new_value}", false)
errors << "expected to change #{flag.inspect} to #{new_value.inspect}"
end
@ -50,7 +50,7 @@ module StringInreplaceExtension
def remove_make_var!(flags)
Array(flags).each do |flag|
# Also remove trailing \n, if present.
unless gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=.*$\n?/, "", false)
unless gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=(?:.*\\\n)*.*$\n?/, "", false)
errors << "expected to remove #{flag.inspect}"
end
end
@ -58,6 +58,6 @@ module StringInreplaceExtension
# Finds the specified variable
def get_make_var(flag)
self[/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=[ \t]*(.*)$/, 1]
self[/^#{Regexp.escape(flag)}[ \t]*[\\?\+\:\!]?=[ \t]*((?:.*\\\n)*.*)$/, 1]
end
end

View File

@ -53,6 +53,24 @@ describe StringInreplaceExtension do
EOS
end
end
context "with newlines" do
let(:string) do
<<~'EOS'
CFLAGS = -Wall -O2 \
-DSOME_VAR=1
LDFLAGS = -lcrypto -lssl
EOS
end
it "is successfully replaced" do
subject.change_make_var! "CFLAGS", "-O3"
expect(subject).to eq <<~EOS
CFLAGS=-O3
LDFLAGS = -lcrypto -lssl
EOS
end
end
end
context "empty flag between other flags" do
@ -146,6 +164,23 @@ describe StringInreplaceExtension do
EOS
end
end
context "with newlines" do
let(:string) do
<<~'EOS'
CFLAGS = -Wall -O2 \
-DSOME_VAR=1
LDFLAGS = -lcrypto -lssl
EOS
end
it "is successfully removed" do
subject.remove_make_var! "CFLAGS"
expect(subject).to eq <<~EOS
LDFLAGS = -lcrypto -lssl
EOS
end
end
end
context "multiple flags" do
@ -194,6 +229,20 @@ describe StringInreplaceExtension do
expect(subject.get_make_var("CFLAGS")).to eq("-Wall -O2")
end
end
context "with newlines" do
let(:string) do
<<~'EOS'
CFLAGS = -Wall -O2 \
-DSOME_VAR=1
LDFLAGS = -lcrypto -lssl
EOS
end
it "extracts the value for a given variable" do
expect(subject.get_make_var("CFLAGS")).to match(/^-Wall -O2 \\\n +-DSOME_VAR=1$/)
end
end
end
describe "#sub!" do