diff options
author | Peter Zhu <[email protected]> | 2024-08-20 13:28:11 -0400 |
---|---|---|
committer | git <[email protected]> | 2024-08-20 18:07:42 +0000 |
commit | a68331e7036d7ab433778bf65eb854aabd5009c4 (patch) | |
tree | e64a0d781c4a332631a49f9fa6066e088b1c92a3 /test/test_tempfile.rb | |
parent | 41b427a2648ed2e049952450c698be917e0bb125 (diff) |
[ruby/tempfile] Add FinalizerManager to manage finalizers
As @jeremyevans pointed out for commit eb2d8b1:
> Each Tempfile instance has a separate File instance and file descriptor:
>
> t = Tempfile.new
> t.to_i # => 6
> t.dup.to_i => 7
FinalizerManager will keep track of the open File objects for the
particular file and will only unlink the file when all of the File objects
have been closed.
https://github.com/ruby/tempfile/commit/753ab16642
Diffstat (limited to 'test/test_tempfile.rb')
-rw-r--r-- | test/test_tempfile.rb | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb index ccebbf3c1a..1e40e4e480 100644 --- a/test/test_tempfile.rb +++ b/test/test_tempfile.rb @@ -3,6 +3,8 @@ require 'test/unit' require 'tempfile' class TestTempfile < Test::Unit::TestCase + LIB_TEMPFILE_RB_PATH = File.expand_path(__dir__ + "/../lib/tempfile.rb") + def initialize(*) super @tempfile = nil @@ -172,6 +174,38 @@ class TestTempfile < Test::Unit::TestCase end end unless /mswin|mingw/ =~ RUBY_PLATFORM + def test_finalizer_removes_file + assert_in_out_err(["-r#{LIB_TEMPFILE_RB_PATH}"], <<~RUBY) do |(filename,*), (error,*)| + file = Tempfile.new("foo") + puts file.path + RUBY + assert_file.not_exist?(filename) + assert_nil error + end + end + + def test_finalizer_removes_file_when_dup + assert_in_out_err(["-r#{LIB_TEMPFILE_RB_PATH}"], <<~RUBY) do |(filename,*), (error,*)| + file = Tempfile.new("foo") + file.dup + puts file.path + RUBY + assert_file.not_exist?(filename) + assert_nil error + end + end + + def test_finalizer_removes_file_when_clone + assert_in_out_err(["-r#{LIB_TEMPFILE_RB_PATH}"], <<~RUBY) do |(filename,*), (error,*)| + file = Tempfile.new("foo") + file.clone + puts file.path + RUBY + assert_file.not_exist?(filename) + assert_nil error + end + end + def test_finalizer_does_not_unlink_if_already_unlinked assert_in_out_err('-rtempfile', <<-'EOS') do |(filename,*), (error,*)| file = Tempfile.new('foo') @@ -474,5 +508,4 @@ puts Tempfile.new('foo').path assert_equal(true, t.autoclose?) } end - end |