forked from Homebrew/brew
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
StringInreplaceExtension
into separate file.
- Loading branch information
1 parent
24ae318
commit 318091c
Showing
7 changed files
with
180 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,4 @@ | ||
# typed: false | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "active_support/core_ext/object/blank" | ||
|
||
# Used by the inreplace function (in `utils.rb`). | ||
class StringInreplaceExtension | ||
attr_accessor :errors, :inreplace_string | ||
|
||
def initialize(str) | ||
@inreplace_string = str | ||
@errors = [] | ||
end | ||
|
||
def self.extended(str) | ||
str.errors = [] | ||
end | ||
|
||
def sub!(before, after) | ||
result = inreplace_string.sub!(before, after) | ||
errors << "expected replacement of #{before.inspect} with #{after.inspect}" unless result | ||
result | ||
end | ||
|
||
# Warn if nothing was replaced | ||
def gsub!(before, after, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter | ||
result = inreplace_string.gsub!(before, after) | ||
errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil? | ||
result | ||
end | ||
|
||
# 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]*((?:.*\\\n)*.*)$/, "#{flag}=#{new_value}", false) | ||
|
||
errors << "expected to change #{flag.inspect} to #{new_value.inspect}" | ||
end | ||
|
||
# Removes variable assignments completely. | ||
def remove_make_var!(flags) | ||
Array(flags).each do |flag| | ||
# Also remove trailing \n, if present. | ||
unless gsub!(/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=(?:.*\\\n)*.*$\n?/, "", false) | ||
errors << "expected to remove #{flag.inspect}" | ||
end | ||
end | ||
end | ||
|
||
# Finds the specified variable | ||
def get_make_var(flag) | ||
inreplace_string[/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, 1] | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "tempfile" | ||
require "utils/inreplace" | ||
|
||
describe Utils::Inreplace do | ||
let(:file) { Tempfile.new("test") } | ||
|
||
before do | ||
file.write <<~EOS | ||
a | ||
b | ||
c | ||
EOS | ||
end | ||
|
||
after { file.unlink } | ||
|
||
it "raises error if there are no files given to replace" do | ||
expect { | ||
described_class.inreplace [], "d", "f" | ||
}.to raise_error(Utils::Inreplace::Error) | ||
end | ||
|
||
it "raises error if there is nothing to replace" do | ||
expect { | ||
described_class.inreplace file.path, "d", "f" | ||
}.to raise_error(Utils::Inreplace::Error) | ||
end | ||
|
||
it "raises error if there is nothing to replace in block form" do | ||
expect { | ||
described_class.inreplace(file.path) do |s| | ||
s.gsub!("d", "f") # rubocop:disable Performance/StringReplacement | ||
end | ||
}.to raise_error(Utils::Inreplace::Error) | ||
end | ||
|
||
it "raises error if there is no make variables to replace" do | ||
expect { | ||
described_class.inreplace(file.path) do |s| | ||
s.change_make_var! "VAR", "value" | ||
s.remove_make_var! "VAR2" | ||
end | ||
}.to raise_error(Utils::Inreplace::Error) | ||
end | ||
|
||
describe "#inreplace_pairs" do | ||
it "raises error if there is no old value" do | ||
expect { | ||
described_class.inreplace_pairs(file.path, [[nil, "f"]]) | ||
}.to raise_error(Utils::Inreplace::Error) | ||
end | ||
end | ||
end |
Oops, something went wrong.