From 3c5344bf303ae50bc2a0a6664fc8aff8d1684ea0 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 19 May 2017 09:20:14 +0000 Subject: tempfile.rb: remove in Tempfile.create * lib/tempfile.rb (Tempfile.create): should not fail even if the temporary file has been removed in the block, just ignore. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58791 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/tempfile.rb | 10 +++++++++- test/test_tempfile.rb | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/tempfile.rb b/lib/tempfile.rb index b36c6638b6..b6270b827a 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -334,8 +334,16 @@ def Tempfile.create(basename="", tmpdir=nil, mode: 0, **options) begin yield tmpfile ensure + if File.identical?(tmpfile, tmpfile.path) + unlinked = File.unlink tmpfile.path rescue nil + end tmpfile.close - File.unlink tmpfile + unless unlinked + begin + File.unlink tmpfile.path + rescue Errno::ENOENT + end + end end else tmpfile diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb index a2b272747e..92ec988f63 100644 --- a/test/test_tempfile.rb +++ b/test/test_tempfile.rb @@ -343,6 +343,12 @@ puts Tempfile.new('foo').path assert_file.exist?(path) } assert_file.not_exist?(path) + + Tempfile.create("tempfile-create") {|f| + path = f.path + File.unlink(f.path) + } + assert_file.not_exist?(path) end def test_create_without_block -- cgit v1.2.1