summaryrefslogtreecommitdiff
path: root/test/test_tempfile.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_tempfile.rb')
-rw-r--r--test/test_tempfile.rb77
1 files changed, 75 insertions, 2 deletions
diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb
index 6b087f9207..d4ae7d4b3f 100644
--- a/test/test_tempfile.rb
+++ b/test/test_tempfile.rb
@@ -63,6 +63,22 @@ class TestTempfile < Test::Unit::TestCase
assert_match(/\.txt$/, File.basename(t.path))
end
+ def test_dup
+ t = tempfile
+ t2 = t.dup
+ t2.close
+ assert_equal true, t2.closed?
+ assert_equal false, t.closed?
+ end
+
+ def test_clone
+ t = tempfile
+ t2 = t.clone
+ t2.close
+ assert_equal true, t2.closed?
+ assert_equal false, t.closed?
+ end
+
def test_unlink
t = tempfile("foo")
path = t.path
@@ -209,8 +225,7 @@ puts Tempfile.new('foo').path
def test_tempfile_finalizer_does_not_run_if_unlinked
bug8768 = '[ruby-core:56521] [Bug #8768]'
- args = %w(--disable-gems -rtempfile)
- assert_in_out_err(args, <<-'EOS') do |(filename), (error)|
+ assert_in_out_err(%w(-rtempfile), <<-'EOS') do |(filename), (error)|
tmp = Tempfile.new('foo')
puts tmp.path
tmp.close
@@ -363,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'])
@@ -402,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