2015-11-10 06:48:14 -05:00
|
|
|
# frozen_string_literal: true
|
2015-10-04 23:19:30 -04:00
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
require 'fileutils'
|
2005-12-14 22:35:04 -05:00
|
|
|
require 'tmpdir'
|
2005-11-26 15:43:08 -05:00
|
|
|
|
2010-08-06 20:29:52 -04:00
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
class TestPathname < Test::Unit::TestCase
|
2011-06-14 23:36:57 -04:00
|
|
|
def self.define_assertion(name, linenum, &block)
|
|
|
|
name = "test_#{name}_#{linenum}"
|
|
|
|
define_method(name, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_linenum
|
2017-06-16 10:44:01 -04:00
|
|
|
if loc = caller_locations(2, 1)
|
|
|
|
loc[0].lineno
|
2011-06-14 23:36:57 -04:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.defassert(name, result, *args)
|
2011-06-14 23:36:57 -04:00
|
|
|
define_assertion(name, get_linenum) {
|
2010-08-07 00:59:27 -04:00
|
|
|
mesg = "#{name}(#{args.map {|a| a.inspect }.join(', ')})"
|
|
|
|
assert_nothing_raised(mesg) {
|
|
|
|
assert_equal(result, self.send(name, *args), mesg)
|
|
|
|
}
|
2005-11-26 15:43:08 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-06-14 23:36:57 -04:00
|
|
|
def self.defassert_raise(name, exc, *args)
|
|
|
|
define_assertion(name, get_linenum) {
|
|
|
|
message = "#{name}(#{args.map {|a| a.inspect }.join(', ')})"
|
|
|
|
assert_raise(exc, message) { self.send(name, *args) }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
DOSISH = File::ALT_SEPARATOR != nil
|
|
|
|
DOSISH_DRIVE_LETTER = File.dirname("A:") == "A:."
|
|
|
|
DOSISH_UNC = File.dirname("//") == "//"
|
|
|
|
|
|
|
|
def cleanpath_aggressive(path)
|
|
|
|
Pathname.new(path).cleanpath.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:cleanpath_aggressive, '/', '/')
|
|
|
|
defassert(:cleanpath_aggressive, '.', '')
|
|
|
|
defassert(:cleanpath_aggressive, '.', '.')
|
|
|
|
defassert(:cleanpath_aggressive, '..', '..')
|
|
|
|
defassert(:cleanpath_aggressive, 'a', 'a')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '/.')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '/..')
|
|
|
|
defassert(:cleanpath_aggressive, '/a', '/a')
|
|
|
|
defassert(:cleanpath_aggressive, '.', './')
|
|
|
|
defassert(:cleanpath_aggressive, '..', '../')
|
|
|
|
defassert(:cleanpath_aggressive, 'a', 'a/')
|
|
|
|
defassert(:cleanpath_aggressive, 'a/b', 'a//b')
|
|
|
|
defassert(:cleanpath_aggressive, 'a', 'a/.')
|
|
|
|
defassert(:cleanpath_aggressive, 'a', 'a/./')
|
|
|
|
defassert(:cleanpath_aggressive, '.', 'a/..')
|
|
|
|
defassert(:cleanpath_aggressive, '.', 'a/../')
|
|
|
|
defassert(:cleanpath_aggressive, '/a', '/a/.')
|
|
|
|
defassert(:cleanpath_aggressive, '..', './..')
|
|
|
|
defassert(:cleanpath_aggressive, '..', '../.')
|
|
|
|
defassert(:cleanpath_aggressive, '..', './../')
|
|
|
|
defassert(:cleanpath_aggressive, '..', '.././')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '/./..')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '/../.')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '/./../')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '/.././')
|
|
|
|
defassert(:cleanpath_aggressive, 'a/b/c', 'a/b/c')
|
|
|
|
defassert(:cleanpath_aggressive, 'b/c', './b/c')
|
|
|
|
defassert(:cleanpath_aggressive, 'a/c', 'a/./c')
|
|
|
|
defassert(:cleanpath_aggressive, 'a/b', 'a/b/.')
|
|
|
|
defassert(:cleanpath_aggressive, '.', 'a/../.')
|
|
|
|
defassert(:cleanpath_aggressive, '/a', '/../.././../a')
|
|
|
|
defassert(:cleanpath_aggressive, '../../d', 'a/b/../../../../c/../d')
|
|
|
|
|
|
|
|
if DOSISH_UNC
|
|
|
|
defassert(:cleanpath_aggressive, '//a/b/c', '//a/b/c/')
|
|
|
|
else
|
|
|
|
defassert(:cleanpath_aggressive, '/', '///')
|
|
|
|
defassert(:cleanpath_aggressive, '/a', '///a')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '///..')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '///.')
|
|
|
|
defassert(:cleanpath_aggressive, '/', '///a/../..')
|
|
|
|
end
|
|
|
|
|
2014-05-05 09:09:58 -04:00
|
|
|
if DOSISH
|
|
|
|
defassert(:cleanpath_aggressive, 'c:/foo/bar', 'c:\\foo\\bar')
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
def cleanpath_conservative(path)
|
|
|
|
Pathname.new(path).cleanpath(true).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:cleanpath_conservative, '/', '/')
|
|
|
|
defassert(:cleanpath_conservative, '.', '')
|
|
|
|
defassert(:cleanpath_conservative, '.', '.')
|
|
|
|
defassert(:cleanpath_conservative, '..', '..')
|
|
|
|
defassert(:cleanpath_conservative, 'a', 'a')
|
|
|
|
defassert(:cleanpath_conservative, '/', '/.')
|
|
|
|
defassert(:cleanpath_conservative, '/', '/..')
|
|
|
|
defassert(:cleanpath_conservative, '/a', '/a')
|
|
|
|
defassert(:cleanpath_conservative, '.', './')
|
|
|
|
defassert(:cleanpath_conservative, '..', '../')
|
|
|
|
defassert(:cleanpath_conservative, 'a/', 'a/')
|
|
|
|
defassert(:cleanpath_conservative, 'a/b', 'a//b')
|
|
|
|
defassert(:cleanpath_conservative, 'a/.', 'a/.')
|
|
|
|
defassert(:cleanpath_conservative, 'a/.', 'a/./')
|
|
|
|
defassert(:cleanpath_conservative, 'a/..', 'a/../')
|
|
|
|
defassert(:cleanpath_conservative, '/a/.', '/a/.')
|
|
|
|
defassert(:cleanpath_conservative, '..', './..')
|
|
|
|
defassert(:cleanpath_conservative, '..', '../.')
|
|
|
|
defassert(:cleanpath_conservative, '..', './../')
|
|
|
|
defassert(:cleanpath_conservative, '..', '.././')
|
|
|
|
defassert(:cleanpath_conservative, '/', '/./..')
|
|
|
|
defassert(:cleanpath_conservative, '/', '/../.')
|
|
|
|
defassert(:cleanpath_conservative, '/', '/./../')
|
|
|
|
defassert(:cleanpath_conservative, '/', '/.././')
|
|
|
|
defassert(:cleanpath_conservative, 'a/b/c', 'a/b/c')
|
|
|
|
defassert(:cleanpath_conservative, 'b/c', './b/c')
|
|
|
|
defassert(:cleanpath_conservative, 'a/c', 'a/./c')
|
|
|
|
defassert(:cleanpath_conservative, 'a/b/.', 'a/b/.')
|
|
|
|
defassert(:cleanpath_conservative, 'a/..', 'a/../.')
|
|
|
|
defassert(:cleanpath_conservative, '/a', '/../.././../a')
|
|
|
|
defassert(:cleanpath_conservative, 'a/b/../../../../c/../d', 'a/b/../../../../c/../d')
|
|
|
|
|
2014-05-05 09:09:58 -04:00
|
|
|
if DOSISH
|
|
|
|
defassert(:cleanpath_conservative, 'c:/foo/bar', 'c:\\foo\\bar')
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
if DOSISH_UNC
|
|
|
|
defassert(:cleanpath_conservative, '//', '//')
|
|
|
|
else
|
|
|
|
defassert(:cleanpath_conservative, '/', '//')
|
|
|
|
end
|
|
|
|
|
|
|
|
# has_trailing_separator?(path) -> bool
|
|
|
|
def has_trailing_separator?(path)
|
2007-11-04 15:36:20 -05:00
|
|
|
Pathname.allocate.__send__(:has_trailing_separator?, path)
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:has_trailing_separator?, false, "/")
|
|
|
|
defassert(:has_trailing_separator?, false, "///")
|
|
|
|
defassert(:has_trailing_separator?, false, "a")
|
|
|
|
defassert(:has_trailing_separator?, true, "a/")
|
|
|
|
|
|
|
|
def add_trailing_separator(path)
|
2007-11-04 15:36:20 -05:00
|
|
|
Pathname.allocate.__send__(:add_trailing_separator, path)
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def del_trailing_separator(path)
|
2007-11-13 09:59:02 -05:00
|
|
|
Pathname.allocate.__send__(:del_trailing_separator, path)
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:del_trailing_separator, "/", "/")
|
|
|
|
defassert(:del_trailing_separator, "/a", "/a")
|
|
|
|
defassert(:del_trailing_separator, "/a", "/a/")
|
|
|
|
defassert(:del_trailing_separator, "/a", "/a//")
|
|
|
|
defassert(:del_trailing_separator, ".", ".")
|
|
|
|
defassert(:del_trailing_separator, ".", "./")
|
|
|
|
defassert(:del_trailing_separator, ".", ".//")
|
|
|
|
|
|
|
|
if DOSISH_DRIVE_LETTER
|
|
|
|
defassert(:del_trailing_separator, "A:", "A:")
|
|
|
|
defassert(:del_trailing_separator, "A:/", "A:/")
|
|
|
|
defassert(:del_trailing_separator, "A:/", "A://")
|
|
|
|
defassert(:del_trailing_separator, "A:.", "A:.")
|
|
|
|
defassert(:del_trailing_separator, "A:.", "A:./")
|
|
|
|
defassert(:del_trailing_separator, "A:.", "A:.//")
|
|
|
|
end
|
|
|
|
|
|
|
|
if DOSISH_UNC
|
|
|
|
defassert(:del_trailing_separator, "//", "//")
|
|
|
|
defassert(:del_trailing_separator, "//a", "//a")
|
|
|
|
defassert(:del_trailing_separator, "//a", "//a/")
|
|
|
|
defassert(:del_trailing_separator, "//a", "//a//")
|
|
|
|
defassert(:del_trailing_separator, "//a/b", "//a/b")
|
|
|
|
defassert(:del_trailing_separator, "//a/b", "//a/b/")
|
|
|
|
defassert(:del_trailing_separator, "//a/b", "//a/b//")
|
|
|
|
defassert(:del_trailing_separator, "//a/b/c", "//a/b/c")
|
|
|
|
defassert(:del_trailing_separator, "//a/b/c", "//a/b/c/")
|
|
|
|
defassert(:del_trailing_separator, "//a/b/c", "//a/b/c//")
|
|
|
|
else
|
|
|
|
defassert(:del_trailing_separator, "/", "///")
|
|
|
|
defassert(:del_trailing_separator, "///a", "///a/")
|
|
|
|
end
|
|
|
|
|
|
|
|
if DOSISH
|
|
|
|
defassert(:del_trailing_separator, "a", "a\\")
|
2015-10-04 23:19:30 -04:00
|
|
|
defassert(:del_trailing_separator, "\225\\".dup.force_encoding("cp932"), "\225\\\\".dup.force_encoding("cp932"))
|
|
|
|
defassert(:del_trailing_separator, "\225".dup.force_encoding("cp437"), "\225\\\\".dup.force_encoding("cp437"))
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_plus
|
|
|
|
assert_kind_of(Pathname, Pathname("a") + Pathname("b"))
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
def plus(path1, path2) # -> path
|
|
|
|
(Pathname.new(path1) + Pathname.new(path2)).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:plus, '/', '/', '/')
|
|
|
|
defassert(:plus, 'a/b', 'a', 'b')
|
|
|
|
defassert(:plus, 'a', 'a', '.')
|
|
|
|
defassert(:plus, 'b', '.', 'b')
|
|
|
|
defassert(:plus, '.', '.', '.')
|
|
|
|
defassert(:plus, '/b', 'a', '/b')
|
|
|
|
|
|
|
|
defassert(:plus, '/', '/', '..')
|
|
|
|
defassert(:plus, '.', 'a', '..')
|
|
|
|
defassert(:plus, 'a', 'a/b', '..')
|
|
|
|
defassert(:plus, '../..', '..', '..')
|
|
|
|
defassert(:plus, '/c', '/', '../c')
|
|
|
|
defassert(:plus, 'c', 'a', '../c')
|
|
|
|
defassert(:plus, 'a/c', 'a/b', '../c')
|
|
|
|
defassert(:plus, '../../c', '..', '../c')
|
|
|
|
|
|
|
|
defassert(:plus, 'a//b/d//e', 'a//b/c', '../d//e')
|
|
|
|
|
2017-06-16 10:44:01 -04:00
|
|
|
defassert(:plus, '//foo/var/bar', '//foo/var', 'bar')
|
|
|
|
|
2014-05-05 08:49:27 -04:00
|
|
|
def test_slash
|
|
|
|
assert_kind_of(Pathname, Pathname("a") / Pathname("b"))
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_parent
|
|
|
|
assert_equal(Pathname("."), Pathname("a").parent)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent(path) # -> path
|
|
|
|
Pathname.new(path).parent.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:parent, '/', '/')
|
|
|
|
defassert(:parent, '/', '/a')
|
|
|
|
defassert(:parent, '/a', '/a/b')
|
|
|
|
defassert(:parent, '/a/b', '/a/b/c')
|
|
|
|
defassert(:parent, '.', 'a')
|
|
|
|
defassert(:parent, 'a', 'a/b')
|
|
|
|
defassert(:parent, 'a/b', 'a/b/c')
|
|
|
|
defassert(:parent, '..', '.')
|
|
|
|
defassert(:parent, '../..', '..')
|
|
|
|
|
|
|
|
def test_join
|
|
|
|
r = Pathname("a").join(Pathname("b"), Pathname("c"))
|
|
|
|
assert_equal(Pathname("a/b/c"), r)
|
2014-03-25 10:42:36 -04:00
|
|
|
r = Pathname("/a").join(Pathname("b"), Pathname("c"))
|
|
|
|
assert_equal(Pathname("/a/b/c"), r)
|
|
|
|
r = Pathname("/a").join(Pathname("/b"), Pathname("c"))
|
|
|
|
assert_equal(Pathname("/b/c"), r)
|
|
|
|
r = Pathname("/a").join(Pathname("/b"), Pathname("/c"))
|
|
|
|
assert_equal(Pathname("/c"), r)
|
|
|
|
r = Pathname("/a").join("/b", "/c")
|
|
|
|
assert_equal(Pathname("/c"), r)
|
2014-04-01 22:51:20 -04:00
|
|
|
r = Pathname("/foo/var").join()
|
|
|
|
assert_equal(Pathname("/foo/var"), r)
|
2010-08-03 10:35:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_absolute
|
|
|
|
assert_equal(true, Pathname("/").absolute?)
|
|
|
|
assert_equal(false, Pathname("a").absolute?)
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
def relative?(path)
|
|
|
|
Pathname.new(path).relative?
|
|
|
|
end
|
|
|
|
|
2019-04-03 15:22:18 -04:00
|
|
|
defassert(:relative?, true, '')
|
2005-11-26 15:43:08 -05:00
|
|
|
defassert(:relative?, false, '/')
|
|
|
|
defassert(:relative?, false, '/a')
|
|
|
|
defassert(:relative?, false, '/..')
|
|
|
|
defassert(:relative?, true, 'a')
|
|
|
|
defassert(:relative?, true, 'a/b')
|
|
|
|
|
2019-04-03 15:22:18 -04:00
|
|
|
defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:.')
|
|
|
|
defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:')
|
|
|
|
defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:/')
|
|
|
|
defassert(:relative?, !DOSISH_DRIVE_LETTER, 'A:/a')
|
2005-11-26 15:43:08 -05:00
|
|
|
|
|
|
|
if File.dirname('//') == '//'
|
|
|
|
defassert(:relative?, false, '//')
|
|
|
|
defassert(:relative?, false, '//a')
|
|
|
|
defassert(:relative?, false, '//a/')
|
|
|
|
defassert(:relative?, false, '//a/b')
|
|
|
|
defassert(:relative?, false, '//a/b/')
|
|
|
|
defassert(:relative?, false, '//a/b/c')
|
|
|
|
end
|
|
|
|
|
|
|
|
def relative_path_from(dest_directory, base_directory)
|
2018-11-13 01:20:06 -05:00
|
|
|
Pathname.new(dest_directory).relative_path_from(base_directory).to_s
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
2018-11-13 01:20:06 -05:00
|
|
|
defassert(:relative_path_from, "../a", Pathname.new("a"), "b")
|
2005-11-26 15:43:08 -05:00
|
|
|
defassert(:relative_path_from, "../a", "a", "b")
|
|
|
|
defassert(:relative_path_from, "../a", "a", "b/")
|
|
|
|
defassert(:relative_path_from, "../a", "a/", "b")
|
|
|
|
defassert(:relative_path_from, "../a", "a/", "b/")
|
|
|
|
defassert(:relative_path_from, "../a", "/a", "/b")
|
|
|
|
defassert(:relative_path_from, "../a", "/a", "/b/")
|
|
|
|
defassert(:relative_path_from, "../a", "/a/", "/b")
|
|
|
|
defassert(:relative_path_from, "../a", "/a/", "/b/")
|
|
|
|
|
|
|
|
defassert(:relative_path_from, "../b", "a/b", "a/c")
|
|
|
|
defassert(:relative_path_from, "../a", "../a", "../b")
|
|
|
|
|
|
|
|
defassert(:relative_path_from, "a", "a", ".")
|
|
|
|
defassert(:relative_path_from, "..", ".", "a")
|
|
|
|
|
|
|
|
defassert(:relative_path_from, ".", ".", ".")
|
|
|
|
defassert(:relative_path_from, ".", "..", "..")
|
|
|
|
defassert(:relative_path_from, "..", "..", ".")
|
|
|
|
|
|
|
|
defassert(:relative_path_from, "c/d", "/a/b/c/d", "/a/b")
|
|
|
|
defassert(:relative_path_from, "../..", "/a/b", "/a/b/c/d")
|
|
|
|
defassert(:relative_path_from, "../../../../e", "/e", "/a/b/c/d")
|
|
|
|
defassert(:relative_path_from, "../b/c", "a/b/c", "a/d")
|
|
|
|
|
|
|
|
defassert(:relative_path_from, "../a", "/../a", "/b")
|
|
|
|
defassert(:relative_path_from, "../../a", "../a", "b")
|
|
|
|
defassert(:relative_path_from, ".", "/a/../../b", "/b")
|
|
|
|
defassert(:relative_path_from, "..", "a/..", "a")
|
|
|
|
defassert(:relative_path_from, ".", "a/../b", "b")
|
|
|
|
|
|
|
|
defassert(:relative_path_from, "a", "a", "b/..")
|
|
|
|
defassert(:relative_path_from, "b/c", "b/c", "b/..")
|
|
|
|
|
2008-10-01 22:40:33 -04:00
|
|
|
defassert_raise(:relative_path_from, ArgumentError, "/", ".")
|
|
|
|
defassert_raise(:relative_path_from, ArgumentError, ".", "/")
|
|
|
|
defassert_raise(:relative_path_from, ArgumentError, "a", "..")
|
|
|
|
defassert_raise(:relative_path_from, ArgumentError, ".", "..")
|
2005-11-26 15:43:08 -05:00
|
|
|
|
2009-01-29 13:02:25 -05:00
|
|
|
def with_tmpchdir(base=nil)
|
|
|
|
Dir.mktmpdir(base) {|d|
|
|
|
|
d = Pathname.new(d).realpath.to_s
|
|
|
|
Dir.chdir(d) {
|
|
|
|
yield d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-30 07:50:55 -05:00
|
|
|
def has_symlink?
|
2005-11-26 15:43:08 -05:00
|
|
|
begin
|
2015-08-28 21:25:07 -04:00
|
|
|
File.symlink("", "")
|
2020-10-12 08:26:05 -04:00
|
|
|
rescue NotImplementedError
|
2009-01-30 07:50:55 -05:00
|
|
|
return false
|
2015-08-29 01:19:49 -04:00
|
|
|
rescue Errno::ENOENT
|
2020-10-12 08:26:05 -04:00
|
|
|
return false
|
|
|
|
rescue Errno::EACCES
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_hardlink?
|
|
|
|
begin
|
|
|
|
with_tmpchdir("rubytest-pathname") {|dir|
|
|
|
|
File.write("dummy", "dummy")
|
|
|
|
File.link("dummy", "hardlink")
|
|
|
|
}
|
|
|
|
rescue NotImplementedError
|
|
|
|
return false
|
|
|
|
rescue Errno::EACCES
|
|
|
|
return false
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
2009-01-30 07:50:55 -05:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2010-03-06 23:55:34 -05:00
|
|
|
def realpath(path, basedir=nil)
|
|
|
|
Pathname.new(path).realpath(basedir).to_s
|
2009-01-30 07:50:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_realpath
|
|
|
|
return if !has_symlink?
|
2009-01-29 13:02:25 -05:00
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
2009-01-30 07:50:55 -05:00
|
|
|
assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") }
|
2005-11-26 15:43:08 -05:00
|
|
|
File.symlink("not-exist-target", "#{dir}/not-exist")
|
2008-10-01 22:40:33 -04:00
|
|
|
assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") }
|
2009-01-29 13:02:25 -05:00
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
File.symlink("loop", "#{dir}/loop")
|
2008-10-01 22:40:33 -04:00
|
|
|
assert_raise(Errno::ELOOP) { realpath("#{dir}/loop") }
|
2010-03-06 23:55:34 -05:00
|
|
|
assert_raise(Errno::ELOOP) { realpath("#{dir}/loop", dir) }
|
2009-01-29 13:02:25 -05:00
|
|
|
|
2009-01-30 07:50:55 -05:00
|
|
|
File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2")
|
|
|
|
assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist2") }
|
|
|
|
|
|
|
|
File.open("#{dir}/exist-target", "w") {}
|
|
|
|
File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist2")
|
|
|
|
assert_nothing_raised { realpath("#{dir}/exist2") }
|
|
|
|
|
2009-01-29 13:02:25 -05:00
|
|
|
File.symlink("loop-relative", "loop-relative")
|
|
|
|
assert_raise(Errno::ELOOP) { realpath("#{dir}/loop-relative") }
|
|
|
|
|
|
|
|
Dir.mkdir("exist")
|
|
|
|
assert_equal("#{dir}/exist", realpath("exist"))
|
2010-03-06 23:55:34 -05:00
|
|
|
assert_raise(Errno::ELOOP) { realpath("../loop", "#{dir}/exist") }
|
2009-01-29 13:02:25 -05:00
|
|
|
|
|
|
|
File.symlink("loop1/loop1", "loop1")
|
|
|
|
assert_raise(Errno::ELOOP) { realpath("#{dir}/loop1") }
|
|
|
|
|
|
|
|
File.symlink("loop2", "loop3")
|
|
|
|
File.symlink("loop3", "loop2")
|
|
|
|
assert_raise(Errno::ELOOP) { realpath("#{dir}/loop2") }
|
|
|
|
|
|
|
|
Dir.mkdir("b")
|
|
|
|
|
|
|
|
File.symlink("b", "c")
|
|
|
|
assert_equal("#{dir}/b", realpath("c"))
|
|
|
|
assert_equal("#{dir}/b", realpath("c/../c"))
|
|
|
|
assert_equal("#{dir}/b", realpath("c/../c/../c/."))
|
|
|
|
|
|
|
|
File.symlink("..", "b/d")
|
|
|
|
assert_equal("#{dir}/b", realpath("c/d/c/d/c"))
|
|
|
|
|
|
|
|
File.symlink("#{dir}/b", "e")
|
|
|
|
assert_equal("#{dir}/b", realpath("e"))
|
|
|
|
|
|
|
|
Dir.mkdir("f")
|
|
|
|
Dir.mkdir("f/g")
|
|
|
|
File.symlink("f/g", "h")
|
|
|
|
assert_equal("#{dir}/f/g", realpath("h"))
|
|
|
|
File.chmod(0000, "f")
|
2015-08-26 10:49:07 -04:00
|
|
|
next if File.readable?("f")
|
2009-01-29 13:02:25 -05:00
|
|
|
assert_raise(Errno::EACCES) { realpath("h") }
|
|
|
|
File.chmod(0755, "f")
|
2007-10-24 01:55:26 -04:00
|
|
|
}
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
2009-01-30 07:50:55 -05:00
|
|
|
def realdirpath(path)
|
|
|
|
Pathname.new(path).realdirpath.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_realdirpath
|
|
|
|
return if !has_symlink?
|
|
|
|
Dir.mktmpdir('rubytest-pathname') {|dir|
|
|
|
|
rdir = realpath(dir)
|
|
|
|
assert_equal("#{rdir}/not-exist", realdirpath("#{dir}/not-exist"))
|
|
|
|
assert_raise(Errno::ENOENT) { realdirpath("#{dir}/not-exist/not-exist-child") }
|
|
|
|
File.symlink("not-exist-target", "#{dir}/not-exist")
|
|
|
|
assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist"))
|
|
|
|
File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2")
|
|
|
|
assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist2"))
|
|
|
|
File.open("#{dir}/exist-target", "w") {}
|
|
|
|
File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist")
|
|
|
|
assert_equal("#{rdir}/exist-target", realdirpath("#{dir}/exist"))
|
|
|
|
File.symlink("loop", "#{dir}/loop")
|
|
|
|
assert_raise(Errno::ELOOP) { realdirpath("#{dir}/loop") }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
def descend(path)
|
2015-06-14 11:14:46 -04:00
|
|
|
Pathname.new(path).descend.map(&:to_s)
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:descend, %w[/ /a /a/b /a/b/c], "/a/b/c")
|
|
|
|
defassert(:descend, %w[a a/b a/b/c], "a/b/c")
|
|
|
|
defassert(:descend, %w[. ./a ./a/b ./a/b/c], "./a/b/c")
|
|
|
|
defassert(:descend, %w[a/], "a/")
|
|
|
|
|
|
|
|
def ascend(path)
|
2015-06-14 11:14:46 -04:00
|
|
|
Pathname.new(path).ascend.map(&:to_s)
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:ascend, %w[/a/b/c /a/b /a /], "/a/b/c")
|
|
|
|
defassert(:ascend, %w[a/b/c a/b a], "a/b/c")
|
|
|
|
defassert(:ascend, %w[./a/b/c ./a/b ./a .], "./a/b/c")
|
|
|
|
defassert(:ascend, %w[a/], "a/")
|
|
|
|
|
2015-06-14 11:14:46 -04:00
|
|
|
def test_blockless_ascend_is_enumerator
|
|
|
|
assert_kind_of(Enumerator, Pathname.new('a').ascend)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_blockless_descend_is_enumerator
|
|
|
|
assert_kind_of(Enumerator, Pathname.new('a').descend)
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
def test_initialize
|
|
|
|
p1 = Pathname.new('a')
|
|
|
|
assert_equal('a', p1.to_s)
|
|
|
|
p2 = Pathname.new(p1)
|
|
|
|
assert_equal(p1, p2)
|
|
|
|
end
|
|
|
|
|
2006-06-17 01:59:29 -04:00
|
|
|
def test_initialize_nul
|
2008-10-01 22:40:33 -04:00
|
|
|
assert_raise(ArgumentError) { Pathname.new("a\0") }
|
2006-06-17 01:59:29 -04:00
|
|
|
end
|
|
|
|
|
Make Kernel#{Pathname,BigDecimal,Complex} return argument if given correct type
This is how Kernel#{Array,String,Float,Integer,Hash,Rational} work.
BigDecimal and Complex instances are always frozen, so this should
not cause backwards compatibility issues for those. Pathname
instances are not frozen, so potentially this could cause backwards
compatibility issues by not returning a new object.
Based on a patch from Joshua Ballanco, some minor changes by me.
Fixes [Bug #7522]
2019-08-07 12:01:33 -04:00
|
|
|
def test_global_constructor
|
|
|
|
p = Pathname.new('a')
|
|
|
|
assert_equal(p, Pathname('a'))
|
|
|
|
assert_same(p, Pathname(p))
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
class AnotherStringLike # :nodoc:
|
|
|
|
def initialize(s) @s = s end
|
|
|
|
def to_str() @s end
|
|
|
|
def ==(other) @s == other end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_equality
|
|
|
|
obj = Pathname.new("a")
|
|
|
|
str = "a"
|
|
|
|
sym = :a
|
|
|
|
ano = AnotherStringLike.new("a")
|
|
|
|
assert_equal(false, obj == str)
|
|
|
|
assert_equal(false, str == obj)
|
|
|
|
assert_equal(false, obj == ano)
|
|
|
|
assert_equal(false, ano == obj)
|
|
|
|
assert_equal(false, obj == sym)
|
|
|
|
assert_equal(false, sym == obj)
|
|
|
|
|
|
|
|
obj2 = Pathname.new("a")
|
|
|
|
assert_equal(true, obj == obj2)
|
|
|
|
assert_equal(true, obj === obj2)
|
|
|
|
assert_equal(true, obj.eql?(obj2))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hashkey
|
|
|
|
h = {}
|
|
|
|
h[Pathname.new("a")] = 1
|
|
|
|
h[Pathname.new("a")] = 2
|
|
|
|
assert_equal(1, h.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_pathname_cmp(e, s1, s2)
|
|
|
|
p1 = Pathname.new(s1)
|
|
|
|
p2 = Pathname.new(s2)
|
|
|
|
r = p1 <=> p2
|
|
|
|
assert(e == r,
|
|
|
|
"#{p1.inspect} <=> #{p2.inspect}: <#{e}> expected but was <#{r}>")
|
|
|
|
end
|
|
|
|
def test_comparison
|
|
|
|
assert_pathname_cmp( 0, "a", "a")
|
|
|
|
assert_pathname_cmp( 1, "b", "a")
|
|
|
|
assert_pathname_cmp(-1, "a", "b")
|
|
|
|
ss = %w(
|
|
|
|
a
|
|
|
|
a/
|
|
|
|
a/b
|
|
|
|
a.
|
|
|
|
a0
|
|
|
|
)
|
|
|
|
s1 = ss.shift
|
|
|
|
ss.each {|s2|
|
|
|
|
assert_pathname_cmp(-1, s1, s2)
|
|
|
|
s1 = s2
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_comparison_string
|
|
|
|
assert_equal(nil, Pathname.new("a") <=> "a")
|
|
|
|
assert_equal(nil, "a" <=> Pathname.new("a"))
|
|
|
|
end
|
|
|
|
|
2006-06-17 01:59:29 -04:00
|
|
|
def pathsub(path, pat, repl) Pathname.new(path).sub(pat, repl).to_s end
|
|
|
|
defassert(:pathsub, "a.o", "a.c", /\.c\z/, ".o")
|
|
|
|
|
2008-02-13 08:50:31 -05:00
|
|
|
def pathsubext(path, repl) Pathname.new(path).sub_ext(repl).to_s end
|
|
|
|
defassert(:pathsubext, 'a.o', 'a.c', '.o')
|
|
|
|
defassert(:pathsubext, 'a.o', 'a.c++', '.o')
|
|
|
|
defassert(:pathsubext, 'a.png', 'a.gif', '.png')
|
|
|
|
defassert(:pathsubext, 'ruby.tar.bz2', 'ruby.tar.gz', '.bz2')
|
|
|
|
defassert(:pathsubext, 'd/a.o', 'd/a.c', '.o')
|
|
|
|
defassert(:pathsubext, 'foo', 'foo.exe', '')
|
|
|
|
defassert(:pathsubext, 'lex.yy.o', 'lex.yy.c', '.o')
|
|
|
|
defassert(:pathsubext, 'fooaa.o', 'fooaa', '.o')
|
|
|
|
defassert(:pathsubext, 'd.e/aa.o', 'd.e/aa', '.o')
|
2017-10-22 07:27:06 -04:00
|
|
|
defassert(:pathsubext, 'long_enough.bug-3664', 'long_enough.not_to_be_embedded[ruby-core:31640]', '.bug-3664')
|
2008-02-13 08:50:31 -05:00
|
|
|
|
2009-03-17 01:26:15 -04:00
|
|
|
def test_sub_matchdata
|
|
|
|
result = Pathname("abc.gif").sub(/\..*/) {
|
|
|
|
assert_not_nil($~)
|
|
|
|
assert_equal(".gif", $~[0])
|
|
|
|
".png"
|
|
|
|
}
|
|
|
|
assert_equal("abc.png", result.to_s)
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
def root?(path)
|
|
|
|
Pathname.new(path).root?
|
|
|
|
end
|
|
|
|
|
|
|
|
defassert(:root?, true, "/")
|
|
|
|
defassert(:root?, true, "//")
|
|
|
|
defassert(:root?, true, "///")
|
|
|
|
defassert(:root?, false, "")
|
|
|
|
defassert(:root?, false, "a")
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_mountpoint?
|
|
|
|
r = Pathname("/").mountpoint?
|
2011-02-12 09:17:54 -05:00
|
|
|
assert_include([true, false], r)
|
2010-08-03 10:35:34 -04:00
|
|
|
end
|
|
|
|
|
2014-09-04 07:22:20 -04:00
|
|
|
def test_mountpoint_enoent
|
|
|
|
r = Pathname("/nonexistent").mountpoint?
|
|
|
|
assert_equal false, r
|
|
|
|
end
|
|
|
|
|
2005-11-26 15:43:08 -05:00
|
|
|
def test_destructive_update
|
|
|
|
path = Pathname.new("a")
|
|
|
|
path.to_s.replace "b"
|
|
|
|
assert_equal(Pathname.new("a"), path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_null_character
|
2008-10-01 22:40:33 -04:00
|
|
|
assert_raise(ArgumentError) { Pathname.new("\0") }
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_freeze
|
|
|
|
obj = Pathname.new("a"); assert_same(obj, obj.freeze)
|
|
|
|
|
|
|
|
assert_equal(false, Pathname.new("a" ) .frozen?)
|
|
|
|
assert_equal(false, Pathname.new("a".freeze) .frozen?)
|
|
|
|
assert_equal(true, Pathname.new("a" ).freeze .frozen?)
|
|
|
|
assert_equal(true, Pathname.new("a".freeze).freeze .frozen?)
|
|
|
|
assert_equal(false, Pathname.new("a" ) .to_s.frozen?)
|
|
|
|
assert_equal(false, Pathname.new("a".freeze) .to_s.frozen?)
|
|
|
|
assert_equal(false, Pathname.new("a" ).freeze.to_s.frozen?)
|
|
|
|
assert_equal(false, Pathname.new("a".freeze).freeze.to_s.frozen?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
str = "a"
|
|
|
|
obj = Pathname.new(str)
|
|
|
|
assert_equal(str, obj.to_s)
|
2008-10-01 22:40:33 -04:00
|
|
|
assert_not_same(str, obj.to_s)
|
|
|
|
assert_not_same(obj.to_s, obj.to_s)
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_kernel_open
|
|
|
|
count = 0
|
|
|
|
result = Kernel.open(Pathname.new(__FILE__)) {|f|
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.identical?(__FILE__, f)
|
2005-11-26 15:43:08 -05:00
|
|
|
count += 1
|
|
|
|
2
|
|
|
|
}
|
|
|
|
assert_equal(1, count)
|
|
|
|
assert_equal(2, result)
|
|
|
|
end
|
2006-02-22 07:18:58 -05:00
|
|
|
|
|
|
|
def test_each_filename
|
|
|
|
result = []
|
|
|
|
Pathname.new("/usr/bin/ruby").each_filename {|f| result << f }
|
|
|
|
assert_equal(%w[usr bin ruby], result)
|
2008-09-22 20:03:42 -04:00
|
|
|
assert_equal(%w[usr bin ruby], Pathname.new("/usr/bin/ruby").each_filename.to_a)
|
2006-02-22 07:18:58 -05:00
|
|
|
end
|
2006-06-17 01:59:29 -04:00
|
|
|
|
|
|
|
def test_kernel_pathname
|
|
|
|
assert_equal(Pathname.new("a"), Pathname("a"))
|
|
|
|
end
|
2007-12-09 00:12:31 -05:00
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_children
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
open("b", "w") {}
|
|
|
|
Dir.mkdir("d")
|
|
|
|
open("d/x", "w") {}
|
|
|
|
open("d/y", "w") {}
|
|
|
|
assert_equal([Pathname("a"), Pathname("b"), Pathname("d")], Pathname(".").children.sort)
|
|
|
|
assert_equal([Pathname("d/x"), Pathname("d/y")], Pathname("d").children.sort)
|
|
|
|
assert_equal([Pathname("x"), Pathname("y")], Pathname("d").children(false).sort)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_child
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
open("b", "w") {}
|
|
|
|
Dir.mkdir("d")
|
|
|
|
open("d/x", "w") {}
|
|
|
|
open("d/y", "w") {}
|
|
|
|
a = []; Pathname(".").each_child {|v| a << v }; a.sort!
|
|
|
|
assert_equal([Pathname("a"), Pathname("b"), Pathname("d")], a)
|
|
|
|
a = []; Pathname("d").each_child {|v| a << v }; a.sort!
|
|
|
|
assert_equal([Pathname("d/x"), Pathname("d/y")], a)
|
|
|
|
a = []; Pathname("d").each_child(false) {|v| a << v }; a.sort!
|
|
|
|
assert_equal([Pathname("x"), Pathname("y")], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_line
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.puts 1, 2 }
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line {|line| a << line }
|
|
|
|
assert_equal(["1\n", "2\n"], a)
|
2010-08-23 10:16:06 -04:00
|
|
|
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line("2") {|line| a << line }
|
|
|
|
assert_equal(["1\n2", "\n"], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line(1) {|line| a << line }
|
|
|
|
assert_equal(["1", "\n", "2", "\n"], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line("2", 1) {|line| a << line }
|
|
|
|
assert_equal(["1", "\n", "2", "\n"], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
enum = Pathname("a").each_line
|
|
|
|
enum.each {|line| a << line }
|
|
|
|
assert_equal(["1\n", "2\n"], a)
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-28 23:09:38 -05:00
|
|
|
def test_each_line_opts
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.puts 1, 2 }
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line(chomp: true) {|line| a << line }
|
|
|
|
assert_equal(["1", "2"], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line("2", chomp: true) {|line| a << line }
|
|
|
|
assert_equal(["1\n", "\n"], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line(1, chomp: true) {|line| a << line }
|
|
|
|
assert_equal(["1", "", "2", ""], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
Pathname("a").each_line("2", 1, chomp: true) {|line| a << line }
|
|
|
|
assert_equal(["1", "\n", "", "\n"], a)
|
|
|
|
|
|
|
|
a = []
|
|
|
|
enum = Pathname("a").each_line(chomp: true)
|
|
|
|
enum.each {|line| a << line }
|
|
|
|
assert_equal(["1", "2"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_readlines
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.puts 1, 2 }
|
|
|
|
a = Pathname("a").readlines
|
|
|
|
assert_equal(["1\n", "2\n"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-25 18:32:35 -04:00
|
|
|
def test_readlines_opts
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.puts 1, 2 }
|
|
|
|
a = Pathname("a").readlines 1, chomp: true
|
|
|
|
assert_equal(["1", "", "2", ""], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_read
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.puts 1, 2 }
|
|
|
|
assert_equal("1\n2\n", Pathname("a").read)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_binread
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
2012-02-09 09:44:10 -05:00
|
|
|
str = Pathname("a").binread
|
|
|
|
assert_equal("abc", str)
|
|
|
|
assert_equal(Encoding::ASCII_8BIT, str.encoding)
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-04-04 07:12:12 -04:00
|
|
|
def test_write
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
path = Pathname("a")
|
|
|
|
path.write "abc"
|
|
|
|
assert_equal("abc", path.read)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-25 18:32:35 -04:00
|
|
|
def test_write_opts
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
path = Pathname("a")
|
|
|
|
path.write "abc", mode: "w"
|
|
|
|
assert_equal("abc", path.read)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-04-04 07:12:12 -04:00
|
|
|
def test_binwrite
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
path = Pathname("a")
|
|
|
|
path.binwrite "abc\x80"
|
|
|
|
assert_equal("abc\x80".b, path.binread)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-25 18:32:35 -04:00
|
|
|
def test_binwrite_opts
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
path = Pathname("a")
|
|
|
|
path.binwrite "abc\x80", mode: 'w'
|
|
|
|
assert_equal("abc\x80".b, path.binread)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_sysopen
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
fd = Pathname("a").sysopen
|
|
|
|
io = IO.new(fd)
|
|
|
|
begin
|
|
|
|
assert_equal("abc", io.read)
|
|
|
|
ensure
|
|
|
|
io.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_atime
|
|
|
|
assert_kind_of(Time, Pathname(__FILE__).atime)
|
|
|
|
end
|
|
|
|
|
2014-06-28 09:46:02 -04:00
|
|
|
def test_birthtime
|
2021-09-06 23:21:49 -04:00
|
|
|
omit if RUBY_PLATFORM =~ /android/
|
2019-02-20 01:54:23 -05:00
|
|
|
# Check under a (probably) local filesystem.
|
|
|
|
# Remote filesystems often may not support birthtime.
|
|
|
|
with_tmpchdir('rubytest-pathname') do |dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
assert_kind_of(Time, Pathname("a").birthtime)
|
2019-02-20 03:31:17 -05:00
|
|
|
rescue Errno::EPERM
|
|
|
|
# Docker prohibits statx syscall by the default.
|
2021-09-06 23:21:49 -04:00
|
|
|
omit("statx(2) is prohibited by seccomp")
|
2019-02-22 01:43:11 -05:00
|
|
|
rescue Errno::ENOSYS
|
2021-09-06 23:21:49 -04:00
|
|
|
omit("statx(2) is not supported on this filesystem")
|
2019-02-20 01:54:23 -05:00
|
|
|
rescue NotImplementedError
|
2019-02-20 04:55:25 -05:00
|
|
|
# assert_raise(NotImplementedError) do
|
|
|
|
# File.birthtime("a")
|
|
|
|
# end
|
2014-06-28 09:46:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_ctime
|
|
|
|
assert_kind_of(Time, Pathname(__FILE__).ctime)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mtime
|
|
|
|
assert_kind_of(Time, Pathname(__FILE__).mtime)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chmod
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
path = Pathname("a")
|
|
|
|
old = path.stat.mode
|
|
|
|
path.chmod(0444)
|
|
|
|
assert_equal(0444, path.stat.mode & 0777)
|
|
|
|
path.chmod(old)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lchmod
|
|
|
|
return if !has_symlink?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
File.symlink("a", "l")
|
|
|
|
path = Pathname("l")
|
|
|
|
old = path.lstat.mode
|
|
|
|
begin
|
|
|
|
path.lchmod(0444)
|
2020-01-23 01:33:42 -05:00
|
|
|
rescue NotImplementedError, Errno::EOPNOTSUPP
|
2010-08-03 10:35:34 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal(0444, path.lstat.mode & 0777)
|
|
|
|
path.chmod(old)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chown
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
path = Pathname("a")
|
|
|
|
old_uid = path.stat.uid
|
|
|
|
old_gid = path.stat.gid
|
|
|
|
begin
|
|
|
|
path.chown(0, 0)
|
|
|
|
rescue Errno::EPERM
|
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal(0, path.stat.uid)
|
|
|
|
assert_equal(0, path.stat.gid)
|
|
|
|
path.chown(old_uid, old_gid)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lchown
|
|
|
|
return if !has_symlink?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
File.symlink("a", "l")
|
|
|
|
path = Pathname("l")
|
|
|
|
old_uid = path.stat.uid
|
|
|
|
old_gid = path.stat.gid
|
|
|
|
begin
|
|
|
|
path.lchown(0, 0)
|
|
|
|
rescue Errno::EPERM
|
|
|
|
next
|
|
|
|
end
|
|
|
|
assert_equal(0, path.stat.uid)
|
|
|
|
assert_equal(0, path.stat.gid)
|
|
|
|
path.lchown(old_uid, old_gid)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fnmatch
|
|
|
|
path = Pathname("a")
|
|
|
|
assert_equal(true, path.fnmatch("*"))
|
|
|
|
assert_equal(false, path.fnmatch("*.*"))
|
2010-08-07 18:33:51 -04:00
|
|
|
assert_equal(false, Pathname(".foo").fnmatch("*"))
|
|
|
|
assert_equal(true, Pathname(".foo").fnmatch("*", File::FNM_DOTMATCH))
|
2010-08-03 10:35:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fnmatch?
|
|
|
|
path = Pathname("a")
|
|
|
|
assert_equal(true, path.fnmatch?("*"))
|
|
|
|
assert_equal(false, path.fnmatch?("*.*"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ftype
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal("file", Pathname("f").ftype)
|
|
|
|
Dir.mkdir("d")
|
|
|
|
assert_equal("directory", Pathname("d").ftype)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-08 07:41:15 -04:00
|
|
|
def test_make_link
|
2020-10-12 08:26:05 -04:00
|
|
|
return if !has_hardlink?
|
2010-08-03 10:35:34 -04:00
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
Pathname("l").make_link(Pathname("a"))
|
|
|
|
assert_equal("abc", Pathname("l").read)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
2010-08-09 09:15:49 -04:00
|
|
|
path = Pathname("a")
|
|
|
|
|
|
|
|
path.open {|f|
|
|
|
|
assert_equal("abc", f.read)
|
|
|
|
}
|
|
|
|
|
|
|
|
path.open("r") {|f|
|
2010-08-03 10:35:34 -04:00
|
|
|
assert_equal("abc", f.read)
|
|
|
|
}
|
2010-08-09 09:15:49 -04:00
|
|
|
|
2019-09-25 18:32:35 -04:00
|
|
|
path.open(mode: "r") {|f|
|
|
|
|
assert_equal("abc", f.read)
|
|
|
|
}
|
|
|
|
|
2010-08-09 09:15:49 -04:00
|
|
|
Pathname("b").open("w", 0444) {|f| f.write "def" }
|
2017-10-21 10:22:12 -04:00
|
|
|
assert_equal(0444 & ~File.umask, File.stat("b").mode & 0777)
|
2010-08-09 09:15:49 -04:00
|
|
|
assert_equal("def", File.read("b"))
|
|
|
|
|
Make rb_scan_args handle keywords more similar to Ruby methods (#2460)
Cfuncs that use rb_scan_args with the : entry suffer similar keyword
argument separation issues that Ruby methods suffer if the cfuncs
accept optional or variable arguments.
This makes the following changes to : handling.
* Treats as **kw, prompting keyword argument separation warnings
if called with a positional hash.
* Do not look for an option hash if empty keywords are provided.
For backwards compatibility, treat an empty keyword splat as a empty
mandatory positional hash argument, but emit a a warning, as this
behavior will be removed in Ruby 3. The argument number check
needs to be moved lower so it can correctly handle an empty
positional argument being added.
* If the last argument is nil and it is necessary to treat it as an option
hash in order to make sure all arguments are processed, continue to
treat the last argument as the option hash. Emit a warning in this case,
as this behavior will be removed in Ruby 3.
* If splitting the keyword hash into two hashes, issue a warning, as we
will not be splitting hashes in Ruby 3.
* If the keyword argument is required to fill a mandatory positional
argument, continue to do so, but emit a warning as this behavior will
be going away in Ruby 3.
* If keyword arguments are provided and the last argument is not a hash,
that indicates something wrong. This can happen if a cfunc is calling
rb_scan_args multiple times, and providing arguments that were not
passed to it from Ruby. Callers need to switch to the new
rb_scan_args_kw function, which allows passing of whether keywords
were provided.
This commit fixes all warnings caused by the changes above.
It switches some function calls to *_kw versions with appropriate
kw_splat flags. If delegating arguments, RB_PASS_CALLED_KEYWORDS
is used. If creating new arguments, RB_PASS_KEYWORDS is used if
the last argument is a hash to be treated as keywords.
In open_key_args in io.c, use rb_scan_args_kw.
In this case, the arguments provided come from another C
function, not Ruby. The last argument may or may not be a hash,
so we can't set keyword argument mode. However, if it is a
hash, we don't want to warn when treating it as keywords.
In Ruby files, make sure to appropriately use keyword splats
or literal keywords when calling Cfuncs that now issue keyword
argument separation warnings through rb_scan_args. Also, make
sure not to pass nil in place of an option hash.
Work around Kernel#warn warnings due to problems in the Rubygems
override of the method. There is an open pull request to fix
these issues in Rubygems, but part of the Rubygems tests for
their override fail on ruby-head due to rb_scan_args not
recognizing empty keyword splats, which this commit fixes.
Implementation wise, adding rb_scan_args_kw is kind of a pain,
because rb_scan_args takes a variable number of arguments.
In order to not duplicate all the code, the function internals need
to be split into two functions taking a va_list, and to avoid passing
in a ton of arguments, a single struct argument is used to handle
the variables previously local to the function.
2019-09-25 14:18:49 -04:00
|
|
|
Pathname("c").open("w", 0444, **{}) {|f| f.write "ghi" }
|
2017-10-21 10:22:12 -04:00
|
|
|
assert_equal(0444 & ~File.umask, File.stat("c").mode & 0777)
|
2010-08-09 09:15:49 -04:00
|
|
|
assert_equal("ghi", File.read("c"))
|
|
|
|
|
|
|
|
g = path.open
|
|
|
|
assert_equal("abc", g.read)
|
|
|
|
g.close
|
2019-09-25 18:32:35 -04:00
|
|
|
|
|
|
|
g = path.open(mode: "r")
|
|
|
|
assert_equal("abc", g.read)
|
|
|
|
g.close
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_readlink
|
|
|
|
return if !has_symlink?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
File.symlink("a", "l")
|
|
|
|
assert_equal(Pathname("a"), Pathname("l").readlink)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rename
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
Pathname("a").rename(Pathname("b"))
|
|
|
|
assert_equal("abc", File.read("b"))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stat
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
s = Pathname("a").stat
|
|
|
|
assert_equal(3, s.size)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lstat
|
|
|
|
return if !has_symlink?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
File.symlink("a", "l")
|
|
|
|
s = Pathname("l").lstat
|
|
|
|
assert_equal(true, s.symlink?)
|
|
|
|
s = Pathname("l").stat
|
|
|
|
assert_equal(false, s.symlink?)
|
|
|
|
assert_equal(3, s.size)
|
|
|
|
s = Pathname("a").lstat
|
|
|
|
assert_equal(false, s.symlink?)
|
|
|
|
assert_equal(3, s.size)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_make_symlink
|
|
|
|
return if !has_symlink?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
Pathname("l").make_symlink(Pathname("a"))
|
|
|
|
s = Pathname("l").lstat
|
|
|
|
assert_equal(true, s.symlink?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_truncate
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
Pathname("a").truncate(2)
|
|
|
|
assert_equal("ab", File.read("a"))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_utime
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
atime = Time.utc(2000)
|
|
|
|
mtime = Time.utc(1999)
|
|
|
|
Pathname("a").utime(atime, mtime)
|
|
|
|
s = File.stat("a")
|
|
|
|
assert_equal(atime, s.atime)
|
|
|
|
assert_equal(mtime, s.mtime)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-04-03 13:03:09 -04:00
|
|
|
def test_lutime
|
|
|
|
return if !has_symlink?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {|f| f.write "abc" }
|
|
|
|
atime = File.atime("a")
|
|
|
|
mtime = File.mtime("a")
|
|
|
|
latime = Time.utc(2000)
|
|
|
|
lmtime = Time.utc(1999)
|
|
|
|
File.symlink("a", "l")
|
|
|
|
Pathname("l").utime(latime, lmtime)
|
|
|
|
s = File.lstat("a")
|
|
|
|
ls = File.lstat("l")
|
|
|
|
assert_equal(atime, s.atime)
|
|
|
|
assert_equal(mtime, s.mtime)
|
|
|
|
assert_equal(latime, ls.atime)
|
|
|
|
assert_equal(lmtime, ls.mtime)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_basename
|
|
|
|
assert_equal(Pathname("basename"), Pathname("dirname/basename").basename)
|
2010-08-16 08:40:10 -04:00
|
|
|
assert_equal(Pathname("bar"), Pathname("foo/bar.x").basename(".x"))
|
2010-08-03 10:35:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dirname
|
|
|
|
assert_equal(Pathname("dirname"), Pathname("dirname/basename").dirname)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extname
|
|
|
|
assert_equal(".ext", Pathname("basename.ext").extname)
|
|
|
|
end
|
|
|
|
|
2010-08-19 21:40:51 -04:00
|
|
|
def test_expand_path
|
2010-08-28 20:50:58 -04:00
|
|
|
drv = DOSISH_DRIVE_LETTER ? Dir.pwd.sub(%r(/.*), '') : ""
|
|
|
|
assert_equal(Pathname(drv + "/a"), Pathname("/a").expand_path)
|
|
|
|
assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path("/"))
|
|
|
|
assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path(Pathname("/")))
|
|
|
|
assert_equal(Pathname(drv + "/b"), Pathname("/b").expand_path(Pathname("/a")))
|
|
|
|
assert_equal(Pathname(drv + "/a/b"), Pathname("b").expand_path(Pathname("/a")))
|
2010-08-19 21:40:51 -04:00
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_split
|
|
|
|
assert_equal([Pathname("dirname"), Pathname("basename")], Pathname("dirname/basename").split)
|
2021-03-27 01:45:10 -04:00
|
|
|
|
|
|
|
assert_separately([], <<-'end;')
|
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
mod = Module.new do
|
|
|
|
def split(_arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
File.singleton_class.prepend(mod)
|
|
|
|
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Pathname('/').split
|
|
|
|
end
|
|
|
|
end;
|
2010-08-03 10:35:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_blockdev?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").blockdev?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chardev?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").chardev?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_executable?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").executable?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_executable_real?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").executable_real?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exist?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(true, Pathname("f").exist?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_grpowned?
|
2021-09-06 23:21:49 -04:00
|
|
|
omit "Unix file owner test" if DOSISH
|
2010-08-03 10:35:34 -04:00
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
2010-10-29 17:40:09 -04:00
|
|
|
File.chown(-1, Process.gid, "f")
|
2010-08-03 10:35:34 -04:00
|
|
|
assert_equal(true, Pathname("f").grpowned?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_directory?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").directory?)
|
|
|
|
Dir.mkdir("d")
|
|
|
|
assert_equal(true, Pathname("d").directory?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(true, Pathname("f").file?)
|
|
|
|
Dir.mkdir("d")
|
|
|
|
assert_equal(false, Pathname("d").file?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pipe?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").pipe?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_socket?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").socket?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_owned?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(true, Pathname("f").owned?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_readable?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(true, Pathname("f").readable?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_world_readable?
|
2021-09-06 23:21:49 -04:00
|
|
|
omit "Unix file mode bit test" if DOSISH
|
2010-08-03 10:35:34 -04:00
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
File.chmod(0400, "f")
|
|
|
|
assert_equal(nil, Pathname("f").world_readable?)
|
|
|
|
File.chmod(0444, "f")
|
|
|
|
assert_equal(0444, Pathname("f").world_readable?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_readable_real?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(true, Pathname("f").readable_real?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_setuid?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").setuid?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_setgid?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").setgid?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(3, Pathname("f").size)
|
|
|
|
open("z", "w") {|f| }
|
|
|
|
assert_equal(0, Pathname("z").size)
|
|
|
|
assert_raise(Errno::ENOENT) { Pathname("not-exist").size }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(3, Pathname("f").size?)
|
|
|
|
open("z", "w") {|f| }
|
|
|
|
assert_equal(nil, Pathname("z").size?)
|
|
|
|
assert_equal(nil, Pathname("not-exist").size?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sticky?
|
2021-09-06 23:21:49 -04:00
|
|
|
omit "Unix file mode bit test" if DOSISH
|
2010-08-03 10:35:34 -04:00
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").sticky?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_symlink?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").symlink?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_writable?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(true, Pathname("f").writable?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_world_writable?
|
2021-09-06 23:21:49 -04:00
|
|
|
omit "Unix file mode bit test" if DOSISH
|
2010-08-03 10:35:34 -04:00
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
File.chmod(0600, "f")
|
|
|
|
assert_equal(nil, Pathname("f").world_writable?)
|
|
|
|
File.chmod(0666, "f")
|
|
|
|
assert_equal(0666, Pathname("f").world_writable?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_writable_real?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(true, Pathname("f").writable?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_zero?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
assert_equal(false, Pathname("f").zero?)
|
|
|
|
open("z", "w") {|f| }
|
|
|
|
assert_equal(true, Pathname("z").zero?)
|
|
|
|
assert_equal(false, Pathname("not-exist").zero?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-11-04 23:29:08 -04:00
|
|
|
def test_empty?
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("nonemptyfile", "w") {|f| f.write "abc" }
|
|
|
|
open("emptyfile", "w") {|f| }
|
|
|
|
Dir.mkdir("nonemptydir")
|
|
|
|
open("nonemptydir/somefile", "w") {|f| }
|
|
|
|
Dir.mkdir("emptydir")
|
|
|
|
assert_equal(true, Pathname("emptyfile").empty?)
|
|
|
|
assert_equal(false, Pathname("nonemptyfile").empty?)
|
|
|
|
assert_equal(true, Pathname("emptydir").empty?)
|
|
|
|
assert_equal(false, Pathname("nonemptydir").empty?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_s_glob
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
Dir.mkdir("d")
|
|
|
|
assert_equal([Pathname("d"), Pathname("f")], Pathname.glob("*").sort)
|
2010-09-13 16:14:04 -04:00
|
|
|
a = []
|
|
|
|
Pathname.glob("*") {|path| a << path }
|
|
|
|
a.sort!
|
|
|
|
assert_equal([Pathname("d"), Pathname("f")], a)
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-07-14 04:39:21 -04:00
|
|
|
def test_s_glob_3args
|
2021-02-10 01:53:44 -05:00
|
|
|
expect = RUBY_VERSION >= "3.1" ? [Pathname("."), Pathname("f")] : [Pathname("."), Pathname(".."), Pathname("f")]
|
2019-07-14 04:39:21 -04:00
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
Dir.chdir("/") {
|
|
|
|
assert_equal(
|
2021-02-10 01:53:44 -05:00
|
|
|
expect,
|
2019-07-14 04:39:21 -04:00
|
|
|
Pathname.glob("*", File::FNM_DOTMATCH, base: dir).sort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_s_getwd
|
|
|
|
wd = Pathname.getwd
|
|
|
|
assert_kind_of(Pathname, wd)
|
|
|
|
end
|
|
|
|
|
2010-09-14 07:18:37 -04:00
|
|
|
def test_s_pwd
|
|
|
|
wd = Pathname.pwd
|
|
|
|
assert_kind_of(Pathname, wd)
|
|
|
|
end
|
|
|
|
|
2017-10-21 04:34:49 -04:00
|
|
|
def test_glob
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
Dir.mkdir("d")
|
|
|
|
open("d/f", "w") {|f| f.write "abc" }
|
|
|
|
Dir.mkdir("d/e")
|
|
|
|
assert_equal([Pathname("d/e"), Pathname("d/f")], Pathname("d").glob("*").sort)
|
|
|
|
a = []
|
|
|
|
Pathname("d").glob("*") {|path| a << path }
|
|
|
|
a.sort!
|
|
|
|
assert_equal([Pathname("d/e"), Pathname("d/f")], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_entries
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
open("b", "w") {}
|
|
|
|
assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], Pathname(".").entries.sort)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_entry
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
open("b", "w") {}
|
|
|
|
a = []
|
|
|
|
Pathname(".").each_entry {|v| a << v }
|
|
|
|
assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], a.sort)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-17 15:12:08 -04:00
|
|
|
def test_each_entry_enumerator
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
open("b", "w") {}
|
|
|
|
a = []
|
|
|
|
e = Pathname(".").each_entry
|
|
|
|
assert_kind_of(Enumerator, e)
|
|
|
|
e.each {|v| a << v }
|
|
|
|
assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], a.sort)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_mkdir
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
Pathname("d").mkdir
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.directory?("d")
|
2010-09-15 08:07:43 -04:00
|
|
|
Pathname("e").mkdir(0770)
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.directory?("e")
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rmdir
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
Pathname("d").mkdir
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.directory?("d")
|
2010-08-03 10:35:34 -04:00
|
|
|
Pathname("d").rmdir
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.not_exist?("d")
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_opendir
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
open("b", "w") {}
|
|
|
|
a = []
|
|
|
|
Pathname(".").opendir {|d|
|
|
|
|
d.each {|e| a << e }
|
|
|
|
}
|
|
|
|
assert_equal([".", "..", "a", "b"], a.sort)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("a", "w") {}
|
|
|
|
open("b", "w") {}
|
|
|
|
Dir.mkdir("d")
|
|
|
|
open("d/x", "w") {}
|
|
|
|
open("d/y", "w") {}
|
|
|
|
a = []; Pathname(".").find {|v| a << v }; a.sort!
|
|
|
|
assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
|
|
|
|
a = []; Pathname("d").find {|v| a << v }; a.sort!
|
|
|
|
assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
|
2011-11-19 07:52:37 -05:00
|
|
|
a = Pathname(".").find.sort
|
|
|
|
assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
|
|
|
|
a = Pathname("d").find.sort
|
|
|
|
assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
|
2014-03-04 10:44:53 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
File.unlink("d/y")
|
|
|
|
File.chmod(0600, "d")
|
|
|
|
a = []; Pathname(".").find(ignore_error: true) {|v| a << v }; a.sort!
|
|
|
|
assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x")], a)
|
|
|
|
a = []; Pathname("d").find(ignore_error: true) {|v| a << v }; a.sort!
|
|
|
|
assert_equal([Pathname("d"), Pathname("d/x")], a)
|
|
|
|
|
2021-09-06 23:21:49 -04:00
|
|
|
omit "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
omit 'skipped in root privilege' if Process.uid == 0
|
2014-03-04 10:44:53 -05:00
|
|
|
a = [];
|
|
|
|
assert_raise_with_message(Errno::EACCES, %r{d/x}) do
|
|
|
|
Pathname(".").find(ignore_error: false) {|v| a << v }
|
|
|
|
end
|
|
|
|
a.sort!
|
|
|
|
assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x")], a)
|
|
|
|
a = [];
|
|
|
|
assert_raise_with_message(Errno::EACCES, %r{d/x}) do
|
|
|
|
Pathname("d").find(ignore_error: false) {|v| a << v }
|
|
|
|
end
|
|
|
|
a.sort!
|
|
|
|
assert_equal([Pathname("d"), Pathname("d/x")], a)
|
|
|
|
ensure
|
|
|
|
File.chmod(0700, "d")
|
|
|
|
end
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-20 22:33:09 -04:00
|
|
|
def assert_mode(val, mask, path, mesg = nil)
|
|
|
|
st = File.stat(path)
|
|
|
|
assert_equal(val.to_s(8), (st.mode & mask).to_s(8), st.inspect)
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:35:34 -04:00
|
|
|
def test_mkpath
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
Pathname("a/b/c/d").mkpath
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.directory?("a/b/c/d")
|
2020-06-20 22:33:09 -04:00
|
|
|
unless File.stat(dir).world_readable?
|
|
|
|
# mktmpdir should make unreadable
|
|
|
|
Pathname("x/y/z").mkpath(mode: 0775)
|
|
|
|
assert_mode(0775, 0777, "x")
|
|
|
|
assert_mode(0775, 0777, "x/y")
|
|
|
|
assert_mode(0775, 0777, "x/y/z")
|
|
|
|
end
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rmtree
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
Pathname("a/b/c/d").mkpath
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.exist?("a/b/c/d")
|
2010-08-03 10:35:34 -04:00
|
|
|
Pathname("a").rmtree
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.not_exist?("a")
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink
|
|
|
|
with_tmpchdir('rubytest-pathname') {|dir|
|
|
|
|
open("f", "w") {|f| f.write "abc" }
|
|
|
|
Pathname("f").unlink
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.not_exist?("f")
|
2010-08-03 10:35:34 -04:00
|
|
|
Dir.mkdir("d")
|
|
|
|
Pathname("d").unlink
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.not_exist?("d")
|
2010-08-03 10:35:34 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_matchop
|
|
|
|
assert_raise(NoMethodError) { Pathname("a") =~ /a/ }
|
|
|
|
end
|
|
|
|
|
2007-12-09 00:12:31 -05:00
|
|
|
def test_file_basename
|
|
|
|
assert_equal("bar", File.basename(Pathname.new("foo/bar")))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_dirname
|
|
|
|
assert_equal("foo", File.dirname(Pathname.new("foo/bar")))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_split
|
|
|
|
assert_equal(["foo", "bar"], File.split(Pathname.new("foo/bar")))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_extname
|
|
|
|
assert_equal(".baz", File.extname(Pathname.new("bar.baz")))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_fnmatch
|
2020-06-20 22:31:48 -04:00
|
|
|
assert_file.fnmatch("*.*", Pathname.new("bar.baz"))
|
2007-12-09 00:12:31 -05:00
|
|
|
end
|
|
|
|
|
2014-09-14 21:29:21 -04:00
|
|
|
def test_relative_path_from_casefold
|
|
|
|
assert_separately([], <<-'end;') # do
|
|
|
|
module File::Constants
|
|
|
|
remove_const :FNM_SYSCASE
|
|
|
|
FNM_SYSCASE = FNM_CASEFOLD
|
|
|
|
end
|
|
|
|
require 'pathname'
|
|
|
|
foo = Pathname.new("fo\u{f6}")
|
|
|
|
bar = Pathname.new("b\u{e4}r".encode("ISO-8859-1"))
|
|
|
|
assert_instance_of(Pathname, foo.relative_path_from(bar))
|
|
|
|
end;
|
|
|
|
end
|
2018-12-16 07:26:52 -05:00
|
|
|
|
|
|
|
def test_relative_path_from_mock
|
|
|
|
assert_equal(
|
|
|
|
Pathname.new("../bar"),
|
|
|
|
Pathname.new("/foo/bar").relative_path_from(Pathname.new("/foo/baz")))
|
|
|
|
assert_equal(
|
|
|
|
Pathname.new("../bar"),
|
|
|
|
Pathname.new("/foo/bar").relative_path_from("/foo/baz"))
|
|
|
|
obj = Object.new
|
|
|
|
def obj.cleanpath() Pathname.new("/foo/baz") end
|
2018-12-17 22:09:54 -05:00
|
|
|
def obj.is_a?(m) m == Pathname end
|
2018-12-16 07:26:52 -05:00
|
|
|
assert_equal(
|
|
|
|
Pathname.new("../bar"),
|
|
|
|
Pathname.new("/foo/bar").relative_path_from(obj))
|
|
|
|
end
|
2005-11-26 15:43:08 -05:00
|
|
|
end
|