Skip to content

Commit

Permalink
chore: use File::write instead of Kernel#open
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdb committed Oct 1, 2024
1 parent 4538838 commit 8834181
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 109 deletions.
16 changes: 6 additions & 10 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,17 @@ def rake_system_dir

FileUtils.mkdir_p @system_dir

open File.join(@system_dir, "sys1.rake"), "w" do |io|
io << <<~SYS
task "sys1" do
puts "SYS1"
end
SYS
end
File.write File.join(@system_dir, "sys1.rake"), <<~SYS
task "sys1" do
puts "SYS1"
end
SYS

ENV["RAKE_SYSTEM"] = @system_dir
end

def rakefile(contents)
open "Rakefile", "w" do |io|
io << contents
end
File.write "Rakefile", contents
end

def jruby?
Expand Down
126 changes: 55 additions & 71 deletions test/support/rakefile_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,16 @@ def rakefile_extra

FileUtils.mkdir_p "rakelib"

open File.join("rakelib", "extra.rake"), "w" do |io|
io << <<~EXTRA_RAKE
# Added for testing
namespace :extra do
desc "An Extra Task"
task :extra do
puts "Read all about it"
end
File.write File.join("rakelib", "extra.rake"), <<~EXTRA_RAKE
# Added for testing
namespace :extra do
desc "An Extra Task"
task :extra do
puts "Read all about it"
end
EXTRA_RAKE
end
end
EXTRA_RAKE
end

def rakefile_file_creation
Expand Down Expand Up @@ -245,7 +243,7 @@ def rakefile_imports
end
file "dynamic_deps" do |t|
open(t.name, "w") do |f| f.puts "puts 'DYNAMIC'" end
File.write t.name, "puts 'DYNAMIC'\n"
end
import "dynamic_deps"
Expand All @@ -255,39 +253,31 @@ def rakefile_imports
puts "FIRST"
IMPORTS

open "deps.mf", "w" do |io|
io << <<~DEPS
default: other
DEPS
end
File.write "deps.mf", <<~DEPS
default: other
DEPS

open "static_deps", "w" do |f|
f.puts 'puts "STATIC"'
end
File.write "static_deps", "puts 'STATIC'\n"
end

def rakefile_regenerate_imports
rakefile <<~REGENERATE_IMPORTS
task :default
task :regenerate do
open("deps", "w") do |f|
f << <<~CONTENT
file "deps" => :regenerate
puts "REGENERATED"
CONTENT
end
File.write "deps", <<~CONTENT
file "deps" => :regenerate
puts "REGENERATED"
CONTENT
end
import "deps"
REGENERATE_IMPORTS

open "deps", "w" do |f|
f << <<~CONTENT
file "deps" => :regenerate
puts "INITIAL"
CONTENT
end
File.write "deps", <<~CONTENT
file "deps" => :regenerate
puts "INITIAL"
CONTENT
end

def rakefile_multidesc
Expand Down Expand Up @@ -387,28 +377,22 @@ def rakefile_rakelib
FileUtils.mkdir_p "rakelib"

Dir.chdir "rakelib" do
open "test1.rb", "w" do |io|
io << <<~TEST1
task :default do
puts "TEST1"
end
TEST1
end
File.write "test1.rb", <<~TEST1
task :default do
puts "TEST1"
end
TEST1

open "test2.rake", "w" do |io|
io << <<~TEST1
task :default do
puts "TEST2"
end
TEST1
end
File.write "test2.rake", <<~TEST2
task :default do
puts "TEST2"
end
TEST2
end
end

def rakefile_rbext
open "rakefile.rb", "w" do |io|
io << 'task :default do puts "OK" end'
end
File.write "rakefile.rb", 'task :default do puts "OK" end'
end

def rakefile_unittest
Expand Down Expand Up @@ -477,15 +461,15 @@ def rakefile_test_signal
task :default => :test
TEST_SIGNAL
open "a_test.rb", "w" do |io|
io << 'puts "ATEST"' << "\n"
io << "$stdout.flush" << "\n"
io << 'Process.kill("TERM", $$)' << "\n"
end
open "b_test.rb", "w" do |io|
io << 'puts "BTEST"' << "\n"
io << "$stdout.flush" << "\n"
end
File.write "a_test.rb", <<~A_TEST
puts "ATEST"
$stdout.flush
Process.kill("TERM", $$)
A_TEST
File.write "b_test.rb", <<~B_TEST
puts "BTEST"
$stdout.flush
B_TEST
end

def rakefile_failing_test_task
Expand All @@ -497,21 +481,21 @@ def rakefile_failing_test_task
t.test_files = ['a_test.rb']
end
TEST_TASK
open "a_test.rb", "w" do |io|
io << "require 'minitest/autorun'\n"
io << "class ExitTaskTest < Minitest::Test\n"
io << " def test_exit\n"
io << " assert false, 'this should fail'\n"
io << " end\n"
io << "end\n"
end
File.write "a_test.rb", <<~A_TEST
require 'minitest/autorun'
class ExitTaskTest < Minitest::Test
def test_exit
assert false, 'this should fail'
end
end
A_TEST
end

def rakefile_stand_alone_filelist
open "stand_alone_filelist.rb", "w" do |io|
io << "require 'rake/file_list'\n"
io << "FL = Rake::FileList['*.rb']\n"
io << "puts FL\n"
end
File.write "stand_alone_filelist.rb", <<~STAND_ALONE
require 'rake/file_list'
FL = Rake::FileList['*.rb']
puts FL
STAND_ALONE
end
end
6 changes: 3 additions & 3 deletions test/test_rake_application_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ def test_rakelib
def test_require
$LOAD_PATH.unshift @tempdir

open "reqfile.rb", "w" do |io| io << "TESTING_REQUIRE << 1" end
open "reqfile2.rb", "w" do |io| io << "TESTING_REQUIRE << 2" end
open "reqfile3.rake", "w" do |io| io << "TESTING_REQUIRE << 3" end
File.write "reqfile.rb", "TESTING_REQUIRE << 1"
File.write "reqfile2.rb", "TESTING_REQUIRE << 2"
File.write "reqfile3.rake", "TESTING_REQUIRE << 3"

flags(["--require", "reqfile"], "-rreqfile2", "-rreqfile3")

Expand Down
2 changes: 1 addition & 1 deletion test/test_rake_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_implicit_file_dependencies
def create_existing_file
Dir.mkdir File.dirname(EXISTINGFILE) unless
File.exist?(File.dirname(EXISTINGFILE))
open(EXISTINGFILE, "w") do |f| f.puts "HI" end unless
File.write(EXISTINGFILE, "HI") unless
File.exist?(EXISTINGFILE)
end

Expand Down
8 changes: 4 additions & 4 deletions test/test_rake_file_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def setup
FileUtils.touch "abc.x"
FileUtils.touch "existing"

open "xyzzy.txt", "w" do |io|
io.puts "x"
io.puts "XYZZY"
end
File.write "xyzzy.txt", <<~EOTEXT
x
XYZZY
EOTEXT

end

Expand Down
2 changes: 1 addition & 1 deletion test/test_rake_file_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_file_need
assert ftask.needed?, "file should be needed"
assert_equal Rake::LATE, ftask.timestamp

open(ftask.name, "w") { |f| f.puts "HI" }
File.write(ftask.name, "HI\n")

assert_nil ftask.prerequisites.map { |n| Task[n].timestamp }.max
assert ! ftask.needed?, "file should not be needed"
Expand Down
6 changes: 2 additions & 4 deletions test/test_rake_file_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_rm_nowrite
end

def test_ln
open("a", "w") { |f| f.puts "TEST_LN" }
File.write("a", "TEST_LN\n")

Rake::FileUtilsExt.safe_ln("a", "b", verbose: false)

Expand Down Expand Up @@ -410,9 +410,7 @@ def test_split_all
end

def command(name, text)
open name, "w", 0750 do |io|
io << text
end
File.write(name, text)
end

def check_no_expansion
Expand Down
28 changes: 13 additions & 15 deletions test/test_rake_makefile_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ class TestRakeMakefileLoader < Rake::TestCase # :nodoc:
def test_parse
Dir.chdir @tempdir

open "sample.mf", "w" do |io|
io << <<~'SAMPLE_MF'
# Comments
a: a1 a2 a3 a4
b: b1 b2 b3 \
b4 b5 b6\
# Mid: Comment
b7
File.write "sample.mf", <<~'SAMPLE_MF'
# Comments
a: a1 a2 a3 a4
b: b1 b2 b3 \
b4 b5 b6\
# Mid: Comment
b7
a : a5 a6 a7
c: c1
d: d1 d2 \
a : a5 a6 a7
c: c1
d: d1 d2 \
e f : e1 f1
e f : e1 f1
g\ 0: g1 g\ 2 g\ 3 g4
SAMPLE_MF
end
g\ 0: g1 g\ 2 g\ 3 g4
SAMPLE_MF

Task.clear
loader = Rake::MakefileLoader.new
Expand Down

0 comments on commit 8834181

Please sign in to comment.