2017-01-21 07:21:05 -05:00
|
|
|
# frozen_string_literal: true
|
2008-01-07 10:32:07 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'tempfile'
|
2014-09-20 21:40:21 -04:00
|
|
|
require 'thread'
|
2008-01-07 10:32:07 -05:00
|
|
|
|
|
|
|
class TestTempfile < Test::Unit::TestCase
|
2009-11-11 16:42:40 -05:00
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@tempfile = nil
|
|
|
|
end
|
|
|
|
|
2009-08-26 06:00:03 -04:00
|
|
|
def tempfile(*args, &block)
|
|
|
|
t = Tempfile.new(*args, &block)
|
|
|
|
@tempfile = (t unless block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
if @tempfile
|
|
|
|
@tempfile.close!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-27 00:01:18 -05:00
|
|
|
def test_leackchecker
|
|
|
|
assert_instance_of(Tempfile, Tempfile.allocate)
|
|
|
|
end
|
|
|
|
|
2009-08-26 06:00:03 -04:00
|
|
|
def test_basic
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
t.write("hello world")
|
|
|
|
t.close
|
|
|
|
assert_equal "hello world", File.read(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_saves_in_dir_tmpdir_by_default
|
|
|
|
t = tempfile("foo")
|
|
|
|
assert_equal Dir.tmpdir, File.dirname(t.path)
|
2010-09-09 18:29:16 -04:00
|
|
|
bug3733 = '[ruby-dev:42089]'
|
|
|
|
assert_nothing_raised(SecurityError, bug3733) {
|
|
|
|
proc {$SAFE = 1; File.expand_path(Dir.tmpdir)}.call
|
|
|
|
}
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_saves_in_given_directory
|
|
|
|
subdir = File.join(Dir.tmpdir, "tempfile-test-#{rand}")
|
|
|
|
Dir.mkdir(subdir)
|
|
|
|
begin
|
|
|
|
tempfile = Tempfile.new("foo", subdir)
|
|
|
|
tempfile.close
|
|
|
|
begin
|
|
|
|
assert_equal subdir, File.dirname(tempfile.path)
|
|
|
|
ensure
|
|
|
|
tempfile.unlink
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Dir.rmdir(subdir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_basename
|
|
|
|
t = tempfile("foo")
|
2009-11-11 16:42:40 -05:00
|
|
|
assert_match(/^foo/, File.basename(t.path))
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
2015-01-03 19:18:38 -05:00
|
|
|
def test_default_basename
|
|
|
|
t = tempfile
|
2015-01-03 20:58:49 -05:00
|
|
|
assert_file.exist?(t.path)
|
2015-01-03 19:18:38 -05:00
|
|
|
end
|
|
|
|
|
2009-08-26 06:00:03 -04:00
|
|
|
def test_basename_with_suffix
|
|
|
|
t = tempfile(["foo", ".txt"])
|
2009-11-11 16:42:40 -05:00
|
|
|
assert_match(/^foo/, File.basename(t.path))
|
|
|
|
assert_match(/\.txt$/, File.basename(t.path))
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
|
|
|
|
t.close
|
2015-01-03 23:41:04 -05:00
|
|
|
assert_file.exist?(path)
|
2009-08-26 06:00:03 -04:00
|
|
|
|
|
|
|
t.unlink
|
2015-05-31 00:45:02 -04:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 06:00:03 -04:00
|
|
|
|
|
|
|
assert_nil t.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink_silently_fails_on_windows
|
|
|
|
tempfile = tempfile("foo")
|
|
|
|
path = tempfile.path
|
|
|
|
begin
|
|
|
|
assert_nothing_raised do
|
|
|
|
tempfile.unlink
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
tempfile.close
|
|
|
|
File.unlink(path) if File.exist?(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink_before_close_works_on_posix_systems
|
|
|
|
tempfile = tempfile("foo")
|
|
|
|
begin
|
|
|
|
path = tempfile.path
|
|
|
|
tempfile.unlink
|
2015-05-31 00:45:02 -04:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 06:00:03 -04:00
|
|
|
tempfile.write("hello ")
|
|
|
|
tempfile.write("world\n")
|
|
|
|
tempfile.rewind
|
|
|
|
assert_equal "hello world\n", tempfile.read
|
|
|
|
ensure
|
|
|
|
tempfile.close
|
|
|
|
tempfile.unlink
|
|
|
|
end
|
2011-04-03 22:51:17 -04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 06:00:03 -04:00
|
|
|
|
|
|
|
def test_close_and_close_p
|
|
|
|
t = tempfile("foo")
|
2015-05-31 00:45:10 -04:00
|
|
|
assert_not_predicate(t, :closed?)
|
2009-08-26 06:00:03 -04:00
|
|
|
t.close
|
2015-05-31 00:45:10 -04:00
|
|
|
assert_predicate(t, :closed?)
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_with_unlink_now_true_works
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
t.close(true)
|
2015-05-31 00:45:10 -04:00
|
|
|
assert_predicate(t, :closed?)
|
2009-08-26 06:00:03 -04:00
|
|
|
assert_nil t.path
|
2015-05-31 00:45:02 -04:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_with_unlink_now_true_does_not_unlink_if_already_unlinked
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
t.unlink
|
|
|
|
File.open(path, "w").close
|
|
|
|
begin
|
|
|
|
t.close(true)
|
2015-01-03 23:41:04 -05:00
|
|
|
assert_file.exist?(path)
|
2009-08-26 06:00:03 -04:00
|
|
|
ensure
|
|
|
|
File.unlink(path) rescue nil
|
|
|
|
end
|
2011-04-03 22:51:17 -04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 06:00:03 -04:00
|
|
|
|
|
|
|
def test_close_bang_works
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
t.close!
|
2015-05-31 00:45:10 -04:00
|
|
|
assert_predicate(t, :closed?)
|
2009-08-26 06:00:03 -04:00
|
|
|
assert_nil t.path
|
2015-05-31 00:45:02 -04:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_bang_does_not_unlink_if_already_unlinked
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
t.unlink
|
|
|
|
File.open(path, "w").close
|
|
|
|
begin
|
|
|
|
t.close!
|
2015-01-03 23:41:04 -05:00
|
|
|
assert_file.exist?(path)
|
2009-08-26 06:00:03 -04:00
|
|
|
ensure
|
|
|
|
File.unlink(path) rescue nil
|
|
|
|
end
|
2011-04-03 22:51:17 -04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 06:00:03 -04:00
|
|
|
|
|
|
|
def test_finalizer_does_not_unlink_if_already_unlinked
|
2010-05-01 18:00:37 -04:00
|
|
|
assert_in_out_err('-rtempfile', <<-'EOS') do |(filename,*), (error,*)|
|
2009-08-26 06:00:03 -04:00
|
|
|
file = Tempfile.new('foo')
|
|
|
|
path = file.path
|
|
|
|
puts path
|
|
|
|
file.close!
|
|
|
|
File.open(path, "w").close
|
|
|
|
EOS
|
2015-01-03 23:41:04 -05:00
|
|
|
assert_file.exist?(filename)
|
2009-08-26 06:00:03 -04:00
|
|
|
File.unlink(filename)
|
|
|
|
assert_nil error
|
|
|
|
end
|
|
|
|
|
2010-05-01 18:00:37 -04:00
|
|
|
assert_in_out_err('-rtempfile', <<-'EOS') do |(filename,*), (error,*)|
|
2009-08-26 06:00:03 -04:00
|
|
|
file = Tempfile.new('foo')
|
|
|
|
path = file.path
|
|
|
|
file.unlink
|
|
|
|
puts path
|
|
|
|
File.open(path, "w").close
|
|
|
|
EOS
|
|
|
|
if !filename.empty?
|
|
|
|
# POSIX unlink semantics supported, continue with test
|
2015-01-03 23:41:04 -05:00
|
|
|
assert_file.exist?(filename)
|
2009-08-26 06:00:03 -04:00
|
|
|
File.unlink(filename)
|
|
|
|
end
|
|
|
|
assert_nil error
|
|
|
|
end
|
2011-04-03 22:51:17 -04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 06:00:03 -04:00
|
|
|
|
|
|
|
def test_close_does_not_make_path_nil
|
|
|
|
t = tempfile("foo")
|
|
|
|
t.close
|
|
|
|
assert_not_nil t.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_flushes_buffer
|
|
|
|
t = tempfile("foo")
|
|
|
|
t.write("hello")
|
|
|
|
t.close
|
2010-05-01 09:54:01 -04:00
|
|
|
assert_equal 5, File.size(t.path)
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tempfile_is_unlinked_when_ruby_exits
|
2010-05-01 18:00:37 -04:00
|
|
|
assert_in_out_err('-rtempfile', <<-'EOS') do |(filename), (error)|
|
2009-08-26 06:00:03 -04:00
|
|
|
puts Tempfile.new('foo').path
|
|
|
|
EOS
|
2015-05-31 00:45:02 -04:00
|
|
|
assert_file.for("tempfile must not be exist after GC.").not_exist?(filename)
|
2013-11-21 04:51:18 -05:00
|
|
|
assert_nil(error)
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-01 09:03:58 -04:00
|
|
|
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)|
|
|
|
|
tmp = Tempfile.new('foo')
|
|
|
|
puts tmp.path
|
2013-10-05 20:38:34 -04:00
|
|
|
tmp.close
|
2013-10-01 09:03:58 -04:00
|
|
|
tmp.unlink
|
|
|
|
$DEBUG = true
|
|
|
|
EOS
|
|
|
|
assert_file.not_exist?(filename)
|
|
|
|
assert_nil(error, "#{bug8768} we used to get a confusing 'removing ...done' here")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-26 06:00:03 -04:00
|
|
|
def test_size_flushes_buffer_before_determining_file_size
|
|
|
|
t = tempfile("foo")
|
|
|
|
t.write("hello")
|
2010-05-01 09:54:01 -04:00
|
|
|
assert_equal 0, File.size(t.path)
|
|
|
|
assert_equal 5, t.size
|
|
|
|
assert_equal 5, File.size(t.path)
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_size_works_if_file_is_closed
|
|
|
|
t = tempfile("foo")
|
|
|
|
t.write("hello")
|
|
|
|
t.close
|
2010-05-01 09:54:01 -04:00
|
|
|
assert_equal 5, t.size
|
2009-08-26 06:00:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_concurrency
|
|
|
|
threads = []
|
|
|
|
tempfiles = []
|
2016-08-30 02:22:30 -04:00
|
|
|
lock = Thread::Mutex.new
|
|
|
|
cond = Thread::ConditionVariable.new
|
2009-08-26 06:00:03 -04:00
|
|
|
start = false
|
|
|
|
|
|
|
|
4.times do
|
|
|
|
threads << Thread.new do
|
|
|
|
lock.synchronize do
|
|
|
|
while !start
|
|
|
|
cond.wait(lock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result = []
|
|
|
|
30.times do
|
|
|
|
result << Tempfile.new('foo')
|
|
|
|
end
|
|
|
|
Thread.current[:result] = result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
lock.synchronize do
|
|
|
|
start = true
|
|
|
|
cond.broadcast
|
|
|
|
end
|
|
|
|
threads.each do |thread|
|
|
|
|
thread.join
|
|
|
|
tempfiles |= thread[:result]
|
|
|
|
end
|
|
|
|
filenames = tempfiles.map { |f| f.path }
|
|
|
|
begin
|
|
|
|
assert_equal filenames.size, filenames.uniq.size
|
|
|
|
ensure
|
|
|
|
tempfiles.each do |tempfile|
|
|
|
|
tempfile.close!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-07 10:32:07 -05:00
|
|
|
module M
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extend
|
2009-08-26 06:00:03 -04:00
|
|
|
o = tempfile("foo")
|
2008-01-07 10:32:07 -05:00
|
|
|
o.extend M
|
|
|
|
assert(M === o, "[ruby-dev:32932]")
|
|
|
|
end
|
2009-08-26 06:00:03 -04:00
|
|
|
|
2008-10-18 10:30:47 -04:00
|
|
|
def test_tempfile_encoding_nooption
|
|
|
|
default_external=Encoding.default_external
|
2009-08-26 06:00:03 -04:00
|
|
|
t = tempfile("TEST")
|
2008-10-18 10:30:47 -04:00
|
|
|
t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
|
|
|
|
t.rewind
|
|
|
|
assert_equal(default_external,t.read.encoding)
|
|
|
|
end
|
2009-08-26 06:00:03 -04:00
|
|
|
|
2008-10-18 10:30:47 -04:00
|
|
|
def test_tempfile_encoding_ascii8bit
|
2009-08-26 06:00:03 -04:00
|
|
|
t = tempfile("TEST",:encoding=>"ascii-8bit")
|
2008-10-18 10:30:47 -04:00
|
|
|
t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
|
|
|
|
t.rewind
|
|
|
|
assert_equal(Encoding::ASCII_8BIT,t.read.encoding)
|
|
|
|
end
|
2009-08-26 06:00:03 -04:00
|
|
|
|
2008-10-18 10:30:47 -04:00
|
|
|
def test_tempfile_encoding_ascii8bit2
|
2009-08-26 06:00:03 -04:00
|
|
|
t = tempfile("TEST",Dir::tmpdir,:encoding=>"ascii-8bit")
|
2008-10-18 10:30:47 -04:00
|
|
|
t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
|
|
|
|
t.rewind
|
|
|
|
assert_equal(Encoding::ASCII_8BIT,t.read.encoding)
|
|
|
|
end
|
2009-11-11 16:42:40 -05:00
|
|
|
|
|
|
|
def test_binmode
|
|
|
|
t = tempfile("TEST", mode: IO::BINARY)
|
|
|
|
if IO::BINARY.nonzero?
|
|
|
|
assert(t.binmode?)
|
2010-03-29 02:11:48 -04:00
|
|
|
t.open
|
|
|
|
assert(t.binmode?, 'binmode after reopen')
|
2009-11-11 16:42:40 -05:00
|
|
|
else
|
|
|
|
assert_equal(0600, t.stat.mode & 0777)
|
|
|
|
end
|
|
|
|
end
|
2013-04-20 09:50:47 -04:00
|
|
|
|
|
|
|
def test_create_with_block
|
|
|
|
path = nil
|
|
|
|
Tempfile.create("tempfile-create") {|f|
|
|
|
|
path = f.path
|
2016-01-08 03:24:01 -05:00
|
|
|
assert_file.exist?(path)
|
2013-04-20 09:50:47 -04:00
|
|
|
}
|
2016-01-08 03:24:01 -05:00
|
|
|
assert_file.not_exist?(path)
|
2013-04-20 09:50:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_without_block
|
|
|
|
path = nil
|
|
|
|
f = Tempfile.create("tempfile-create")
|
|
|
|
path = f.path
|
2016-01-08 03:24:01 -05:00
|
|
|
assert_file.exist?(path)
|
2013-04-20 09:50:47 -04:00
|
|
|
f.close
|
2016-01-08 03:24:01 -05:00
|
|
|
assert_file.exist?(path)
|
2013-04-20 09:50:47 -04:00
|
|
|
ensure
|
|
|
|
f.close if f && !f.closed?
|
|
|
|
File.unlink path if path
|
|
|
|
end
|
2016-09-26 01:45:29 -04:00
|
|
|
|
|
|
|
def test_create_default_basename
|
|
|
|
path = nil
|
|
|
|
Tempfile.create {|f|
|
|
|
|
path = f.path
|
|
|
|
assert_file.exist?(path)
|
|
|
|
}
|
|
|
|
assert_file.not_exist?(path)
|
|
|
|
end
|
2008-01-07 10:32:07 -05:00
|
|
|
end
|
|
|
|
|