From ddd99a529005eade4e99735e7aba8166f4a32ca2 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 8 Nov 2023 08:35:16 -0800 Subject: [PATCH] [ruby/tempfile] Make Tempfile#open return the underlying File Add test for this behavior. https://github.com/ruby/tempfile/commit/0ca31a6b8d --- lib/tempfile.rb | 1 + test/test_tempfile.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/tempfile.rb b/lib/tempfile.rb index d192bf3502..43174082a0 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -191,6 +191,7 @@ class Tempfile < DelegateClass(File) mode = @mode & ~(File::CREAT|File::EXCL) __setobj__(File.open(__getobj__.path, mode, **@opts)) ObjectSpace.define_finalizer(self, Closer.new(__getobj__)) + __getobj__ end def _close # :nodoc: diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb index d5d684b016..eddbac5d75 100644 --- a/test/test_tempfile.rb +++ b/test/test_tempfile.rb @@ -378,6 +378,14 @@ puts Tempfile.new('foo').path assert_file.not_exist?(path) end + def test_open + Tempfile.open {|f| + file = f.open + assert_kind_of File, file + assert_equal f.to_i, file.to_i + } + end + def test_open_traversal_dir assert_mktmpdir_traversal do |traversal_path| t = Tempfile.open([traversal_path, 'foo'])