From d0896d641ad1d27c2d3396087f3c27076027d116 Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Thu, 5 Mar 2020 03:40:23 +0000 Subject: [PATCH] extend/string: match multiline make variables --- Library/Homebrew/extend/string.rb | 6 +-- Library/Homebrew/test/inreplace_spec.rb | 49 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/extend/string.rb b/Library/Homebrew/extend/string.rb index fae54878e4..a0dcb38776 100644 --- a/Library/Homebrew/extend/string.rb +++ b/Library/Homebrew/extend/string.rb @@ -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 diff --git a/Library/Homebrew/test/inreplace_spec.rb b/Library/Homebrew/test/inreplace_spec.rb index 7b5901b94a..965cd0035b 100644 --- a/Library/Homebrew/test/inreplace_spec.rb +++ b/Library/Homebrew/test/inreplace_spec.rb @@ -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