extend/pathname: limit write override to a refinement

This commit is contained in:
Bo Anderson 2021-05-03 13:24:32 +01:00
parent a81a76b196
commit f174d4363f
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
11 changed files with 36 additions and 30 deletions

View File

@ -171,6 +171,7 @@ class Build
interactive_shell(formula) interactive_shell(formula)
else else
formula.prefix.mkpath formula.prefix.mkpath
formula.logs.mkpath
(formula.logs/"00.options.out").write \ (formula.logs/"00.options.out").write \
"#{formula.full_name} #{formula.build.used_options.sort.join(" ")}".strip "#{formula.full_name} #{formula.build.used_options.sort.join(" ")}".strip

View File

@ -215,6 +215,7 @@ module Homebrew
path.delete path.delete
end end
ohai "Writing formula for #{name} from revision #{rev} to:", path ohai "Writing formula for #{name} from revision #{rev} to:", path
path.dirname.mkpath
path.write result path.write result
end end

View File

@ -165,19 +165,6 @@ class Pathname
end end
private :install_symlink_p private :install_symlink_p
# @private
alias old_write write
# We assume this pathname object is a file, obviously.
sig { params(content: String, offset: Integer, open_args: T::Hash[Symbol, T.untyped]).returns(Integer) }
def write(content, offset = 0, open_args = {})
raise "Will not overwrite #{self}" if exist?
dirname.mkpath
old_write(content, offset, open_args)
end
# Only appends to a file that is already created. # Only appends to a file that is already created.
sig { params(content: String, open_args: T.untyped).void } sig { params(content: String, open_args: T.untyped).void }
def append_lines(content, **open_args) def append_lines(content, **open_args)
@ -420,6 +407,7 @@ class Pathname
).returns(Integer) ).returns(Integer)
} }
def write_jar_script(target_jar, script_name, java_opts = "", java_version: nil) def write_jar_script(target_jar, script_name, java_opts = "", java_version: nil)
mkpath
(self/script_name).write <<~EOS (self/script_name).write <<~EOS
#!/bin/bash #!/bin/bash
export JAVA_HOME="#{Language::Java.overridable_java_home_env(java_version)[:JAVA_HOME]}" export JAVA_HOME="#{Language::Java.overridable_java_home_env(java_version)[:JAVA_HOME]}"

View File

@ -87,6 +87,7 @@ module Homebrew
end end
end end
path.dirname.mkpath
path.write ERB.new(template, trim_mode: ">").result(binding) path.write ERB.new(template, trim_mode: ">").result(binding)
end end

View File

@ -47,6 +47,20 @@ module Formulary
super super
end end
# @private
module PathnameWriteMkpath
refine Pathname do
def write(content, offset = nil, **open_args)
raise "Will not overwrite #{self}" if exist? && !offset && !open_args[:mode]&.match?(/^a\+?$/)
dirname.mkpath
super
end
end
end
using PathnameWriteMkpath
def self.load_formula(name, path, contents, namespace, flags:, ignore_errors:) def self.load_formula(name, path, contents, namespace, flags:, ignore_errors:)
raise "Formula loading disabled by HOMEBREW_DISABLE_LOAD_FORMULA!" if Homebrew::EnvConfig.disable_load_formula? raise "Formula loading disabled by HOMEBREW_DISABLE_LOAD_FORMULA!" if Homebrew::EnvConfig.disable_load_formula?

View File

@ -27,11 +27,16 @@ describe Homebrew::Completions do
context "when linking or unlinking completions" do context "when linking or unlinking completions" do
def setup_completions(external:) def setup_completions(external:)
(internal_path/"completions/bash/foo_internal").write "#foo completions" internal_bash_completion = internal_path/"completions/bash"
external_bash_completion = external_path/"completions/bash"
internal_bash_completion.mkpath
(internal_bash_completion/"foo_internal").write "#foo completions"
if external if external
(external_path/"completions/bash/foo_external").write "#foo completions" external_bash_completion.mkpath
elsif (external_path/"completions/bash/foo_external").exist? (external_bash_completion/"foo_external").write "#foo completions"
(external_path/"completions/bash/foo_external").delete elsif (external_bash_completion/"foo_external").exist?
(external_bash_completion/"foo_external").delete
end end
end end

View File

@ -833,6 +833,7 @@ module Homebrew
let(:formula_path) { tap_path/formula_subpath } let(:formula_path) { tap_path/formula_subpath }
before do before do
origin_formula_path.dirname.mkpath
origin_formula_path.write <<~RUBY origin_formula_path.write <<~RUBY
class Foo#{foo_version} < Formula class Foo#{foo_version} < Formula
url "https://brew.sh/foo-1.0.tar.gz" url "https://brew.sh/foo-1.0.tar.gz"

View File

@ -57,6 +57,7 @@ describe Formulary do
describe "::factory" do describe "::factory" do
before do before do
formula_path.dirname.mkpath
formula_path.write formula_content formula_path.write formula_content
end end
@ -194,6 +195,7 @@ describe Formulary do
end end
it "raises an error if a Formula is in multiple Taps" do it "raises an error if a Formula is in multiple Taps" do
another_tap.path.mkpath
(another_tap.path/"#{formula_name}.rb").write formula_content (another_tap.path/"#{formula_name}.rb").write formula_content
expect { expect {

View File

@ -63,19 +63,6 @@ describe Pathname do
end end
end end
describe "#write" do
it "creates a file and writes to it" do
expect(file).not_to exist
file.write("CONTENT")
expect(File.read(file)).to eq("CONTENT")
end
it "raises an error if the file already exists" do
touch file
expect { file.write("CONTENT") }.to raise_error(RuntimeError)
end
end
describe "#append_lines" do describe "#append_lines" do
it "appends lines to a file" do it "appends lines to a file" do
touch file touch file

View File

@ -348,6 +348,7 @@ describe Tab do
end end
it "can create a Tab for a Formula with an outdated Kegs" do it "can create a Tab for a Formula with an outdated Kegs" do
f.prefix.mkpath
f_tab_path.write f_tab_content f_tab_path.write f_tab_content
f2 = formula { url "foo-2.0" } f2 = formula { url "foo-2.0" }

View File

@ -23,6 +23,7 @@ describe Tap do
end end
def setup_tap_files def setup_tap_files
formula_file.dirname.mkpath
formula_file.write <<~RUBY formula_file.write <<~RUBY
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tar.gz" url "https://brew.sh/foo-1.0.tar.gz"
@ -41,6 +42,8 @@ describe Tap do
JSON JSON
%w[audit_exceptions style_exceptions].each do |exceptions_directory| %w[audit_exceptions style_exceptions].each do |exceptions_directory|
(path/exceptions_directory).mkpath
(path/"#{exceptions_directory}/formula_list.json").write <<~JSON (path/"#{exceptions_directory}/formula_list.json").write <<~JSON
[ "foo", "bar" ] [ "foo", "bar" ]
JSON JSON
@ -516,6 +519,7 @@ describe Tap do
specify "files" do specify "files" do
path = Tap::TAP_DIRECTORY/"homebrew/homebrew-core" path = Tap::TAP_DIRECTORY/"homebrew/homebrew-core"
formula_file = core_tap.formula_dir/"foo.rb" formula_file = core_tap.formula_dir/"foo.rb"
core_tap.formula_dir.mkpath
formula_file.write <<~RUBY formula_file.write <<~RUBY
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tar.gz" url "https://brew.sh/foo-1.0.tar.gz"
@ -531,6 +535,7 @@ describe Tap do
style_exceptions/formula_hash.json style_exceptions/formula_hash.json
pypi_formula_mappings.json pypi_formula_mappings.json
].each do |file| ].each do |file|
(path/file).dirname.mkpath
(path/file).write formula_list_file_json (path/file).write formula_list_file_json
end end