1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Simplify Tempfile.open calls with a block as they now unlink the file automatically

This commit is contained in:
Benoit Daloze 2020-08-29 12:17:07 +02:00
parent ff323b2a5c
commit e8c3872555
9 changed files with 13 additions and 50 deletions

View file

@ -36,7 +36,6 @@ class Gem::Ext::ExtConfBuilder < Gem::Ext::Builder
siteconf.close
destdir = ENV["DESTDIR"]
begin
cmd = Gem.ruby.shellsplit << "-I" << File.expand_path("../../..", __FILE__) <<
"-r" << get_relative_path(siteconf.path) << File.basename(extension)
@ -76,7 +75,6 @@ class Gem::Ext::ExtConfBuilder < Gem::Ext::Builder
end
ensure
ENV["DESTDIR"] = destdir
siteconf.close!
end
end

View file

@ -106,7 +106,7 @@ class TestRDocParser < RDoc::TestCase
def test_class_for_forbidden
omit 'chmod not supported' if Gem.win_platform?
tf = Tempfile.open 'forbidden' do |io|
Tempfile.open 'forbidden' do |io|
begin
File.chmod 0000, io.path
forbidden = @store.add_file io.path
@ -117,9 +117,7 @@ class TestRDocParser < RDoc::TestCase
ensure
File.chmod 0400, io.path
end
io
end
tf.close!
end
def test_class_for_modeline

View file

@ -154,7 +154,7 @@ class TestRDocRdBlockParser < RDoc::TestCase
blank_line,
blank_line)
tf = Tempfile.open %w[parse_include .rd] do |io|
Tempfile.open %w[parse_include .rd] do |io|
io.puts "=begin\ninclude ((*worked*))\n=end"
io.flush
@ -163,9 +163,7 @@ class TestRDocRdBlockParser < RDoc::TestCase
STR
assert_equal expected, parse(str)
io
end
tf.close!
end
def test_parse_heading

View file

@ -342,16 +342,14 @@ class TestRDocRDoc < RDoc::TestCase
@rdoc.options.encoding = Encoding::ISO_8859_1
@rdoc.store = RDoc::Store.new
tf = Tempfile.open 'test.txt' do |io|
Tempfile.open 'test.txt' do |io|
io.write 'hi'
io.rewind
top_level = @rdoc.parse_file io.path
assert_equal Encoding::ISO_8859_1, top_level.absolute_name.encoding
io
end
tf.close!
end
def test_parse_file_forbidden
@ -360,7 +358,7 @@ class TestRDocRDoc < RDoc::TestCase
@rdoc.store = RDoc::Store.new
tf = Tempfile.open 'test.txt' do |io|
Tempfile.open 'test.txt' do |io|
io.write 'hi'
io.rewind
@ -379,9 +377,7 @@ class TestRDocRDoc < RDoc::TestCase
ensure
File.chmod 0400, io.path
end
io
end
tf.close!
end
def test_remove_unparseable
@ -478,7 +474,7 @@ class TestRDocRDoc < RDoc::TestCase
end
def test_setup_output_dir_exists_file
tf = Tempfile.open 'test_rdoc_rdoc' do |tempfile|
Tempfile.open 'test_rdoc_rdoc' do |tempfile|
path = tempfile.path
e = assert_raise RDoc::Error do
@ -487,9 +483,7 @@ class TestRDocRDoc < RDoc::TestCase
assert_match(%r%#{Regexp.escape path} exists and is not a directory%,
e.message)
tempfile
end
tf.close!
end
def test_setup_output_dir_exists_not_rdoc

View file

@ -2814,7 +2814,7 @@ __END__
def test_flush_in_finalizer1
bug3910 = '[ruby-dev:42341]'
tmp = Tempfile.open("bug3910") {|t|
Tempfile.open("bug3910") {|t|
path = t.path
t.close
fds = []
@ -2826,7 +2826,6 @@ __END__
f.print "hoge"
}
end
t
}
ensure
ObjectSpace.each_object(File) {|f|
@ -2834,7 +2833,6 @@ __END__
f.close
end
}
tmp.close!
end
def test_flush_in_finalizer2
@ -2858,7 +2856,6 @@ __END__
end
}
end
t.close!
}
end

View file

@ -887,7 +887,7 @@ class TestGemPackage < Gem::Package::TarTestCase
def test_verify_corrupt
skip "jruby strips the null byte and does not think it's corrupt" if Gem.java_platform?
tf = Tempfile.open 'corrupt' do |io|
Tempfile.open 'corrupt' do |io|
data = Gem::Util.gzip 'a' * 10
io.write \
tar_file_header('metadata.gz', "\000x", 0644, data.length, Time.now)
@ -902,9 +902,7 @@ class TestGemPackage < Gem::Package::TarTestCase
assert_equal "tar is corrupt, name contains null byte in #{io.path}",
e.message
io
end
tf.close!
end
def test_verify_empty

View file

@ -261,16 +261,14 @@ ruby "0"
def test_load_gemdeps
rs = Gem::RequestSet.new
tf = Tempfile.open 'gem.deps.rb' do |io|
Tempfile.open 'gem.deps.rb' do |io|
io.puts 'gem "a"'
io.flush
gem_deps = rs.load_gemdeps io.path
assert_kind_of Gem::RequestSet::GemDependencyAPI, gem_deps
io
end
tf.close!
assert_equal [dep('a')], rs.dependencies
@ -281,7 +279,7 @@ ruby "0"
def test_load_gemdeps_installing
rs = Gem::RequestSet.new
tf = Tempfile.open 'gem.deps.rb' do |io|
Tempfile.open 'gem.deps.rb' do |io|
io.puts 'ruby "0"'
io.puts 'gem "a"'
io.flush
@ -289,9 +287,7 @@ ruby "0"
gem_deps = rs.load_gemdeps io.path, [], true
assert_kind_of Gem::RequestSet::GemDependencyAPI, gem_deps
io
end
tf.close!
assert_equal [dep('a')], rs.dependencies
end
@ -299,14 +295,12 @@ ruby "0"
def test_load_gemdeps_without_groups
rs = Gem::RequestSet.new
tf = Tempfile.open 'gem.deps.rb' do |io|
Tempfile.open 'gem.deps.rb' do |io|
io.puts 'gem "a", :group => :test'
io.flush
rs.load_gemdeps io.path, [:test]
io
end
tf.close!
assert_empty rs.dependencies
end
@ -394,7 +388,7 @@ ruby "0"
rs = Gem::RequestSet.new
tf = Tempfile.open 'gem.deps.rb' do |io|
Tempfile.open 'gem.deps.rb' do |io|
io.puts <<-GEMS_DEPS_RB
gem "#{name}", :git => "#{repository}"
GEMS_DEPS_RB
@ -402,9 +396,7 @@ ruby "0"
io.flush
rs.load_gemdeps io.path
io
end
tf.close!
res = rs.resolve
assert_equal 1, res.size
@ -457,7 +449,7 @@ ruby "0"
rs = Gem::RequestSet.new
tf = Tempfile.open 'gem.deps.rb' do |io|
Tempfile.open 'gem.deps.rb' do |io|
io.puts <<-GEMS_DEPS_RB
gem "#{a_name}", :path => "#{a_directory}"
gem "#{b_name}", :path => "#{b_directory}"
@ -466,9 +458,7 @@ ruby "0"
io.flush
rs.load_gemdeps io.path
io
end
tf.close!
res = rs.resolve
assert_equal 2, res.size

View file

@ -632,7 +632,7 @@ class TestGemRequestSetGemDependencyAPI < Gem::TestCase
end
def test_load
tf = Tempfile.open 'gem.deps.rb' do |io|
Tempfile.open 'gem.deps.rb' do |io|
io.write <<-GEM_DEPS
gem 'a'
@ -647,9 +647,7 @@ end
assert_equal gda, gda.load
assert_equal [dep('a'), dep('b')], @set.dependencies
io
end
tf.close!
end
def test_pin_gem_source

View file

@ -122,16 +122,11 @@ module MiniTest
return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
need_to_diff
tempfile_a = nil
tempfile_b = nil
Tempfile.open("expect") do |a|
tempfile_a = a
a.puts expect
a.flush
Tempfile.open("butwas") do |b|
tempfile_b = b
b.puts butwas
b.flush
@ -152,9 +147,6 @@ module MiniTest
end
result
ensure
tempfile_a.close! if tempfile_a
tempfile_b.close! if tempfile_b
end
##