Refactor and document Inreplace.
This commit is contained in:
parent
8e78990176
commit
caa73877c0
@ -280,13 +280,13 @@ describe Utils::Inreplace do
|
|||||||
it "raises error if there are no files given to replace" do
|
it "raises error if there are no files given to replace" do
|
||||||
expect {
|
expect {
|
||||||
described_class.inreplace [], "d", "f"
|
described_class.inreplace [], "d", "f"
|
||||||
}.to raise_error(Utils::InreplaceError)
|
}.to raise_error(Utils::Inreplace::Error)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error if there is nothing to replace" do
|
it "raises error if there is nothing to replace" do
|
||||||
expect {
|
expect {
|
||||||
described_class.inreplace file.path, "d", "f"
|
described_class.inreplace file.path, "d", "f"
|
||||||
}.to raise_error(Utils::InreplaceError)
|
}.to raise_error(Utils::Inreplace::Error)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error if there is nothing to replace in block form" do
|
it "raises error if there is nothing to replace in block form" do
|
||||||
@ -294,7 +294,7 @@ describe Utils::Inreplace do
|
|||||||
described_class.inreplace(file.path) do |s|
|
described_class.inreplace(file.path) do |s|
|
||||||
s.gsub!("d", "f") # rubocop:disable Performance/StringReplacement
|
s.gsub!("d", "f") # rubocop:disable Performance/StringReplacement
|
||||||
end
|
end
|
||||||
}.to raise_error(Utils::InreplaceError)
|
}.to raise_error(Utils::Inreplace::Error)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error if there is no make variables to replace" do
|
it "raises error if there is no make variables to replace" do
|
||||||
@ -303,14 +303,14 @@ describe Utils::Inreplace do
|
|||||||
s.change_make_var! "VAR", "value"
|
s.change_make_var! "VAR", "value"
|
||||||
s.remove_make_var! "VAR2"
|
s.remove_make_var! "VAR2"
|
||||||
end
|
end
|
||||||
}.to raise_error(Utils::InreplaceError)
|
}.to raise_error(Utils::Inreplace::Error)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#inreplace_pairs" do
|
describe "#inreplace_pairs" do
|
||||||
it "raises error if there is no old value" do
|
it "raises error if there is no old value" do
|
||||||
expect {
|
expect {
|
||||||
described_class.inreplace_pairs(file.path, [[nil, "f"]])
|
described_class.inreplace_pairs(file.path, [[nil, "f"]])
|
||||||
}.to raise_error(Utils::InreplaceError)
|
}.to raise_error(Utils::Inreplace::Error)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,7 +1,12 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Utils
|
module Utils
|
||||||
class InreplaceError < RuntimeError
|
# Helper functions for replacing text in files in-place.
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
module Inreplace
|
||||||
|
# Error during replacement.
|
||||||
|
class Error < RuntimeError
|
||||||
def initialize(errors)
|
def initialize(errors)
|
||||||
formatted_errors = errors.reduce(+"inreplace failed\n") do |s, (path, errs)|
|
formatted_errors = errors.reduce(+"inreplace failed\n") do |s, (path, errs)|
|
||||||
s << "#{path}:\n" << errs.map { |e| " #{e}\n" }.join
|
s << "#{path}:\n" << errs.map { |e| " #{e}\n" }.join
|
||||||
@ -10,7 +15,6 @@ module Utils
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Inreplace
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
# Sometimes we have to change a bit before we install. Mostly we
|
# Sometimes we have to change a bit before we install. Mostly we
|
||||||
@ -21,6 +25,8 @@ module Utils
|
|||||||
#
|
#
|
||||||
# `inreplace` supports regular expressions:
|
# `inreplace` supports regular expressions:
|
||||||
# <pre>inreplace "somefile.cfg", /look[for]what?/, "replace by #{bin}/tool"</pre>
|
# <pre>inreplace "somefile.cfg", /look[for]what?/, "replace by #{bin}/tool"</pre>
|
||||||
|
#
|
||||||
|
# @api public
|
||||||
def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
|
def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
@ -42,7 +48,7 @@ module Utils
|
|||||||
Pathname(path).atomic_write(s.inreplace_string)
|
Pathname(path).atomic_write(s.inreplace_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
raise InreplaceError, errors unless errors.empty?
|
raise Error, errors unless errors.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false)
|
def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false)
|
||||||
@ -57,7 +63,7 @@ module Utils
|
|||||||
|
|
||||||
contents.gsub!(old, new)
|
contents.gsub!(old, new)
|
||||||
end
|
end
|
||||||
raise InreplaceError, path => contents.errors unless contents.errors.empty?
|
raise Error, path => contents.errors unless contents.errors.empty?
|
||||||
|
|
||||||
Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run
|
Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run
|
||||||
contents.inreplace_string
|
contents.inreplace_string
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user