diff --git a/lib/tempfile.rb b/lib/tempfile.rb index 77f3e4e..56ed495 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -334,7 +334,10 @@ def Tempfile.create(basename, tmpdir=nil, mode: 0, **options) yield tmpfile ensure tmpfile.close if !tmpfile.closed? - File.unlink tmpfile + begin + File.unlink tmpfile + rescue Errno::ENOENT + end end else tmpfile diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb index bcd9535..dfeea56 100644 --- a/test/test_tempfile.rb +++ b/test/test_tempfile.rb @@ -333,6 +333,15 @@ puts Tempfile.new('foo').path assert(!File.exist?(path)) end + def test_create_with_unlink_in_block_silently_fails + assert_nothing_raised { + Tempfile.create("tempfile-create") {|f| + f.close + File.unlink f.path + } + } + end + def test_create_without_block path = nil f = Tempfile.create("tempfile-create")