diff options
| author | akr <akr@fsij.org> | 2024-06-01 15:11:19 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-01 15:11:19 +0900 |
| commit | 3ee83c73c38070d695537d4322ce4decb970a54a (patch) | |
| tree | 5473bb0b99515adfd2b62888e226e25d489c2a93 /test | |
| parent | 5308da5e1c53839b27cc4c0081bb965b46e0d052 (diff) | |
Tempfile.create(anonymous: true) implemented. (#10803)
The keyword argument `anonymous` is implemented for `Tempfile.create`
The default is `anonymous: false`.
The behavior is not changed as before.
The created temporary file is immediately removed if `anonymous: true` is specified.
So applications don't need to remove the file.
The actual storage of the file is reclaimed by the OS when the file is closed.
It uses `O_TMPFILE` for Linux 3.11 or later.
It creates an anonymous file from the beginning.
It uses FILE_SHARE_DELETE for Windows.
It makes it possible to remove the opened file.
[Feature #20497]
Diffstat (limited to 'test')
| -rw-r--r-- | test/test_tempfile.rb | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb index eddbac5d75..d4ae7d4b3f 100644 --- a/test/test_tempfile.rb +++ b/test/test_tempfile.rb @@ -425,4 +425,54 @@ puts Tempfile.new('foo').path assert_not_send([File.absolute_path(actual), :start_with?, target]) end end + + def test_create_anonymous_without_block + t = Tempfile.create(anonymous: true) + assert_equal(File, t.class) + assert_equal(0600, t.stat.mode & 0777) unless /mswin|mingw/ =~ RUBY_PLATFORM + t.puts "foo" + t.rewind + assert_equal("foo\n", t.read) + t.close + ensure + t.close if t + end + + def test_create_anonymous_with_block + result = Tempfile.create(anonymous: true) {|t| + assert_equal(File, t.class) + assert_equal(0600, t.stat.mode & 0777) unless /mswin|mingw/ =~ RUBY_PLATFORM + t.puts "foo" + t.rewind + assert_equal("foo\n", t.read) + :result + } + assert_equal(:result, result) + end + + def test_create_anonymous_removes_file + Dir.mktmpdir {|d| + t = Tempfile.create("", d, anonymous: true) + t.close + assert_equal([], Dir.children(d)) + } + end + + def test_create_anonymous_path + Dir.mktmpdir {|d| + begin + t = Tempfile.create("", d, anonymous: true) + assert_equal(File.join(d, ""), t.path) + ensure + t.close if t + end + } + end + + def test_create_anonymous_autoclose + Tempfile.create(anonymous: true) {|t| + assert_equal(true, t.autoclose?) + } + end + end |
