diff --git a/lib/tempfile.rb b/lib/tempfile.rb index 8b042b7..dedd98f 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -390,7 +390,7 @@ def call(object_id) $stderr.puts "removing #{@path}..." if $DEBUG begin File.unlink(@path) - rescue Errno::ENOENT + rescue Errno::ENOENT, Errno::EACCES end $stderr.puts "done" if $DEBUG end diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb index 8077cc3..91cb1a8 100644 --- a/test/test_tempfile.rb +++ b/test/test_tempfile.rb @@ -235,6 +235,22 @@ def test_finalizer_does_not_unlink_if_already_unlinked end end unless /mswin|mingw/ =~ RUBY_PLATFORM + def test_finalizer_ignores_eacces_on_unlink + assert_in_out_err("-r#{LIB_TEMPFILE_RB_PATH}", <<~'RUBY') do |(filename,*), (error,*)| + File.singleton_class.prepend(Module.new do + def unlink(*) + raise Errno::EACCES + end + end) + file = Tempfile.new("foo") + puts file.path + RUBY + assert_file.exist?(filename) + File.unlink(filename) + assert_nil error + end + end + def test_close_does_not_make_path_nil t = tempfile("foo") t.close