Merge pull request #20580 from Homebrew/fix-write-mkpath-extension-tests

Fix `WriteMkpathExtension` tests
This commit is contained in:
Rylan Polster 2025-08-26 07:32:53 +00:00 committed by GitHub
commit 1ebeb64196
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,16 +2,17 @@
require "extend/pathname/write_mkpath_extension" require "extend/pathname/write_mkpath_extension"
# Use a copy of Pathname with the WriteMkpathExtension prepended to avoid affecting the original class for all tests
class PathnameCopy < Pathname
PathnameCopy.prepend WriteMkpathExtension
end
RSpec.describe WriteMkpathExtension do RSpec.describe WriteMkpathExtension do
let(:file_content) { "sample contents" } let(:file_content) { "sample contents" }
before do
Pathname.prepend described_class
end
it "creates parent directories if they do not exist" do it "creates parent directories if they do not exist" do
mktmpdir do |tmpdir| mktmpdir do |tmpdir|
file = tmpdir/"foo/bar/baz.txt" file = PathnameCopy.new(tmpdir/"foo/bar/baz.txt")
expect(file.dirname).not_to exist expect(file.dirname).not_to exist
file.write(file_content) file.write(file_content)
expect(file).to exist expect(file).to exist
@ -21,7 +22,7 @@ RSpec.describe WriteMkpathExtension do
it "raises if file exists and not in append mode or with offset" do it "raises if file exists and not in append mode or with offset" do
mktmpdir do |tmpdir| mktmpdir do |tmpdir|
file = tmpdir/"file.txt" file = PathnameCopy.new(tmpdir/"file.txt")
file.write(file_content) file.write(file_content)
expect { file.write("new content") }.to raise_error(RuntimeError, /Will not overwrite/) expect { file.write("new content") }.to raise_error(RuntimeError, /Will not overwrite/)
end end
@ -29,7 +30,7 @@ RSpec.describe WriteMkpathExtension do
it "allows overwrite if offset is provided" do it "allows overwrite if offset is provided" do
mktmpdir do |tmpdir| mktmpdir do |tmpdir|
file = tmpdir/"file.txt" file = PathnameCopy.new(tmpdir/"file.txt")
file.write(file_content) file.write(file_content)
expect do expect do
file.write("change", 0) file.write("change", 0)
@ -40,7 +41,7 @@ RSpec.describe WriteMkpathExtension do
it "allows append mode ('a')" do it "allows append mode ('a')" do
mktmpdir do |tmpdir| mktmpdir do |tmpdir|
file = tmpdir/"file.txt" file = PathnameCopy.new(tmpdir/"file.txt")
file.write(file_content) file.write(file_content)
expect do expect do
file.write(" appended", mode: "a") file.write(" appended", mode: "a")
@ -51,7 +52,7 @@ RSpec.describe WriteMkpathExtension do
it "allows append mode ('a+')" do it "allows append mode ('a+')" do
mktmpdir do |tmpdir| mktmpdir do |tmpdir|
file = tmpdir/"file.txt" file = PathnameCopy.new(tmpdir/"file.txt")
file.write(file_content) file.write(file_content)
expect do expect do
file.write(" again", mode: "a+") file.write(" again", mode: "a+")