2008-02-03 07:14:30 -05:00
|
|
|
require "test/unit"
|
|
|
|
require "fileutils"
|
|
|
|
require "tmpdir"
|
2012-10-09 10:18:05 -04:00
|
|
|
require_relative "envutil"
|
2008-02-03 07:14:30 -05:00
|
|
|
|
|
|
|
class TestFileExhaustive < Test::Unit::TestCase
|
2012-01-24 21:32:06 -05:00
|
|
|
DRIVE = Dir.pwd[%r'\A(?:[a-z]:|//[^/]+/[^/]+)'i]
|
|
|
|
|
2009-09-19 04:33:01 -04:00
|
|
|
def assert_incompatible_encoding
|
|
|
|
d = "\u{3042}\u{3044}".encode("utf-16le")
|
|
|
|
assert_raise(Encoding::CompatibilityError) {yield d}
|
|
|
|
m = Class.new {define_method(:to_path) {d}}
|
|
|
|
assert_raise(Encoding::CompatibilityError) {yield m.new}
|
|
|
|
end
|
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
def setup
|
|
|
|
@dir = Dir.mktmpdir("rubytest-file")
|
2012-08-23 23:44:56 -04:00
|
|
|
@rootdir = "#{DRIVE}/"
|
2008-08-13 13:46:47 -04:00
|
|
|
File.chown(-1, Process.gid, @dir)
|
2008-02-03 07:14:30 -05:00
|
|
|
@file = make_tmp_filename("file")
|
|
|
|
@zerofile = make_tmp_filename("zerofile")
|
|
|
|
@nofile = make_tmp_filename("nofile")
|
|
|
|
@symlinkfile = make_tmp_filename("symlinkfile")
|
|
|
|
@hardlinkfile = make_tmp_filename("hardlinkfile")
|
|
|
|
make_file("foo", @file)
|
|
|
|
make_file("", @zerofile)
|
|
|
|
@time = Time.now
|
|
|
|
begin
|
|
|
|
File.symlink(@file, @symlinkfile)
|
|
|
|
rescue NotImplementedError
|
|
|
|
@symlinkfile = nil
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
File.link(@file, @hardlinkfile)
|
2008-05-20 12:48:32 -04:00
|
|
|
rescue NotImplementedError, Errno::EINVAL # EINVAL for Windows Vista
|
2008-02-03 07:14:30 -05:00
|
|
|
@hardlinkfile = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
GC.start
|
|
|
|
FileUtils.remove_entry_secure @dir
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_file(content, file = @file)
|
|
|
|
open(file, "w") {|fh| fh << content }
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
def make_tmp_filename(prefix)
|
|
|
|
@hardlinkfile = @dir + "/" + prefix + File.basename(__FILE__) + ".#{$$}.test"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_path
|
|
|
|
file = @file
|
|
|
|
|
2009-02-17 22:38:42 -05:00
|
|
|
assert_equal(file, File.open(file) {|f| f.path})
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(file, File.path(file))
|
|
|
|
o = Object.new
|
|
|
|
class << o; self; end.class_eval do
|
|
|
|
define_method(:to_path) { file }
|
|
|
|
end
|
|
|
|
assert_equal(file, File.path(o))
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_integer(n)
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
assert(n.is_a?(Integer), n.inspect + " is not Fixnum.")
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_integer_or_nil(n)
|
|
|
|
assert(n.is_a?(Integer) || n.equal?(nil), n.inspect + " is neither Fixnum nor nil.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat
|
|
|
|
sleep(@time - Time.now + 1.1)
|
|
|
|
make_file("foo", @file + "2")
|
|
|
|
fs1, fs2 = File.stat(@file), File.stat(@file + "2")
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_equal(0, fs1 <=> fs1)
|
|
|
|
assert_equal(-1, fs1 <=> fs2)
|
|
|
|
assert_equal(1, fs2 <=> fs1)
|
|
|
|
assert_nil(fs1 <=> nil)
|
|
|
|
assert_integer(fs1.dev)
|
|
|
|
assert_integer_or_nil(fs1.rdev)
|
|
|
|
assert_integer_or_nil(fs1.dev_major)
|
|
|
|
assert_integer_or_nil(fs1.dev_minor)
|
|
|
|
assert_integer_or_nil(fs1.rdev_major)
|
|
|
|
assert_integer_or_nil(fs1.rdev_minor)
|
|
|
|
assert_integer(fs1.ino)
|
|
|
|
assert_integer(fs1.mode)
|
2010-05-04 12:02:17 -04:00
|
|
|
unless /emx|mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
# on Windows, nlink is always 1. but this behavior will be changed
|
|
|
|
# in the future.
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(@hardlinkfile ? 2 : 1, fs1.nlink)
|
|
|
|
end
|
|
|
|
assert_integer(fs1.uid)
|
|
|
|
assert_integer(fs1.gid)
|
|
|
|
assert_equal(3, fs1.size)
|
|
|
|
assert_integer_or_nil(fs1.blksize)
|
|
|
|
assert_integer_or_nil(fs1.blocks)
|
|
|
|
assert_kind_of(Time, fs1.atime)
|
|
|
|
assert_kind_of(Time, fs1.mtime)
|
|
|
|
assert_kind_of(Time, fs1.ctime)
|
|
|
|
assert_kind_of(String, fs1.inspect)
|
|
|
|
end
|
|
|
|
assert_raise(Errno::ENOENT) { File.stat(@nofile) }
|
2009-02-17 22:38:42 -05:00
|
|
|
assert_kind_of(File::Stat, File.open(@file) {|f| f.stat})
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_raise(Errno::ENOENT) { File.lstat(@nofile) }
|
2009-02-17 22:38:42 -05:00
|
|
|
assert_kind_of(File::Stat, File.open(@file) {|f| f.lstat})
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
2012-08-14 02:39:46 -04:00
|
|
|
def test_stat_drive_root
|
|
|
|
assert_nothing_raised { File.stat(DRIVE + "/") }
|
|
|
|
assert_nothing_raised { File.stat(DRIVE + "/.") }
|
|
|
|
assert_nothing_raised { File.stat(DRIVE + "/..") }
|
|
|
|
assert_raise(Errno::ENOENT) { File.stat(DRIVE + "/...") }
|
|
|
|
# want to test the root of empty drive, but there is no method to test it...
|
|
|
|
end if DRIVE
|
|
|
|
|
2012-08-14 03:03:18 -04:00
|
|
|
def test_stat_dotted_prefix
|
|
|
|
Dir.mktmpdir do |dir|
|
|
|
|
prefix = File.join(dir, "...a")
|
|
|
|
Dir.mkdir(prefix)
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.exist?(prefix)
|
2012-08-14 03:03:18 -04:00
|
|
|
|
|
|
|
assert_nothing_raised { File.stat(prefix) }
|
|
|
|
|
|
|
|
Dir.chdir(dir) do
|
|
|
|
assert_nothing_raised { File.stat(File.basename(prefix)) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM
|
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
def test_directory_p
|
|
|
|
assert(File.directory?(@dir))
|
2009-01-12 02:45:31 -05:00
|
|
|
assert(!(File.directory?(@dir+"/...")))
|
2008-02-03 07:14:30 -05:00
|
|
|
assert(!(File.directory?(@file)))
|
|
|
|
assert(!(File.directory?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pipe_p ## xxx
|
|
|
|
assert(!(File.pipe?(@dir)))
|
|
|
|
assert(!(File.pipe?(@file)))
|
|
|
|
assert(!(File.pipe?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_symlink_p
|
|
|
|
assert(!(File.symlink?(@dir)))
|
|
|
|
assert(!(File.symlink?(@file)))
|
|
|
|
assert(File.symlink?(@symlinkfile)) if @symlinkfile
|
|
|
|
assert(!(File.symlink?(@hardlinkfile))) if @hardlinkfile
|
|
|
|
assert(!(File.symlink?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_socket_p ## xxx
|
|
|
|
assert(!(File.socket?(@dir)))
|
|
|
|
assert(!(File.socket?(@file)))
|
|
|
|
assert(!(File.socket?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_blockdev_p ## xxx
|
|
|
|
assert(!(File.blockdev?(@dir)))
|
|
|
|
assert(!(File.blockdev?(@file)))
|
|
|
|
assert(!(File.blockdev?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chardev_p ## xxx
|
|
|
|
assert(!(File.chardev?(@dir)))
|
|
|
|
assert(!(File.chardev?(@file)))
|
|
|
|
assert(!(File.chardev?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exist_p
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.exist?(@dir)
|
|
|
|
assert_file.exist?(@file)
|
|
|
|
assert_file.not_exist?(@nofile)
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_readable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0200, @file)
|
|
|
|
assert(!(File.readable?(@file)))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File.readable?(@file))
|
|
|
|
assert(!(File.readable?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_readable_real_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0200, @file)
|
|
|
|
assert(!(File.readable_real?(@file)))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File.readable_real?(@file))
|
|
|
|
assert(!(File.readable_real?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_world_readable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0006, @file)
|
|
|
|
assert(File.world_readable?(@file))
|
|
|
|
File.chmod(0060, @file)
|
|
|
|
assert(!(File.world_readable?(@file)))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File.world_readable?(@file)))
|
|
|
|
assert(!(File.world_readable?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_writable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0400, @file)
|
|
|
|
assert(!(File.writable?(@file)))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File.writable?(@file))
|
|
|
|
assert(!(File.writable?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_writable_real_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0400, @file)
|
|
|
|
assert(!(File.writable_real?(@file)))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File.writable_real?(@file))
|
|
|
|
assert(!(File.writable_real?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_world_writable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0006, @file)
|
|
|
|
assert(File.world_writable?(@file))
|
|
|
|
File.chmod(0060, @file)
|
|
|
|
assert(!(File.world_writable?(@file)))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File.world_writable?(@file)))
|
|
|
|
assert(!(File.world_writable?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_executable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0100, @file)
|
|
|
|
assert(File.executable?(@file))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File.executable?(@file)))
|
|
|
|
assert(!(File.executable?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_executable_real_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0100, @file)
|
|
|
|
assert(File.executable_real?(@file))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File.executable_real?(@file)))
|
|
|
|
assert(!(File.executable_real?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_p
|
|
|
|
assert(!(File.file?(@dir)))
|
|
|
|
assert(File.file?(@file))
|
|
|
|
assert(!(File.file?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_zero_p
|
|
|
|
assert_nothing_raised { File.zero?(@dir) }
|
|
|
|
assert(!(File.zero?(@file)))
|
|
|
|
assert(File.zero?(@zerofile))
|
|
|
|
assert(!(File.zero?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size_p
|
|
|
|
assert_nothing_raised { File.size?(@dir) }
|
|
|
|
assert_equal(3, File.size?(@file))
|
|
|
|
assert(!(File.size?(@zerofile)))
|
|
|
|
assert(!(File.size?(@nofile)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_owned_p ## xxx
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
assert(File.owned?(@file))
|
|
|
|
assert(File.grpowned?(@file))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_suid_sgid_sticky ## xxx
|
|
|
|
assert(!(File.setuid?(@file)))
|
|
|
|
assert(!(File.setgid?(@file)))
|
|
|
|
assert(!(File.sticky?(@file)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_identical_p
|
|
|
|
assert(File.identical?(@file, @file))
|
|
|
|
assert(!(File.identical?(@file, @zerofile)))
|
|
|
|
assert(!(File.identical?(@file, @nofile)))
|
|
|
|
assert(!(File.identical?(@nofile, @file)))
|
|
|
|
end
|
|
|
|
|
2009-03-08 12:12:09 -04:00
|
|
|
def test_s_size
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_integer(File.size(@dir))
|
|
|
|
assert_equal(3, File.size(@file))
|
|
|
|
assert_equal(0, File.size(@zerofile))
|
|
|
|
assert_raise(Errno::ENOENT) { File.size(@nofile) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ftype
|
|
|
|
assert_equal("directory", File.ftype(@dir))
|
|
|
|
assert_equal("file", File.ftype(@file))
|
|
|
|
assert_equal("link", File.ftype(@symlinkfile)) if @symlinkfile
|
|
|
|
assert_equal("file", File.ftype(@hardlinkfile)) if @hardlinkfile
|
|
|
|
assert_raise(Errno::ENOENT) { File.ftype(@nofile) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_atime
|
|
|
|
t1 = File.atime(@file)
|
2009-02-17 22:38:42 -05:00
|
|
|
t2 = File.open(@file) {|f| f.atime}
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_kind_of(Time, t1)
|
|
|
|
assert_kind_of(Time, t2)
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
assert_raise(Errno::ENOENT) { File.atime(@nofile) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mtime
|
|
|
|
t1 = File.mtime(@file)
|
2009-02-17 22:38:42 -05:00
|
|
|
t2 = File.open(@file) {|f| f.mtime}
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_kind_of(Time, t1)
|
|
|
|
assert_kind_of(Time, t2)
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
assert_raise(Errno::ENOENT) { File.mtime(@nofile) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ctime
|
|
|
|
t1 = File.ctime(@file)
|
2009-02-17 22:38:42 -05:00
|
|
|
t2 = File.open(@file) {|f| f.ctime}
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_kind_of(Time, t1)
|
|
|
|
assert_kind_of(Time, t2)
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
assert_raise(Errno::ENOENT) { File.ctime(@nofile) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chmod
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(1, File.chmod(0444, @file))
|
|
|
|
assert_equal(0444, File.stat(@file).mode % 01000)
|
2009-02-17 22:38:42 -05:00
|
|
|
assert_equal(0, File.open(@file) {|f| f.chmod(0222)})
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(0222, File.stat(@file).mode % 01000)
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert_raise(Errno::ENOENT) { File.chmod(0600, @nofile) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lchmod
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(1, File.lchmod(0444, @file))
|
|
|
|
assert_equal(0444, File.stat(@file).mode % 01000)
|
|
|
|
File.lchmod(0600, @file)
|
|
|
|
assert_raise(Errno::ENOENT) { File.lchmod(0600, @nofile) }
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chown ## xxx
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lchown ## xxx
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_symlink
|
|
|
|
return unless @symlinkfile
|
|
|
|
assert_equal("link", File.ftype(@symlinkfile))
|
|
|
|
assert_raise(Errno::EEXIST) { File.symlink(@file, @file) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_utime
|
|
|
|
t = Time.local(2000)
|
|
|
|
File.utime(t + 1, t + 2, @zerofile)
|
|
|
|
assert_equal(t + 1, File.atime(@zerofile))
|
|
|
|
assert_equal(t + 2, File.mtime(@zerofile))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hardlink
|
|
|
|
return unless @hardlinkfile
|
|
|
|
assert_equal("file", File.ftype(@hardlinkfile))
|
|
|
|
assert_raise(Errno::EEXIST) { File.link(@file, @file) }
|
|
|
|
end
|
|
|
|
|
2010-09-04 10:48:55 -04:00
|
|
|
def test_readlink
|
2008-02-03 07:14:30 -05:00
|
|
|
return unless @symlinkfile
|
|
|
|
assert_equal(@file, File.readlink(@symlinkfile))
|
|
|
|
assert_raise(Errno::EINVAL) { File.readlink(@file) }
|
|
|
|
assert_raise(Errno::ENOENT) { File.readlink(@nofile) }
|
2010-09-04 10:48:55 -04:00
|
|
|
if fs = Encoding.find("filesystem")
|
|
|
|
assert_equal(fs, File.readlink(@symlinkfile).encoding)
|
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink
|
|
|
|
assert_equal(1, File.unlink(@file))
|
|
|
|
make_file("foo", @file)
|
|
|
|
assert_raise(Errno::ENOENT) { File.unlink(@nofile) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rename
|
|
|
|
assert_equal(0, File.rename(@file, @nofile))
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.not_exist?(@file)
|
|
|
|
assert_file.exist?(@nofile)
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(0, File.rename(@nofile, @file))
|
|
|
|
assert_raise(Errno::ENOENT) { File.rename(@nofile, @file) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_umask
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
prev = File.umask(0777)
|
|
|
|
assert_equal(0777, File.umask)
|
|
|
|
open(@nofile, "w") { }
|
|
|
|
assert_equal(0, File.stat(@nofile).mode % 01000)
|
|
|
|
File.unlink(@nofile)
|
|
|
|
assert_equal(0777, File.umask(prev))
|
|
|
|
assert_raise(ArgumentError) { File.umask(0, 1, 2) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path
|
|
|
|
assert_equal(@file, File.expand_path(File.basename(@file), File.dirname(@file)))
|
2008-05-15 02:34:02 -04:00
|
|
|
if /cygwin|mingw|mswin|bccwin/ =~ RUBY_PLATFORM
|
|
|
|
assert_equal(@file, File.expand_path(@file + " "))
|
|
|
|
assert_equal(@file, File.expand_path(@file + "."))
|
|
|
|
assert_equal(@file, File.expand_path(@file + "::$DATA"))
|
2010-08-01 23:25:12 -04:00
|
|
|
assert_match(/\Ac:\//i, File.expand_path('c:'), '[ruby-core:31591]')
|
2012-01-19 01:01:29 -05:00
|
|
|
assert_match(/\Ac:\//i, File.expand_path('c:foo', 'd:/bar'))
|
2012-01-19 01:20:49 -05:00
|
|
|
assert_match(%r'\Ac:/bar/foo\z'i, File.expand_path('c:foo', 'c:/bar'))
|
2008-05-15 02:34:02 -04:00
|
|
|
end
|
2012-01-24 21:32:06 -05:00
|
|
|
if DRIVE
|
2012-01-19 01:20:49 -05:00
|
|
|
assert_match(%r"\Az:/foo\z"i, File.expand_path('/foo', "z:/bar"))
|
2012-01-19 01:28:48 -05:00
|
|
|
assert_match(%r"\A//host/share/foo\z"i, File.expand_path('/foo', "//host/share/bar"))
|
2012-01-24 21:32:06 -05:00
|
|
|
assert_match(%r"\A#{DRIVE}/foo\z"i, File.expand_path('/foo'))
|
2012-01-19 01:20:49 -05:00
|
|
|
else
|
|
|
|
assert_equal("/foo", File.expand_path('/foo'))
|
2012-01-19 01:01:29 -05:00
|
|
|
end
|
2012-08-06 21:48:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_encoding
|
2012-01-24 21:32:06 -05:00
|
|
|
drive = (DRIVE ? 'C:' : '')
|
|
|
|
if Encoding.find("filesystem") == Encoding::CP1251
|
|
|
|
a = "#{drive}/\u3042\u3044\u3046\u3048\u304a".encode("cp932")
|
|
|
|
else
|
|
|
|
a = "#{drive}/\u043f\u0440\u0438\u0432\u0435\u0442".encode("cp1251")
|
|
|
|
end
|
|
|
|
assert_equal(a, File.expand_path(a))
|
|
|
|
a = "#{drive}/\225\\\\"
|
|
|
|
if File::ALT_SEPARATOR == '\\'
|
|
|
|
[%W"cp437 #{drive}/\225", %W"cp932 #{drive}/\225\\"]
|
|
|
|
else
|
|
|
|
[["cp437", a], ["cp932", a]]
|
|
|
|
end.each do |cp, expected|
|
|
|
|
assert_equal(expected.force_encoding(cp), File.expand_path(a.dup.force_encoding(cp)), cp)
|
|
|
|
end
|
|
|
|
|
2012-08-23 23:44:56 -04:00
|
|
|
path = "\u3042\u3044\u3046\u3048\u304a".encode("EUC-JP")
|
|
|
|
assert_equal("#{Dir.pwd}/#{path}".encode("CP932"), File.expand_path(path).encode("CP932"))
|
|
|
|
|
|
|
|
path = "\u3042\u3044\u3046\u3048\u304a".encode("CP51932")
|
|
|
|
assert_equal("#{Dir.pwd}/#{path}", File.expand_path(path))
|
|
|
|
|
2012-08-06 21:48:36 -04:00
|
|
|
assert_incompatible_encoding {|d| File.expand_path(d)}
|
|
|
|
end
|
|
|
|
|
2012-08-23 23:44:56 -04:00
|
|
|
def test_expand_path_encoding_filesystem
|
2012-08-29 18:45:24 -04:00
|
|
|
home = ENV["HOME"]
|
|
|
|
ENV["HOME"] = "#{DRIVE}/UserHome"
|
|
|
|
|
2012-08-23 23:44:56 -04:00
|
|
|
path = "~".encode("US-ASCII")
|
|
|
|
dir = "C:/".encode("IBM437")
|
|
|
|
fs = Encoding.find("filesystem")
|
|
|
|
|
|
|
|
assert_equal fs, File.expand_path(path).encoding
|
|
|
|
assert_equal fs, File.expand_path(path, dir).encoding
|
2012-08-29 18:45:24 -04:00
|
|
|
ensure
|
|
|
|
ENV["HOME"] = home
|
2012-08-23 23:44:56 -04:00
|
|
|
end
|
|
|
|
|
2012-08-06 21:48:36 -04:00
|
|
|
def test_expand_path_home
|
2010-09-16 02:03:13 -04:00
|
|
|
assert_kind_of(String, File.expand_path("~")) if ENV["HOME"]
|
2010-07-29 19:33:21 -04:00
|
|
|
assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha") }
|
|
|
|
assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha", "/") }
|
2010-07-29 19:51:53 -04:00
|
|
|
begin
|
|
|
|
bug3630 = '[ruby-core:31537]'
|
|
|
|
home = ENV["HOME"]
|
2012-08-23 23:44:56 -04:00
|
|
|
home_drive = ENV["HOMEDRIVE"]
|
|
|
|
home_path = ENV["HOMEPATH"]
|
|
|
|
user_profile = ENV["USERPROFILE"]
|
2010-07-29 19:51:53 -04:00
|
|
|
ENV["HOME"] = nil
|
2012-08-23 23:44:56 -04:00
|
|
|
ENV["HOMEDRIVE"] = nil
|
|
|
|
ENV["HOMEPATH"] = nil
|
|
|
|
ENV["USERPROFILE"] = nil
|
2010-07-29 19:51:53 -04:00
|
|
|
assert_raise(ArgumentError) { File.expand_path("~") }
|
|
|
|
ENV["HOME"] = "~"
|
|
|
|
assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
|
|
|
|
ENV["HOME"] = "."
|
|
|
|
assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
|
|
|
|
ensure
|
|
|
|
ENV["HOME"] = home
|
2012-08-23 23:44:56 -04:00
|
|
|
ENV["HOMEDRIVE"] = home_drive
|
|
|
|
ENV["HOMEPATH"] = home_path
|
|
|
|
ENV["USERPROFILE"] = user_profile
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-10 10:39:09 -04:00
|
|
|
def test_expand_path_home_dir_string
|
|
|
|
home = ENV["HOME"]
|
|
|
|
new_home = "#{DRIVE}/UserHome"
|
|
|
|
ENV["HOME"] = new_home
|
|
|
|
bug8034 = "[ruby-core:53168]"
|
|
|
|
|
|
|
|
assert_equal File.join(new_home, "foo"), File.expand_path("foo", "~")
|
|
|
|
assert_equal File.join(new_home, "bar", "foo"), File.expand_path("foo", "~/bar")
|
|
|
|
ensure
|
|
|
|
ENV["HOME"] = home
|
|
|
|
end if DRIVE
|
|
|
|
|
2012-08-23 23:44:56 -04:00
|
|
|
def test_expand_path_remove_trailing_alternative_data
|
|
|
|
assert_equal File.join(@rootdir, "aaa"), File.expand_path("#{@rootdir}/aaa::$DATA")
|
|
|
|
assert_equal File.join(@rootdir, "aa:a"), File.expand_path("#{@rootdir}/aa:a:$DATA")
|
|
|
|
assert_equal File.join(@rootdir, "aaa:$DATA"), File.expand_path("#{@rootdir}/aaa:$DATA")
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_resolve_empty_string_current_directory
|
|
|
|
assert_equal(Dir.pwd, File.expand_path(""))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_resolve_dot_current_directory
|
|
|
|
assert_equal(Dir.pwd, File.expand_path("."))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_resolve_file_name_relative_current_directory
|
|
|
|
assert_equal(File.join(Dir.pwd, "foo"), File.expand_path("foo"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ignore_nil_dir_string
|
|
|
|
assert_equal(File.join(Dir.pwd, "foo"), File.expand_path("foo", nil))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_resolve_file_name_and_dir_string_relative
|
|
|
|
assert_equal(File.join(Dir.pwd, "bar", "foo"),
|
|
|
|
File.expand_path("foo", "bar"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_cleanup_dots_file_name
|
|
|
|
bug = "[ruby-talk:18512]"
|
|
|
|
|
|
|
|
assert_equal(File.join(Dir.pwd, ".a"), File.expand_path(".a"), bug)
|
|
|
|
assert_equal(File.join(Dir.pwd, "..a"), File.expand_path("..a"), bug)
|
|
|
|
|
|
|
|
if DRIVE
|
|
|
|
# cleanup dots only on Windows
|
|
|
|
assert_equal(File.join(Dir.pwd, "a"), File.expand_path("a."), bug)
|
|
|
|
assert_equal(File.join(Dir.pwd, "a"), File.expand_path("a.."), bug)
|
|
|
|
else
|
|
|
|
assert_equal(File.join(Dir.pwd, "a."), File.expand_path("a."), bug)
|
|
|
|
assert_equal(File.join(Dir.pwd, "a.."), File.expand_path("a.."), bug)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_converts_a_pathname_to_an_absolute_pathname_using_a_complete_path
|
|
|
|
assert_equal(@dir, File.expand_path("", "#{@dir}"))
|
|
|
|
assert_equal(File.join(@dir, "a"), File.expand_path("a", "#{@dir}"))
|
|
|
|
assert_equal(File.join(@dir, "a"), File.expand_path("../a", "#{@dir}/xxx"))
|
|
|
|
assert_equal(@rootdir, File.expand_path(".", "#{@rootdir}"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_ignores_supplied_dir_if_path_contains_a_drive_letter
|
|
|
|
assert_equal(@rootdir, File.expand_path(@rootdir, "D:/"))
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_removes_trailing_slashes_from_absolute_path
|
|
|
|
assert_equal(File.join(@rootdir, "foo"), File.expand_path("#{@rootdir}foo/"))
|
|
|
|
assert_equal(File.join(@rootdir, "foo.rb"), File.expand_path("#{@rootdir}foo.rb/"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_removes_trailing_spaces_from_absolute_path
|
|
|
|
assert_equal(File.join(@rootdir, "a"), File.expand_path("#{@rootdir}a "))
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_converts_a_pathname_which_starts_with_a_slash_using_dir_s_drive
|
|
|
|
assert_match(%r"\Az:/foo\z"i, File.expand_path('/foo', "z:/bar"))
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_converts_a_pathname_which_starts_with_a_slash_and_unc_pathname
|
|
|
|
assert_equal("//foo", File.expand_path('//foo', "//bar"))
|
|
|
|
assert_equal("//bar/foo", File.expand_path('/foo', "//bar"))
|
|
|
|
assert_equal("//foo", File.expand_path('//foo', "/bar"))
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_converts_a_dot_with_unc_dir
|
|
|
|
assert_equal("//", File.expand_path('.', "//"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_preserves_unc_path_root
|
|
|
|
assert_equal("//", File.expand_path("//"))
|
|
|
|
assert_equal("//", File.expand_path("//."))
|
|
|
|
assert_equal("//", File.expand_path("//.."))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_converts_a_pathname_which_starts_with_a_slash_using_host_share
|
|
|
|
assert_match(%r"\A//host/share/foo\z"i, File.expand_path('/foo', "//host/share/bar"))
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_converts_a_pathname_which_starts_with_a_slash_using_a_current_drive
|
|
|
|
assert_match(%r"\A#{DRIVE}/foo\z"i, File.expand_path('/foo'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_returns_tainted_strings_or_not
|
|
|
|
assert_equal(true, File.expand_path('foo').tainted?)
|
|
|
|
assert_equal(true, File.expand_path('foo'.taint).tainted?)
|
|
|
|
assert_equal(true, File.expand_path('/foo'.taint).tainted?)
|
|
|
|
assert_equal(true, File.expand_path('foo', 'bar').tainted?)
|
|
|
|
assert_equal(true, File.expand_path('foo', '/bar'.taint).tainted?)
|
|
|
|
assert_equal(true, File.expand_path('foo'.taint, '/bar').tainted?)
|
2012-08-29 18:45:24 -04:00
|
|
|
assert_equal(true, File.expand_path('~').tainted?) if ENV["HOME"]
|
2012-08-23 23:44:56 -04:00
|
|
|
|
|
|
|
if DRIVE
|
|
|
|
assert_equal(true, File.expand_path('/foo').tainted?)
|
|
|
|
assert_equal(false, File.expand_path('//foo').tainted?)
|
|
|
|
assert_equal(true, File.expand_path('C:/foo'.taint).tainted?)
|
|
|
|
assert_equal(false, File.expand_path('C:/foo').tainted?)
|
|
|
|
assert_equal(true, File.expand_path('foo', '/bar').tainted?)
|
|
|
|
assert_equal(true, File.expand_path('foo', 'C:/bar'.taint).tainted?)
|
|
|
|
assert_equal(true, File.expand_path('foo'.taint, 'C:/bar').tainted?)
|
|
|
|
assert_equal(false, File.expand_path('foo', 'C:/bar').tainted?)
|
|
|
|
assert_equal(false, File.expand_path('C:/foo/../bar').tainted?)
|
|
|
|
assert_equal(false, File.expand_path('foo', '//bar').tainted?)
|
|
|
|
else
|
|
|
|
assert_equal(false, File.expand_path('/foo').tainted?)
|
|
|
|
assert_equal(false, File.expand_path('foo', '/bar').tainted?)
|
2010-07-29 19:51:53 -04:00
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
2012-08-23 23:44:56 -04:00
|
|
|
def test_expand_path_converts_a_pathname_to_an_absolute_pathname_using_home_as_base
|
|
|
|
old_home = ENV["HOME"]
|
|
|
|
home = ENV["HOME"] = "#{DRIVE}/UserHome"
|
|
|
|
assert_equal(home, File.expand_path("~"))
|
|
|
|
assert_equal(home, File.expand_path("~", "C:/FooBar"))
|
|
|
|
assert_equal(File.join(home, "a"), File.expand_path("~/a", "C:/FooBar"))
|
|
|
|
ensure
|
|
|
|
ENV["HOME"] = old_home
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_converts_a_pathname_to_an_absolute_pathname_using_unc_home
|
|
|
|
old_home = ENV["HOME"]
|
|
|
|
unc_home = ENV["HOME"] = "//UserHome"
|
|
|
|
assert_equal(unc_home, File.expand_path("~"))
|
|
|
|
ensure
|
|
|
|
ENV["HOME"] = old_home
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_does_not_modify_a_home_string_argument
|
|
|
|
old_home = ENV["HOME"]
|
|
|
|
home = ENV["HOME"] = "#{DRIVE}/UserHome"
|
|
|
|
str = "~/a"
|
|
|
|
assert_equal("#{home}/a", File.expand_path(str))
|
|
|
|
assert_equal("~/a", str)
|
|
|
|
ensure
|
|
|
|
ENV["HOME"] = old_home
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_raises_argument_error_for_any_supplied_username
|
|
|
|
bug = '[ruby-core:39597]'
|
|
|
|
assert_raise(ArgumentError, bug) { File.expand_path("~anything") }
|
|
|
|
end if DRIVE
|
|
|
|
|
|
|
|
def test_expand_path_raises_a_type_error_if_not_passed_a_string_type
|
|
|
|
assert_raise(TypeError) { File.expand_path(1) }
|
|
|
|
assert_raise(TypeError) { File.expand_path(nil) }
|
|
|
|
assert_raise(TypeError) { File.expand_path(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_expands_dot_dir
|
|
|
|
assert_equal("#{DRIVE}/dir", File.expand_path("#{DRIVE}/./dir"))
|
|
|
|
end
|
|
|
|
|
2012-11-17 07:46:17 -05:00
|
|
|
def test_expand_path_does_not_expand_wildcards
|
2012-11-16 17:43:22 -05:00
|
|
|
assert_equal("#{DRIVE}/*", File.expand_path("./*", "#{DRIVE}/"))
|
|
|
|
assert_equal("#{Dir.pwd}/*", File.expand_path("./*", Dir.pwd))
|
2012-11-17 07:46:17 -05:00
|
|
|
assert_equal("#{DRIVE}/?", File.expand_path("./?", "#{DRIVE}/"))
|
|
|
|
assert_equal("#{Dir.pwd}/?", File.expand_path("./?", Dir.pwd))
|
2012-11-16 17:43:22 -05:00
|
|
|
end if DRIVE
|
|
|
|
|
2012-08-23 23:44:56 -04:00
|
|
|
def test_expand_path_does_not_modify_the_string_argument
|
|
|
|
str = "./a/b/../c"
|
|
|
|
assert_equal("#{Dir.pwd}/a/c", File.expand_path(str, Dir.pwd))
|
|
|
|
assert_equal("./a/b/../c", str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_returns_a_string_when_passed_a_string_subclass
|
|
|
|
sub = Class.new(String)
|
|
|
|
str = sub.new "./a/b/../c"
|
|
|
|
path = File.expand_path(str, Dir.pwd)
|
|
|
|
assert_equal("#{Dir.pwd}/a/c", path)
|
|
|
|
assert_instance_of(String, path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_path_accepts_objects_that_have_a_to_path_method
|
|
|
|
klass = Class.new { def to_path; "a/b/c"; end }
|
|
|
|
obj = klass.new
|
|
|
|
assert_equal("#{Dir.pwd}/a/b/c", File.expand_path(obj))
|
|
|
|
end
|
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
def test_basename
|
|
|
|
assert_equal(File.basename(@file).sub(/\.test$/, ""), File.basename(@file, ".test"))
|
2009-07-08 03:47:09 -04:00
|
|
|
assert_equal("", s = File.basename(""))
|
|
|
|
assert(!s.frozen?, '[ruby-core:24199]')
|
|
|
|
assert_equal("foo", s = File.basename("foo"))
|
|
|
|
assert(!s.frozen?, '[ruby-core:24199]')
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal("foo", File.basename("foo", ".ext"))
|
|
|
|
assert_equal("foo", File.basename("foo.ext", ".ext"))
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
assert_equal("foo", File.basename("foo.ext", ".*"))
|
2008-05-15 02:34:02 -04:00
|
|
|
if /cygwin|mingw|mswin|bccwin/ =~ RUBY_PLATFORM
|
|
|
|
basename = File.basename(@file)
|
|
|
|
assert_equal(basename, File.basename(@file + " "))
|
|
|
|
assert_equal(basename, File.basename(@file + "."))
|
|
|
|
assert_equal(basename, File.basename(@file + "::$DATA"))
|
|
|
|
basename.chomp!(".test")
|
|
|
|
assert_equal(basename, File.basename(@file + " ", ".test"))
|
|
|
|
assert_equal(basename, File.basename(@file + ".", ".test"))
|
|
|
|
assert_equal(basename, File.basename(@file + "::$DATA", ".test"))
|
|
|
|
assert_equal(basename, File.basename(@file + " ", ".*"))
|
|
|
|
assert_equal(basename, File.basename(@file + ".", ".*"))
|
|
|
|
assert_equal(basename, File.basename(@file + "::$DATA", ".*"))
|
|
|
|
end
|
2012-01-24 21:32:06 -05:00
|
|
|
if File::ALT_SEPARATOR == '\\'
|
|
|
|
a = "foo/\225\\\\"
|
|
|
|
[%W"cp437 \225", %W"cp932 \225\\"].each do |cp, expected|
|
|
|
|
assert_equal(expected.force_encoding(cp), File.basename(a.dup.force_encoding(cp)), cp)
|
|
|
|
end
|
|
|
|
end
|
2009-09-19 04:33:01 -04:00
|
|
|
|
|
|
|
assert_incompatible_encoding {|d| File.basename(d)}
|
2010-03-12 10:44:24 -05:00
|
|
|
assert_incompatible_encoding {|d| File.basename(d, ".*")}
|
|
|
|
assert_raise(Encoding::CompatibilityError) {File.basename("foo.ext", ".*".encode("utf-16le"))}
|
2012-01-24 21:32:06 -05:00
|
|
|
|
|
|
|
s = "foo\x93_a".force_encoding("cp932")
|
|
|
|
assert_equal(s, File.basename(s, "_a"))
|
2012-01-24 23:27:45 -05:00
|
|
|
|
|
|
|
s = "\u4032.\u3024"
|
|
|
|
assert_equal(s, File.basename(s, ".\x95\\".force_encoding("cp932")))
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dirname
|
|
|
|
assert(@file.start_with?(File.dirname(@file)))
|
|
|
|
assert_equal(".", File.dirname(""))
|
2009-09-19 04:33:01 -04:00
|
|
|
assert_incompatible_encoding {|d| File.dirname(d)}
|
2012-01-24 21:32:06 -05:00
|
|
|
if File::ALT_SEPARATOR == '\\'
|
|
|
|
a = "\225\\\\foo"
|
|
|
|
[%W"cp437 \225", %W"cp932 \225\\"].each do |cp, expected|
|
|
|
|
assert_equal(expected.force_encoding(cp), File.dirname(a.dup.force_encoding(cp)), cp)
|
|
|
|
end
|
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_extname
|
2010-04-29 01:33:43 -04:00
|
|
|
assert_equal(".test", File.extname(@file))
|
2009-02-17 22:38:42 -05:00
|
|
|
prefixes = ["", "/", ".", "/.", "bar/.", "/bar/."]
|
|
|
|
infixes = ["", " ", "."]
|
|
|
|
infixes2 = infixes + [".ext "]
|
|
|
|
appendixes = [""]
|
2008-05-15 02:34:02 -04:00
|
|
|
if /cygwin|mingw|mswin|bccwin/ =~ RUBY_PLATFORM
|
2009-02-17 22:38:42 -05:00
|
|
|
appendixes << " " << "." << "::$DATA" << "::$DATA.bar"
|
|
|
|
end
|
|
|
|
prefixes.each do |prefix|
|
|
|
|
appendixes.each do |appendix|
|
|
|
|
infixes.each do |infix|
|
|
|
|
path = "#{prefix}foo#{infix}#{appendix}"
|
|
|
|
assert_equal("", File.extname(path), "File.extname(#{path.inspect})")
|
|
|
|
end
|
|
|
|
infixes2.each do |infix|
|
|
|
|
path = "#{prefix}foo#{infix}.ext#{appendix}"
|
|
|
|
assert_equal(".ext", File.extname(path), "File.extname(#{path.inspect})")
|
|
|
|
end
|
|
|
|
end
|
2008-05-15 02:34:02 -04:00
|
|
|
end
|
2010-04-19 11:43:05 -04:00
|
|
|
bug3175 = '[ruby-core:29627]'
|
|
|
|
assert_equal(".rb", File.extname("/tmp//bla.rb"), bug3175)
|
2009-09-19 04:33:01 -04:00
|
|
|
|
|
|
|
assert_incompatible_encoding {|d| File.extname(d)}
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_split
|
|
|
|
d, b = File.split(@file)
|
|
|
|
assert_equal(File.dirname(@file), d)
|
|
|
|
assert_equal(File.basename(@file), b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_join
|
|
|
|
s = "foo" + File::SEPARATOR + "bar" + File::SEPARATOR + "baz"
|
|
|
|
assert_equal(s, File.join("foo", "bar", "baz"))
|
|
|
|
assert_equal(s, File.join(["foo", "bar", "baz"]))
|
2012-10-15 21:54:05 -04:00
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
o = Object.new
|
|
|
|
def o.to_path; "foo"; end
|
|
|
|
assert_equal(s, File.join(o, "bar", "baz"))
|
|
|
|
assert_equal(s, File.join("foo" + File::SEPARATOR, "bar", File::SEPARATOR + "baz"))
|
2012-10-15 21:54:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_join_alt_separator
|
2012-01-24 21:32:06 -05:00
|
|
|
if File::ALT_SEPARATOR == '\\'
|
|
|
|
a = "\225\\"
|
|
|
|
b = "foo"
|
|
|
|
[%W"cp437 \225\\foo", %W"cp932 \225\\/foo"].each do |cp, expected|
|
|
|
|
assert_equal(expected.force_encoding(cp), File.join(a.dup.force_encoding(cp), b.dup.force_encoding(cp)), cp)
|
|
|
|
end
|
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
2012-10-15 21:54:05 -04:00
|
|
|
def test_join_ascii_incompatible
|
|
|
|
bug7168 = '[ruby-core:48012]'
|
|
|
|
names = %w"a b".map {|s| s.encode(Encoding::UTF_16LE)}
|
|
|
|
assert_raise(Encoding::CompatibilityError, bug7168) {File.join(*names)}
|
2012-10-15 22:27:07 -04:00
|
|
|
assert_raise(Encoding::CompatibilityError, bug7168) {File.join(names)}
|
|
|
|
|
|
|
|
a = Object.new
|
|
|
|
b = names[1]
|
|
|
|
names = [a, "b"]
|
|
|
|
a.singleton_class.class_eval do
|
|
|
|
define_method(:to_path) do
|
|
|
|
names[1] = b
|
|
|
|
"a"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_raise(Encoding::CompatibilityError, bug7168) {File.join(names)}
|
2012-10-15 21:54:05 -04:00
|
|
|
end
|
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
def test_truncate
|
|
|
|
assert_equal(0, File.truncate(@file, 1))
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.exist?(@file)
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(1, File.size(@file))
|
|
|
|
assert_equal(0, File.truncate(@file, 0))
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.exist?(@file)
|
|
|
|
assert_file.zero?(@file)
|
2008-02-03 07:14:30 -05:00
|
|
|
make_file("foo", @file)
|
|
|
|
assert_raise(Errno::ENOENT) { File.truncate(@nofile, 0) }
|
|
|
|
|
|
|
|
f = File.new(@file, "w")
|
|
|
|
assert_equal(0, f.truncate(2))
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.exist?(@file)
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(2, File.size(@file))
|
|
|
|
assert_equal(0, f.truncate(0))
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.exist?(@file)
|
|
|
|
assert_file.zero?(@file)
|
2008-02-03 07:14:30 -05:00
|
|
|
f.close
|
|
|
|
make_file("foo", @file)
|
|
|
|
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_raise(IOError) { File.open(@file) {|ff| ff.truncate(0)} }
|
2008-02-03 07:14:30 -05:00
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_flock ## xxx
|
2008-02-10 16:52:25 -05:00
|
|
|
f = File.new(@file, "r+")
|
2008-02-03 07:14:30 -05:00
|
|
|
f.flock(File::LOCK_EX)
|
|
|
|
f.flock(File::LOCK_SH)
|
|
|
|
f.flock(File::LOCK_UN)
|
|
|
|
f.close
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_test
|
|
|
|
sleep(@time - Time.now + 1.1)
|
|
|
|
make_file("foo", @file + "2")
|
|
|
|
[@dir, @file, @zerofile, @symlinkfile, @hardlinkfile].compact.each do |f|
|
|
|
|
assert_equal(File.atime(f), test(?A, f))
|
|
|
|
assert_equal(File.ctime(f), test(?C, f))
|
|
|
|
assert_equal(File.mtime(f), test(?M, f))
|
|
|
|
assert_equal(File.blockdev?(f), test(?b, f))
|
|
|
|
assert_equal(File.chardev?(f), test(?c, f))
|
|
|
|
assert_equal(File.directory?(f), test(?d, f))
|
|
|
|
assert_equal(File.exist?(f), test(?e, f))
|
|
|
|
assert_equal(File.file?(f), test(?f, f))
|
|
|
|
assert_equal(File.setgid?(f), test(?g, f))
|
|
|
|
assert_equal(File.grpowned?(f), test(?G, f))
|
|
|
|
assert_equal(File.sticky?(f), test(?k, f))
|
|
|
|
assert_equal(File.symlink?(f), test(?l, f))
|
|
|
|
assert_equal(File.owned?(f), test(?o, f))
|
|
|
|
assert_nothing_raised { test(?O, f) }
|
|
|
|
assert_equal(File.pipe?(f), test(?p, f))
|
|
|
|
assert_equal(File.readable?(f), test(?r, f))
|
|
|
|
assert_equal(File.readable_real?(f), test(?R, f))
|
|
|
|
assert_equal(File.size?(f), test(?s, f))
|
|
|
|
assert_equal(File.socket?(f), test(?S, f))
|
|
|
|
assert_equal(File.setuid?(f), test(?u, f))
|
|
|
|
assert_equal(File.writable?(f), test(?w, f))
|
2010-06-04 19:30:48 -04:00
|
|
|
assert_equal(File.writable_real?(f), test(?W, f))
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(File.executable?(f), test(?x, f))
|
|
|
|
assert_equal(File.executable_real?(f), test(?X, f))
|
|
|
|
assert_equal(File.zero?(f), test(?z, f))
|
|
|
|
end
|
|
|
|
assert_equal(false, test(?-, @dir, @file))
|
|
|
|
assert_equal(true, test(?-, @file, @file))
|
|
|
|
assert_equal(true, test(?=, @file, @file))
|
|
|
|
assert_equal(false, test(?>, @file, @file))
|
|
|
|
assert_equal(false, test(?<, @file, @file))
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
unless /cygwin/ =~ RUBY_PLATFORM
|
|
|
|
assert_equal(false, test(?=, @file, @file + "2"))
|
|
|
|
assert_equal(false, test(?>, @file, @file + "2"))
|
|
|
|
assert_equal(true, test(?>, @file + "2", @file))
|
|
|
|
assert_equal(true, test(?<, @file, @file + "2"))
|
|
|
|
assert_equal(false, test(?<, @file + "2", @file))
|
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_raise(ArgumentError) { test }
|
|
|
|
assert_raise(Errno::ENOENT) { test(?A, @nofile) }
|
|
|
|
assert_raise(ArgumentError) { test(?a) }
|
|
|
|
assert_raise(ArgumentError) { test("\0".ord) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_init
|
|
|
|
sleep(@time - Time.now + 1.1)
|
|
|
|
make_file("foo", @file + "2")
|
|
|
|
fs1, fs2 = File::Stat.new(@file), File::Stat.new(@file + "2")
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_equal(0, fs1 <=> fs1)
|
|
|
|
assert_equal(-1, fs1 <=> fs2)
|
|
|
|
assert_equal(1, fs2 <=> fs1)
|
|
|
|
assert_nil(fs1 <=> nil)
|
|
|
|
assert_integer(fs1.dev)
|
|
|
|
assert_integer_or_nil(fs1.rdev)
|
|
|
|
assert_integer_or_nil(fs1.dev_major)
|
|
|
|
assert_integer_or_nil(fs1.dev_minor)
|
|
|
|
assert_integer_or_nil(fs1.rdev_major)
|
|
|
|
assert_integer_or_nil(fs1.rdev_minor)
|
|
|
|
assert_integer(fs1.ino)
|
|
|
|
assert_integer(fs1.mode)
|
2010-05-04 12:02:17 -04:00
|
|
|
unless /emx|mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
# on Windows, nlink is always 1. but this behavior will be changed
|
|
|
|
# in the future.
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(@hardlinkfile ? 2 : 1, fs1.nlink)
|
|
|
|
end
|
|
|
|
assert_integer(fs1.uid)
|
|
|
|
assert_integer(fs1.gid)
|
|
|
|
assert_equal(3, fs1.size)
|
|
|
|
assert_integer_or_nil(fs1.blksize)
|
|
|
|
assert_integer_or_nil(fs1.blocks)
|
|
|
|
assert_kind_of(Time, fs1.atime)
|
|
|
|
assert_kind_of(Time, fs1.mtime)
|
|
|
|
assert_kind_of(Time, fs1.ctime)
|
|
|
|
assert_kind_of(String, fs1.inspect)
|
|
|
|
end
|
|
|
|
assert_raise(Errno::ENOENT) { File::Stat.new(@nofile) }
|
|
|
|
assert_kind_of(File::Stat, File::Stat.new(@file).dup)
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
File::Stat.new(@file).instance_eval { initialize_copy(0) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_ftype
|
|
|
|
assert_equal("directory", File::Stat.new(@dir).ftype)
|
|
|
|
assert_equal("file", File::Stat.new(@file).ftype)
|
|
|
|
# File::Stat uses stat
|
|
|
|
assert_equal("file", File::Stat.new(@symlinkfile).ftype) if @symlinkfile
|
|
|
|
assert_equal("file", File::Stat.new(@hardlinkfile).ftype) if @hardlinkfile
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_directory_p
|
|
|
|
assert(File::Stat.new(@dir).directory?)
|
|
|
|
assert(!(File::Stat.new(@file).directory?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_pipe_p ## xxx
|
|
|
|
assert(!(File::Stat.new(@dir).pipe?))
|
|
|
|
assert(!(File::Stat.new(@file).pipe?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_symlink_p
|
|
|
|
assert(!(File::Stat.new(@dir).symlink?))
|
|
|
|
assert(!(File::Stat.new(@file).symlink?))
|
|
|
|
# File::Stat uses stat
|
|
|
|
assert(!(File::Stat.new(@symlinkfile).symlink?)) if @symlinkfile
|
|
|
|
assert(!(File::Stat.new(@hardlinkfile).symlink?)) if @hardlinkfile
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_socket_p ## xxx
|
|
|
|
assert(!(File::Stat.new(@dir).socket?))
|
|
|
|
assert(!(File::Stat.new(@file).socket?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_blockdev_p ## xxx
|
|
|
|
assert(!(File::Stat.new(@dir).blockdev?))
|
|
|
|
assert(!(File::Stat.new(@file).blockdev?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_chardev_p ## xxx
|
|
|
|
assert(!(File::Stat.new(@dir).chardev?))
|
|
|
|
assert(!(File::Stat.new(@file).chardev?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_readable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0200, @file)
|
|
|
|
assert(!(File::Stat.new(@file).readable?))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File::Stat.new(@file).readable?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_readable_real_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0200, @file)
|
|
|
|
assert(!(File::Stat.new(@file).readable_real?))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File::Stat.new(@file).readable_real?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_world_readable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0006, @file)
|
|
|
|
assert(File::Stat.new(@file).world_readable?)
|
|
|
|
File.chmod(0060, @file)
|
|
|
|
assert(!(File::Stat.new(@file).world_readable?))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File::Stat.new(@file).world_readable?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_writable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0400, @file)
|
|
|
|
assert(!(File::Stat.new(@file).writable?))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File::Stat.new(@file).writable?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_writable_real_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2009-02-18 13:17:26 -05:00
|
|
|
return if Process.euid == 0
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0400, @file)
|
|
|
|
assert(!(File::Stat.new(@file).writable_real?))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(File::Stat.new(@file).writable_real?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_world_writable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0006, @file)
|
|
|
|
assert(File::Stat.new(@file).world_writable?)
|
|
|
|
File.chmod(0060, @file)
|
|
|
|
assert(!(File::Stat.new(@file).world_writable?))
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File::Stat.new(@file).world_writable?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_executable_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0100, @file)
|
|
|
|
assert(File::Stat.new(@file).executable?)
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File::Stat.new(@file).executable?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_executable_real_p
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
File.chmod(0100, @file)
|
|
|
|
assert(File::Stat.new(@file).executable_real?)
|
|
|
|
File.chmod(0600, @file)
|
|
|
|
assert(!(File::Stat.new(@file).executable_real?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_file_p
|
|
|
|
assert(!(File::Stat.new(@dir).file?))
|
|
|
|
assert(File::Stat.new(@file).file?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_zero_p
|
|
|
|
assert_nothing_raised { File::Stat.new(@dir).zero? }
|
|
|
|
assert(!(File::Stat.new(@file).zero?))
|
|
|
|
assert(File::Stat.new(@zerofile).zero?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_size_p
|
|
|
|
assert_nothing_raised { File::Stat.new(@dir).size? }
|
|
|
|
assert_equal(3, File::Stat.new(@file).size?)
|
|
|
|
assert(!(File::Stat.new(@zerofile).size?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_owned_p ## xxx
|
* test/ruby/test_file_exhaustive.rb (assert_integer): may not Fixnum.
* test/ruby/test_file_exhaustive.rb (test_stat, test_readable_p)
(test_readable_real_p, test_world_readable_p, test_writable_p)
(test_writable_real_p, test_world_writable_p, test_executable_p)
(test_executable_real_p, test_owned_p, test_chmod, test_lchmod)
(test_umask, test_stat_readable_p, test_stat_readable_real_p)
(test_stat_world_readable_p, test_stat_writable_p)
(test_stat_writable_real_p, test_stat_world_writable_p)
(test_stat_executable_p, test_stat_executable_real_p)
(test_stat_owned_p): mode, owner and group can't rely on cygwin.
* test/ruby/test_file_exhaustive.rb (test_stat_init): test hard link
on Windows too.
* test/ruby/test_file_exhaustive.rb (test_test): struct stat dosn't
have sub second mtime on cygwin.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-05-14 05:11:10 -04:00
|
|
|
return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
|
2008-02-03 07:14:30 -05:00
|
|
|
assert(File::Stat.new(@file).owned?)
|
|
|
|
assert(File::Stat.new(@file).grpowned?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_suid_sgid_sticky ## xxx
|
|
|
|
assert(!(File::Stat.new(@file).setuid?))
|
|
|
|
assert(!(File::Stat.new(@file).setgid?))
|
|
|
|
assert(!(File::Stat.new(@file).sticky?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat_size
|
|
|
|
assert_integer(File::Stat.new(@dir).size)
|
|
|
|
assert_equal(3, File::Stat.new(@file).size)
|
|
|
|
assert_equal(0, File::Stat.new(@zerofile).size)
|
|
|
|
end
|
|
|
|
|
2012-08-09 07:34:48 -04:00
|
|
|
def test_stat_special_file
|
|
|
|
# test for special files such as pagefile.sys on Windows
|
|
|
|
assert_nothing_raised do
|
|
|
|
Dir::glob("C:/*.sys") {|f| File::Stat.new(f) }
|
|
|
|
end
|
|
|
|
end if DRIVE
|
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
def test_path_check
|
|
|
|
assert_nothing_raised { ENV["PATH"] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_file
|
|
|
|
assert_raise(SecurityError) do
|
|
|
|
Thread.new do
|
|
|
|
$SAFE = 4
|
|
|
|
load(@file)
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
2009-03-08 12:12:09 -04:00
|
|
|
|
|
|
|
def test_size
|
|
|
|
assert_equal(3, File.open(@file) {|f| f.size })
|
|
|
|
File.open(@file, "a") do |f|
|
|
|
|
f.write("bar")
|
|
|
|
assert_equal(6, f.size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_absolute_path
|
|
|
|
assert_equal(File.join(Dir.pwd, "~foo"), File.absolute_path("~foo"))
|
|
|
|
dir = File.expand_path("/bar")
|
|
|
|
assert_equal(File.join(dir, "~foo"), File.absolute_path("~foo", dir))
|
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|