2017-01-21 15:21:05 +03:00
|
|
|
# frozen_string_literal: true
|
2008-01-07 18:32:07 +03:00
|
|
|
require 'test/unit'
|
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
class TestTempfile < Test::Unit::TestCase
|
2009-11-12 00:42:40 +03:00
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@tempfile = nil
|
|
|
|
end
|
|
|
|
|
2009-08-26 14: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 08:01:18 +03:00
|
|
|
def test_leackchecker
|
|
|
|
assert_instance_of(Tempfile, Tempfile.allocate)
|
|
|
|
end
|
|
|
|
|
2009-08-26 14: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-10 02:29:16 +04:00
|
|
|
bug3733 = '[ruby-dev:42089]'
|
|
|
|
assert_nothing_raised(SecurityError, bug3733) {
|
|
|
|
proc {$SAFE = 1; File.expand_path(Dir.tmpdir)}.call
|
|
|
|
}
|
2017-12-28 23:09:24 +03:00
|
|
|
ensure
|
|
|
|
$SAFE = 0
|
2009-08-26 14: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-12 00:42:40 +03:00
|
|
|
assert_match(/^foo/, File.basename(t.path))
|
2009-08-26 14:00:03 +04:00
|
|
|
end
|
|
|
|
|
2015-01-04 03:18:38 +03:00
|
|
|
def test_default_basename
|
|
|
|
t = tempfile
|
2015-01-04 04:58:49 +03:00
|
|
|
assert_file.exist?(t.path)
|
2015-01-04 03:18:38 +03:00
|
|
|
end
|
|
|
|
|
2009-08-26 14:00:03 +04:00
|
|
|
def test_basename_with_suffix
|
|
|
|
t = tempfile(["foo", ".txt"])
|
2009-11-12 00:42:40 +03:00
|
|
|
assert_match(/^foo/, File.basename(t.path))
|
|
|
|
assert_match(/\.txt$/, File.basename(t.path))
|
2009-08-26 14:00:03 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
|
|
|
|
t.close
|
2015-01-04 07:41:04 +03:00
|
|
|
assert_file.exist?(path)
|
2009-08-26 14:00:03 +04:00
|
|
|
|
|
|
|
t.unlink
|
2015-05-31 07:45:02 +03:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 14: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 07:45:02 +03:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 14: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-04 06:51:17 +04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 14:00:03 +04:00
|
|
|
|
|
|
|
def test_close_and_close_p
|
|
|
|
t = tempfile("foo")
|
2015-05-31 07:45:10 +03:00
|
|
|
assert_not_predicate(t, :closed?)
|
2009-08-26 14:00:03 +04:00
|
|
|
t.close
|
2015-05-31 07:45:10 +03:00
|
|
|
assert_predicate(t, :closed?)
|
2009-08-26 14: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 07:45:10 +03:00
|
|
|
assert_predicate(t, :closed?)
|
2009-08-26 14:00:03 +04:00
|
|
|
assert_nil t.path
|
2015-05-31 07:45:02 +03:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 14: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-04 07:41:04 +03:00
|
|
|
assert_file.exist?(path)
|
2009-08-26 14:00:03 +04:00
|
|
|
ensure
|
|
|
|
File.unlink(path) rescue nil
|
|
|
|
end
|
2011-04-04 06:51:17 +04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 14:00:03 +04:00
|
|
|
|
|
|
|
def test_close_bang_works
|
|
|
|
t = tempfile("foo")
|
|
|
|
path = t.path
|
|
|
|
t.close!
|
2015-05-31 07:45:10 +03:00
|
|
|
assert_predicate(t, :closed?)
|
2009-08-26 14:00:03 +04:00
|
|
|
assert_nil t.path
|
2015-05-31 07:45:02 +03:00
|
|
|
assert_file.not_exist?(path)
|
2009-08-26 14: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-04 07:41:04 +03:00
|
|
|
assert_file.exist?(path)
|
2009-08-26 14:00:03 +04:00
|
|
|
ensure
|
|
|
|
File.unlink(path) rescue nil
|
|
|
|
end
|
2011-04-04 06:51:17 +04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 14:00:03 +04:00
|
|
|
|
|
|
|
def test_finalizer_does_not_unlink_if_already_unlinked
|
2010-05-02 02:00:37 +04:00
|
|
|
assert_in_out_err('-rtempfile', <<-'EOS') do |(filename,*), (error,*)|
|
2009-08-26 14:00:03 +04:00
|
|
|
file = Tempfile.new('foo')
|
|
|
|
path = file.path
|
|
|
|
puts path
|
|
|
|
file.close!
|
|
|
|
File.open(path, "w").close
|
|
|
|
EOS
|
2015-01-04 07:41:04 +03:00
|
|
|
assert_file.exist?(filename)
|
2009-08-26 14:00:03 +04:00
|
|
|
File.unlink(filename)
|
|
|
|
assert_nil error
|
|
|
|
end
|
|
|
|
|
2010-05-02 02:00:37 +04:00
|
|
|
assert_in_out_err('-rtempfile', <<-'EOS') do |(filename,*), (error,*)|
|
2009-08-26 14: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-04 07:41:04 +03:00
|
|
|
assert_file.exist?(filename)
|
2009-08-26 14:00:03 +04:00
|
|
|
File.unlink(filename)
|
|
|
|
end
|
|
|
|
assert_nil error
|
|
|
|
end
|
2011-04-04 06:51:17 +04:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-08-26 14: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 17:54:01 +04:00
|
|
|
assert_equal 5, File.size(t.path)
|
2009-08-26 14:00:03 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tempfile_is_unlinked_when_ruby_exits
|
2010-05-02 02:00:37 +04:00
|
|
|
assert_in_out_err('-rtempfile', <<-'EOS') do |(filename), (error)|
|
2009-08-26 14:00:03 +04:00
|
|
|
puts Tempfile.new('foo').path
|
|
|
|
EOS
|
2015-05-31 07:45:02 +03:00
|
|
|
assert_file.for("tempfile must not be exist after GC.").not_exist?(filename)
|
2013-11-21 13:51:18 +04:00
|
|
|
assert_nil(error)
|
2009-08-26 14:00:03 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-01 17: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-06 04:38:34 +04:00
|
|
|
tmp.close
|
2013-10-01 17: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 14:00:03 +04:00
|
|
|
def test_size_flushes_buffer_before_determining_file_size
|
|
|
|
t = tempfile("foo")
|
|
|
|
t.write("hello")
|
2010-05-01 17: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 14:00:03 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_size_works_if_file_is_closed
|
|
|
|
t = tempfile("foo")
|
|
|
|
t.write("hello")
|
|
|
|
t.close
|
2010-05-01 17:54:01 +04:00
|
|
|
assert_equal 5, t.size
|
2009-08-26 14:00:03 +04:00
|
|
|
end
|
|
|
|
|
2017-03-14 15:53:32 +03:00
|
|
|
def test_size_on_empty_file
|
|
|
|
t = tempfile("foo")
|
|
|
|
t.write("")
|
|
|
|
t.close
|
|
|
|
assert_equal 0, t.size
|
|
|
|
end
|
|
|
|
|
2009-08-26 14:00:03 +04:00
|
|
|
def test_concurrency
|
|
|
|
threads = []
|
|
|
|
tempfiles = []
|
2016-08-30 09:22:30 +03:00
|
|
|
lock = Thread::Mutex.new
|
|
|
|
cond = Thread::ConditionVariable.new
|
2009-08-26 14: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 18:32:07 +03:00
|
|
|
module M
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extend
|
2009-08-26 14:00:03 +04:00
|
|
|
o = tempfile("foo")
|
2008-01-07 18:32:07 +03:00
|
|
|
o.extend M
|
|
|
|
assert(M === o, "[ruby-dev:32932]")
|
|
|
|
end
|
2009-08-26 14:00:03 +04:00
|
|
|
|
2008-10-18 18:30:47 +04:00
|
|
|
def test_tempfile_encoding_nooption
|
|
|
|
default_external=Encoding.default_external
|
2009-08-26 14:00:03 +04:00
|
|
|
t = tempfile("TEST")
|
2008-10-18 18: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 14:00:03 +04:00
|
|
|
|
2008-10-18 18:30:47 +04:00
|
|
|
def test_tempfile_encoding_ascii8bit
|
2009-08-26 14:00:03 +04:00
|
|
|
t = tempfile("TEST",:encoding=>"ascii-8bit")
|
2008-10-18 18: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 14:00:03 +04:00
|
|
|
|
2008-10-18 18:30:47 +04:00
|
|
|
def test_tempfile_encoding_ascii8bit2
|
2009-08-26 14:00:03 +04:00
|
|
|
t = tempfile("TEST",Dir::tmpdir,:encoding=>"ascii-8bit")
|
2008-10-18 18: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-12 00:42:40 +03:00
|
|
|
|
|
|
|
def test_binmode
|
|
|
|
t = tempfile("TEST", mode: IO::BINARY)
|
|
|
|
if IO::BINARY.nonzero?
|
|
|
|
assert(t.binmode?)
|
2010-03-29 10:11:48 +04:00
|
|
|
t.open
|
|
|
|
assert(t.binmode?, 'binmode after reopen')
|
2009-11-12 00:42:40 +03:00
|
|
|
else
|
|
|
|
assert_equal(0600, t.stat.mode & 0777)
|
|
|
|
end
|
|
|
|
end
|
2013-04-20 17:50:47 +04:00
|
|
|
|
|
|
|
def test_create_with_block
|
|
|
|
path = nil
|
|
|
|
Tempfile.create("tempfile-create") {|f|
|
|
|
|
path = f.path
|
2016-01-08 11:24:01 +03:00
|
|
|
assert_file.exist?(path)
|
2013-04-20 17:50:47 +04:00
|
|
|
}
|
2016-01-08 11:24:01 +03:00
|
|
|
assert_file.not_exist?(path)
|
2017-05-19 12:20:14 +03:00
|
|
|
|
|
|
|
Tempfile.create("tempfile-create") {|f|
|
|
|
|
path = f.path
|
2017-05-19 15:44:46 +03:00
|
|
|
f.close
|
2017-05-19 12:20:14 +03:00
|
|
|
File.unlink(f.path)
|
|
|
|
}
|
|
|
|
assert_file.not_exist?(path)
|
2013-04-20 17:50:47 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_without_block
|
|
|
|
path = nil
|
|
|
|
f = Tempfile.create("tempfile-create")
|
|
|
|
path = f.path
|
2016-01-08 11:24:01 +03:00
|
|
|
assert_file.exist?(path)
|
2013-04-20 17:50:47 +04:00
|
|
|
f.close
|
2016-01-08 11:24:01 +03:00
|
|
|
assert_file.exist?(path)
|
2013-04-20 17:50:47 +04:00
|
|
|
ensure
|
2018-06-11 17:13:56 +03:00
|
|
|
f&.close
|
2013-04-20 17:50:47 +04:00
|
|
|
File.unlink path if path
|
|
|
|
end
|
2016-09-26 08:45:29 +03: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 18:32:07 +03:00
|
|
|
|
2018-03-28 13:03:03 +03:00
|
|
|
TRAVERSAL_PATH = Array.new(Dir.pwd.split('/').count, '..').join('/') + Dir.pwd + '/'
|
|
|
|
|
|
|
|
def test_open_traversal_dir
|
|
|
|
expect = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
t = Tempfile.open([TRAVERSAL_PATH, 'foo'])
|
|
|
|
actual = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
assert_equal expect, actual
|
|
|
|
ensure
|
|
|
|
t.close!
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_traversal_dir
|
|
|
|
expect = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
t = Tempfile.new(TRAVERSAL_PATH + 'foo')
|
|
|
|
actual = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
assert_equal expect, actual
|
|
|
|
ensure
|
2018-04-04 18:44:56 +03:00
|
|
|
t&.close!
|
2018-03-28 13:03:03 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_traversal_dir
|
|
|
|
expect = Dir.glob(TRAVERSAL_PATH + '*').count
|
2018-04-04 18:26:11 +03:00
|
|
|
t = Tempfile.create(TRAVERSAL_PATH + 'foo')
|
2018-03-28 13:03:03 +03:00
|
|
|
actual = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
assert_equal expect, actual
|
2018-04-04 18:26:11 +03:00
|
|
|
ensure
|
2018-04-04 18:44:56 +03:00
|
|
|
t&.close
|
2018-03-28 13:03:03 +03:00
|
|
|
end
|
|
|
|
end
|