Use File.write instead of Kernel#open

This commit is contained in:
Nobuyoshi Nakada 2024-07-09 13:01:44 +09:00
parent e500222de1
commit 690b56440b
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
5 changed files with 27 additions and 35 deletions

View File

@ -704,23 +704,19 @@ def assert_normal_exit(testsrc, *rest, timeout: BT.timeout, **opt)
timeout_signaled = false timeout_signaled = false
logfile = "assert_normal_exit.#{as.path}.#{as.lineno}.log" logfile = "assert_normal_exit.#{as.path}.#{as.lineno}.log"
begin io = IO.popen("#{BT.ruby} -W0 #{filename}", err: logfile)
err = open(logfile, "w") pid = io.pid
io = IO.popen("#{BT.ruby} -W0 #{filename}", err: err) th = Thread.new {
pid = io.pid io.read
th = Thread.new { io.close
io.read $?
io.close }
$? if !th.join(timeout)
} Process.kill :KILL, pid
if !th.join(timeout) timeout_signaled = true
Process.kill :KILL, pid
timeout_signaled = true
end
status = th.value
ensure
err.close
end end
status = th.value
if status && status.signaled? if status && status.signaled?
signo = status.termsig signo = status.termsig
signame = Signal.list.invert[signo] signame = Signal.list.invert[signo]

View File

@ -11,7 +11,7 @@ assert_equal 'ok', %q{
}, '[ruby-dev:43816]' }, '[ruby-dev:43816]'
assert_equal 'ok', %q{ assert_equal 'ok', %q{
open('zzz2.rb', 'w') {|f| f.puts '' } File.write('zzz2.rb', '')
instance_eval do instance_eval do
autoload :ZZZ, './zzz2.rb' autoload :ZZZ, './zzz2.rb'
begin begin
@ -23,7 +23,7 @@ assert_equal 'ok', %q{
}, '[ruby-dev:43816]' }, '[ruby-dev:43816]'
assert_equal 'ok', %q{ assert_equal 'ok', %q{
open('zzz3.rb', 'w') {|f| f.puts 'class ZZZ; def self.ok;:ok;end;end'} File.write('zzz3.rb', "class ZZZ; def self.ok;:ok;end;end\n")
instance_eval do instance_eval do
autoload :ZZZ, './zzz3.rb' autoload :ZZZ, './zzz3.rb'
ZZZ.ok ZZZ.ok
@ -31,20 +31,20 @@ assert_equal 'ok', %q{
}, '[ruby-dev:43816]' }, '[ruby-dev:43816]'
assert_equal 'ok', %q{ assert_equal 'ok', %q{
open("zzz4.rb", "w") {|f| f.puts "class ZZZ; def self.ok;:ok;end;end"} File.write("zzz4.rb", "class ZZZ; def self.ok;:ok;end;end\n")
autoload :ZZZ, "./zzz4.rb" autoload :ZZZ, "./zzz4.rb"
ZZZ.ok ZZZ.ok
} }
assert_equal 'ok', %q{ assert_equal 'ok', %q{
open("zzz5.rb", "w") {|f| f.puts "class ZZZ; def self.ok;:ok;end;end"} File.write("zzz5.rb", "class ZZZ; def self.ok;:ok;end;end\n")
autoload :ZZZ, "./zzz5.rb" autoload :ZZZ, "./zzz5.rb"
require "./zzz5.rb" require "./zzz5.rb"
ZZZ.ok ZZZ.ok
} }
assert_equal 'okok', %q{ assert_equal 'okok', %q{
open("zzz6.rb", "w") {|f| f.puts "class ZZZ; def self.ok;:ok;end;end"} File.write("zzz6.rb", "class ZZZ; def self.ok;:ok;end;end\n")
autoload :ZZZ, "./zzz6.rb" autoload :ZZZ, "./zzz6.rb"
t1 = Thread.new {ZZZ.ok} t1 = Thread.new {ZZZ.ok}
t2 = Thread.new {ZZZ.ok} t2 = Thread.new {ZZZ.ok}
@ -60,7 +60,7 @@ assert_finish 5, %q{
}, '[ruby-core:21696]' }, '[ruby-core:21696]'
assert_equal 'A::C', %q{ assert_equal 'A::C', %q{
open("zzz7.rb", "w") {} File.write("zzz7.rb", "")
class A class A
autoload :C, "./zzz7" autoload :C, "./zzz7"
class C class C

View File

@ -91,7 +91,7 @@ assert_normal_exit %q{
at_exit { p :foo } at_exit { p :foo }
megacontent = "abc" * 12345678 megacontent = "abc" * 12345678
#File.open("megasrc", "w") {|f| f << megacontent } #File.write("megasrc", megacontent)
t0 = Thread.main t0 = Thread.main
Thread.new { sleep 0.001 until t0.stop?; Process.kill(:INT, $$) } Thread.new { sleep 0.001 until t0.stop?; Process.kill(:INT, $$) }

View File

@ -1,9 +1,9 @@
assert_equal 'ok', %q{ assert_equal 'ok', %q{
open("require-lock-test.rb", "w") {|f| File.write("require-lock-test.rb", <<-END)
f.puts "sleep 0.1" sleep 0.1
f.puts "module M" module M
f.puts "end" end
} END
$:.unshift Dir.pwd $:.unshift Dir.pwd
vs = (1..2).map {|i| vs = (1..2).map {|i|
Thread.start { Thread.start {
@ -16,7 +16,7 @@ assert_equal 'ok', %q{
assert_equal 'ok', %q{ assert_equal 'ok', %q{
%w[a a/foo b].each {|d| Dir.mkdir(d)} %w[a a/foo b].each {|d| Dir.mkdir(d)}
open("b/foo", "w") {|f| f.puts "$ok = :ok"} File.write("b/foo", "$ok = :ok\n")
$:.replace(%w[a b]) $:.replace(%w[a b])
begin begin
load "foo" load "foo"

View File

@ -257,8 +257,7 @@ assert_equal 'true', %{
} }
assert_equal 'ok', %{ assert_equal 'ok', %{
open("zzz_t1.rb", "w") do |f| File.write("zzz_t1.rb", <<-END)
f.puts <<-END
begin begin
Thread.new { fork { GC.start } }.join Thread.new { fork { GC.start } }.join
pid, status = Process.wait2 pid, status = Process.wait2
@ -267,7 +266,6 @@ assert_equal 'ok', %{
$result = :ok $result = :ok
end end
END END
end
require "./zzz_t1.rb" require "./zzz_t1.rb"
$result $result
} }
@ -422,8 +420,7 @@ assert_equal 'ok', %q{
} }
assert_equal 'ok', %{ assert_equal 'ok', %{
open("zzz_t2.rb", "w") do |f| File.write("zzz_t2.rb", <<-'end;') # do
f.puts <<-'end;' # do
begin begin
m = Thread::Mutex.new m = Thread::Mutex.new
parent = Thread.current parent = Thread.current
@ -445,7 +442,6 @@ assert_equal 'ok', %{
$result = :ok $result = :ok
end end
end; end;
end
require "./zzz_t2.rb" require "./zzz_t2.rb"
$result $result
} }