From cec3daf6be639b6a5c6a09b39f4747e9d695acbe Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 23 Jul 2023 18:42:22 -0700 Subject: [PATCH] Fix type regression --- Library/Homebrew/test/utils/inreplace_spec.rb | 16 +++++++++++++++- .../Homebrew/utils/string_inreplace_extension.rb | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/utils/inreplace_spec.rb b/Library/Homebrew/test/utils/inreplace_spec.rb index 79217d0840..4283323f2a 100644 --- a/Library/Homebrew/test/utils/inreplace_spec.rb +++ b/Library/Homebrew/test/utils/inreplace_spec.rb @@ -7,7 +7,7 @@ describe Utils::Inreplace do let(:file) { Tempfile.new("test") } before do - file.write <<~EOS + File.binwrite(file, <<~EOS) a b c @@ -52,4 +52,18 @@ describe Utils::Inreplace do end.to raise_error(Utils::Inreplace::Error) end end + + describe "#gsub!" do + it "substitutes pathname within file" do + # For a specific instance of this, see https://github.com/Homebrew/homebrew-core/blob/a8b0b10/Formula/loki.rb#L48 + described_class.inreplace(file.path) do |s| + s.gsub!(Pathname("b"), "f") + end + expect(File.binread(file)).to eq <<~EOS + a + f + c + EOS + end + end end diff --git a/Library/Homebrew/utils/string_inreplace_extension.rb b/Library/Homebrew/utils/string_inreplace_extension.rb index 8c5c49dc10..92fc0ecb43 100644 --- a/Library/Homebrew/utils/string_inreplace_extension.rb +++ b/Library/Homebrew/utils/string_inreplace_extension.rb @@ -31,10 +31,11 @@ class StringInreplaceExtension # # @api public sig { - params(before: T.any(Regexp, String), after: String, audit_result: T::Boolean) + params(before: T.any(Pathname, Regexp, String), after: String, audit_result: T::Boolean) .returns(T.nilable(String)) } def gsub!(before, after, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter + before = before.to_s if before.is_a?(Pathname) result = inreplace_string.gsub!(before, after) errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil? result