PageRenderTime 32ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test/pathname/test_pathname.rb

http://github.com/ruby/ruby
Ruby | 1450 lines | 1248 code | 194 blank | 8 comment | 40 complexity | 5d028f0ab7501db09d1ed0f64e41b735 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: true
  2. require 'test/unit'
  3. require 'pathname'
  4. require 'fileutils'
  5. require 'tmpdir'
  6. class TestPathname < Test::Unit::TestCase
  7. def self.define_assertion(name, linenum, &block)
  8. name = "test_#{name}_#{linenum}"
  9. define_method(name, &block)
  10. end
  11. def self.get_linenum
  12. if loc = caller_locations(2, 1)
  13. loc[0].lineno
  14. else
  15. nil
  16. end
  17. end
  18. def self.defassert(name, result, *args)
  19. define_assertion(name, get_linenum) {
  20. mesg = "#{name}(#{args.map {|a| a.inspect }.join(', ')})"
  21. assert_nothing_raised(mesg) {
  22. assert_equal(result, self.send(name, *args), mesg)
  23. }
  24. }
  25. end
  26. def self.defassert_raise(name, exc, *args)
  27. define_assertion(name, get_linenum) {
  28. message = "#{name}(#{args.map {|a| a.inspect }.join(', ')})"
  29. assert_raise(exc, message) { self.send(name, *args) }
  30. }
  31. end
  32. DOSISH = File::ALT_SEPARATOR != nil
  33. DOSISH_DRIVE_LETTER = File.dirname("A:") == "A:."
  34. DOSISH_UNC = File.dirname("//") == "//"
  35. def cleanpath_aggressive(path)
  36. Pathname.new(path).cleanpath.to_s
  37. end
  38. defassert(:cleanpath_aggressive, '/', '/')
  39. defassert(:cleanpath_aggressive, '.', '')
  40. defassert(:cleanpath_aggressive, '.', '.')
  41. defassert(:cleanpath_aggressive, '..', '..')
  42. defassert(:cleanpath_aggressive, 'a', 'a')
  43. defassert(:cleanpath_aggressive, '/', '/.')
  44. defassert(:cleanpath_aggressive, '/', '/..')
  45. defassert(:cleanpath_aggressive, '/a', '/a')
  46. defassert(:cleanpath_aggressive, '.', './')
  47. defassert(:cleanpath_aggressive, '..', '../')
  48. defassert(:cleanpath_aggressive, 'a', 'a/')
  49. defassert(:cleanpath_aggressive, 'a/b', 'a//b')
  50. defassert(:cleanpath_aggressive, 'a', 'a/.')
  51. defassert(:cleanpath_aggressive, 'a', 'a/./')
  52. defassert(:cleanpath_aggressive, '.', 'a/..')
  53. defassert(:cleanpath_aggressive, '.', 'a/../')
  54. defassert(:cleanpath_aggressive, '/a', '/a/.')
  55. defassert(:cleanpath_aggressive, '..', './..')
  56. defassert(:cleanpath_aggressive, '..', '../.')
  57. defassert(:cleanpath_aggressive, '..', './../')
  58. defassert(:cleanpath_aggressive, '..', '.././')
  59. defassert(:cleanpath_aggressive, '/', '/./..')
  60. defassert(:cleanpath_aggressive, '/', '/../.')
  61. defassert(:cleanpath_aggressive, '/', '/./../')
  62. defassert(:cleanpath_aggressive, '/', '/.././')
  63. defassert(:cleanpath_aggressive, 'a/b/c', 'a/b/c')
  64. defassert(:cleanpath_aggressive, 'b/c', './b/c')
  65. defassert(:cleanpath_aggressive, 'a/c', 'a/./c')
  66. defassert(:cleanpath_aggressive, 'a/b', 'a/b/.')
  67. defassert(:cleanpath_aggressive, '.', 'a/../.')
  68. defassert(:cleanpath_aggressive, '/a', '/../.././../a')
  69. defassert(:cleanpath_aggressive, '../../d', 'a/b/../../../../c/../d')
  70. if DOSISH_UNC
  71. defassert(:cleanpath_aggressive, '//a/b/c', '//a/b/c/')
  72. else
  73. defassert(:cleanpath_aggressive, '/', '///')
  74. defassert(:cleanpath_aggressive, '/a', '///a')
  75. defassert(:cleanpath_aggressive, '/', '///..')
  76. defassert(:cleanpath_aggressive, '/', '///.')
  77. defassert(:cleanpath_aggressive, '/', '///a/../..')
  78. end
  79. if DOSISH
  80. defassert(:cleanpath_aggressive, 'c:/foo/bar', 'c:\\foo\\bar')
  81. end
  82. def cleanpath_conservative(path)
  83. Pathname.new(path).cleanpath(true).to_s
  84. end
  85. defassert(:cleanpath_conservative, '/', '/')
  86. defassert(:cleanpath_conservative, '.', '')
  87. defassert(:cleanpath_conservative, '.', '.')
  88. defassert(:cleanpath_conservative, '..', '..')
  89. defassert(:cleanpath_conservative, 'a', 'a')
  90. defassert(:cleanpath_conservative, '/', '/.')
  91. defassert(:cleanpath_conservative, '/', '/..')
  92. defassert(:cleanpath_conservative, '/a', '/a')
  93. defassert(:cleanpath_conservative, '.', './')
  94. defassert(:cleanpath_conservative, '..', '../')
  95. defassert(:cleanpath_conservative, 'a/', 'a/')
  96. defassert(:cleanpath_conservative, 'a/b', 'a//b')
  97. defassert(:cleanpath_conservative, 'a/.', 'a/.')
  98. defassert(:cleanpath_conservative, 'a/.', 'a/./')
  99. defassert(:cleanpath_conservative, 'a/..', 'a/../')
  100. defassert(:cleanpath_conservative, '/a/.', '/a/.')
  101. defassert(:cleanpath_conservative, '..', './..')
  102. defassert(:cleanpath_conservative, '..', '../.')
  103. defassert(:cleanpath_conservative, '..', './../')
  104. defassert(:cleanpath_conservative, '..', '.././')
  105. defassert(:cleanpath_conservative, '/', '/./..')
  106. defassert(:cleanpath_conservative, '/', '/../.')
  107. defassert(:cleanpath_conservative, '/', '/./../')
  108. defassert(:cleanpath_conservative, '/', '/.././')
  109. defassert(:cleanpath_conservative, 'a/b/c', 'a/b/c')
  110. defassert(:cleanpath_conservative, 'b/c', './b/c')
  111. defassert(:cleanpath_conservative, 'a/c', 'a/./c')
  112. defassert(:cleanpath_conservative, 'a/b/.', 'a/b/.')
  113. defassert(:cleanpath_conservative, 'a/..', 'a/../.')
  114. defassert(:cleanpath_conservative, '/a', '/../.././../a')
  115. defassert(:cleanpath_conservative, 'a/b/../../../../c/../d', 'a/b/../../../../c/../d')
  116. if DOSISH
  117. defassert(:cleanpath_conservative, 'c:/foo/bar', 'c:\\foo\\bar')
  118. end
  119. if DOSISH_UNC
  120. defassert(:cleanpath_conservative, '//', '//')
  121. else
  122. defassert(:cleanpath_conservative, '/', '//')
  123. end
  124. # has_trailing_separator?(path) -> bool
  125. def has_trailing_separator?(path)
  126. Pathname.allocate.__send__(:has_trailing_separator?, path)
  127. end
  128. defassert(:has_trailing_separator?, false, "/")
  129. defassert(:has_trailing_separator?, false, "///")
  130. defassert(:has_trailing_separator?, false, "a")
  131. defassert(:has_trailing_separator?, true, "a/")
  132. def add_trailing_separator(path)
  133. Pathname.allocate.__send__(:add_trailing_separator, path)
  134. end
  135. def del_trailing_separator(path)
  136. Pathname.allocate.__send__(:del_trailing_separator, path)
  137. end
  138. defassert(:del_trailing_separator, "/", "/")
  139. defassert(:del_trailing_separator, "/a", "/a")
  140. defassert(:del_trailing_separator, "/a", "/a/")
  141. defassert(:del_trailing_separator, "/a", "/a//")
  142. defassert(:del_trailing_separator, ".", ".")
  143. defassert(:del_trailing_separator, ".", "./")
  144. defassert(:del_trailing_separator, ".", ".//")
  145. if DOSISH_DRIVE_LETTER
  146. defassert(:del_trailing_separator, "A:", "A:")
  147. defassert(:del_trailing_separator, "A:/", "A:/")
  148. defassert(:del_trailing_separator, "A:/", "A://")
  149. defassert(:del_trailing_separator, "A:.", "A:.")
  150. defassert(:del_trailing_separator, "A:.", "A:./")
  151. defassert(:del_trailing_separator, "A:.", "A:.//")
  152. end
  153. if DOSISH_UNC
  154. defassert(:del_trailing_separator, "//", "//")
  155. defassert(:del_trailing_separator, "//a", "//a")
  156. defassert(:del_trailing_separator, "//a", "//a/")
  157. defassert(:del_trailing_separator, "//a", "//a//")
  158. defassert(:del_trailing_separator, "//a/b", "//a/b")
  159. defassert(:del_trailing_separator, "//a/b", "//a/b/")
  160. defassert(:del_trailing_separator, "//a/b", "//a/b//")
  161. defassert(:del_trailing_separator, "//a/b/c", "//a/b/c")
  162. defassert(:del_trailing_separator, "//a/b/c", "//a/b/c/")
  163. defassert(:del_trailing_separator, "//a/b/c", "//a/b/c//")
  164. else
  165. defassert(:del_trailing_separator, "/", "///")
  166. defassert(:del_trailing_separator, "///a", "///a/")
  167. end
  168. if DOSISH
  169. defassert(:del_trailing_separator, "a", "a\\")
  170. defassert(:del_trailing_separator, "\225\\".dup.force_encoding("cp932"), "\225\\\\".dup.force_encoding("cp932"))
  171. defassert(:del_trailing_separator, "\225".dup.force_encoding("cp437"), "\225\\\\".dup.force_encoding("cp437"))
  172. end
  173. def test_plus
  174. assert_kind_of(Pathname, Pathname("a") + Pathname("b"))
  175. end
  176. def plus(path1, path2) # -> path
  177. (Pathname.new(path1) + Pathname.new(path2)).to_s
  178. end
  179. defassert(:plus, '/', '/', '/')
  180. defassert(:plus, 'a/b', 'a', 'b')
  181. defassert(:plus, 'a', 'a', '.')
  182. defassert(:plus, 'b', '.', 'b')
  183. defassert(:plus, '.', '.', '.')
  184. defassert(:plus, '/b', 'a', '/b')
  185. defassert(:plus, '/', '/', '..')
  186. defassert(:plus, '.', 'a', '..')
  187. defassert(:plus, 'a', 'a/b', '..')
  188. defassert(:plus, '../..', '..', '..')
  189. defassert(:plus, '/c', '/', '../c')
  190. defassert(:plus, 'c', 'a', '../c')
  191. defassert(:plus, 'a/c', 'a/b', '../c')
  192. defassert(:plus, '../../c', '..', '../c')
  193. defassert(:plus, 'a//b/d//e', 'a//b/c', '../d//e')
  194. defassert(:plus, '//foo/var/bar', '//foo/var', 'bar')
  195. def test_slash
  196. assert_kind_of(Pathname, Pathname("a") / Pathname("b"))
  197. end
  198. def test_parent
  199. assert_equal(Pathname("."), Pathname("a").parent)
  200. end
  201. def parent(path) # -> path
  202. Pathname.new(path).parent.to_s
  203. end
  204. defassert(:parent, '/', '/')
  205. defassert(:parent, '/', '/a')
  206. defassert(:parent, '/a', '/a/b')
  207. defassert(:parent, '/a/b', '/a/b/c')
  208. defassert(:parent, '.', 'a')
  209. defassert(:parent, 'a', 'a/b')
  210. defassert(:parent, 'a/b', 'a/b/c')
  211. defassert(:parent, '..', '.')
  212. defassert(:parent, '../..', '..')
  213. def test_join
  214. r = Pathname("a").join(Pathname("b"), Pathname("c"))
  215. assert_equal(Pathname("a/b/c"), r)
  216. r = Pathname("/a").join(Pathname("b"), Pathname("c"))
  217. assert_equal(Pathname("/a/b/c"), r)
  218. r = Pathname("/a").join(Pathname("/b"), Pathname("c"))
  219. assert_equal(Pathname("/b/c"), r)
  220. r = Pathname("/a").join(Pathname("/b"), Pathname("/c"))
  221. assert_equal(Pathname("/c"), r)
  222. r = Pathname("/a").join("/b", "/c")
  223. assert_equal(Pathname("/c"), r)
  224. r = Pathname("/foo/var").join()
  225. assert_equal(Pathname("/foo/var"), r)
  226. end
  227. def test_absolute
  228. assert_equal(true, Pathname("/").absolute?)
  229. assert_equal(false, Pathname("a").absolute?)
  230. end
  231. def relative?(path)
  232. Pathname.new(path).relative?
  233. end
  234. defassert(:relative?, false, '/')
  235. defassert(:relative?, false, '/a')
  236. defassert(:relative?, false, '/..')
  237. defassert(:relative?, true, 'a')
  238. defassert(:relative?, true, 'a/b')
  239. if DOSISH_DRIVE_LETTER
  240. defassert(:relative?, false, 'A:')
  241. defassert(:relative?, false, 'A:/')
  242. defassert(:relative?, false, 'A:/a')
  243. end
  244. if File.dirname('//') == '//'
  245. defassert(:relative?, false, '//')
  246. defassert(:relative?, false, '//a')
  247. defassert(:relative?, false, '//a/')
  248. defassert(:relative?, false, '//a/b')
  249. defassert(:relative?, false, '//a/b/')
  250. defassert(:relative?, false, '//a/b/c')
  251. end
  252. def relative_path_from(dest_directory, base_directory)
  253. Pathname.new(dest_directory).relative_path_from(base_directory).to_s
  254. end
  255. defassert(:relative_path_from, "../a", Pathname.new("a"), "b")
  256. defassert(:relative_path_from, "../a", "a", "b")
  257. defassert(:relative_path_from, "../a", "a", "b/")
  258. defassert(:relative_path_from, "../a", "a/", "b")
  259. defassert(:relative_path_from, "../a", "a/", "b/")
  260. defassert(:relative_path_from, "../a", "/a", "/b")
  261. defassert(:relative_path_from, "../a", "/a", "/b/")
  262. defassert(:relative_path_from, "../a", "/a/", "/b")
  263. defassert(:relative_path_from, "../a", "/a/", "/b/")
  264. defassert(:relative_path_from, "../b", "a/b", "a/c")
  265. defassert(:relative_path_from, "../a", "../a", "../b")
  266. defassert(:relative_path_from, "a", "a", ".")
  267. defassert(:relative_path_from, "..", ".", "a")
  268. defassert(:relative_path_from, ".", ".", ".")
  269. defassert(:relative_path_from, ".", "..", "..")
  270. defassert(:relative_path_from, "..", "..", ".")
  271. defassert(:relative_path_from, "c/d", "/a/b/c/d", "/a/b")
  272. defassert(:relative_path_from, "../..", "/a/b", "/a/b/c/d")
  273. defassert(:relative_path_from, "../../../../e", "/e", "/a/b/c/d")
  274. defassert(:relative_path_from, "../b/c", "a/b/c", "a/d")
  275. defassert(:relative_path_from, "../a", "/../a", "/b")
  276. defassert(:relative_path_from, "../../a", "../a", "b")
  277. defassert(:relative_path_from, ".", "/a/../../b", "/b")
  278. defassert(:relative_path_from, "..", "a/..", "a")
  279. defassert(:relative_path_from, ".", "a/../b", "b")
  280. defassert(:relative_path_from, "a", "a", "b/..")
  281. defassert(:relative_path_from, "b/c", "b/c", "b/..")
  282. defassert_raise(:relative_path_from, ArgumentError, "/", ".")
  283. defassert_raise(:relative_path_from, ArgumentError, ".", "/")
  284. defassert_raise(:relative_path_from, ArgumentError, "a", "..")
  285. defassert_raise(:relative_path_from, ArgumentError, ".", "..")
  286. def with_tmpchdir(base=nil)
  287. Dir.mktmpdir(base) {|d|
  288. d = Pathname.new(d).realpath.to_s
  289. Dir.chdir(d) {
  290. yield d
  291. }
  292. }
  293. end
  294. def has_symlink?
  295. begin
  296. File.symlink("", "")
  297. rescue NotImplementedError, Errno::EACCES
  298. return false
  299. rescue Errno::ENOENT
  300. end
  301. return true
  302. end
  303. def realpath(path, basedir=nil)
  304. Pathname.new(path).realpath(basedir).to_s
  305. end
  306. def test_realpath
  307. return if !has_symlink?
  308. with_tmpchdir('rubytest-pathname') {|dir|
  309. assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") }
  310. File.symlink("not-exist-target", "#{dir}/not-exist")
  311. assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") }
  312. File.symlink("loop", "#{dir}/loop")
  313. assert_raise(Errno::ELOOP) { realpath("#{dir}/loop") }
  314. assert_raise(Errno::ELOOP) { realpath("#{dir}/loop", dir) }
  315. File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2")
  316. assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist2") }
  317. File.open("#{dir}/exist-target", "w") {}
  318. File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist2")
  319. assert_nothing_raised { realpath("#{dir}/exist2") }
  320. File.symlink("loop-relative", "loop-relative")
  321. assert_raise(Errno::ELOOP) { realpath("#{dir}/loop-relative") }
  322. Dir.mkdir("exist")
  323. assert_equal("#{dir}/exist", realpath("exist"))
  324. assert_raise(Errno::ELOOP) { realpath("../loop", "#{dir}/exist") }
  325. File.symlink("loop1/loop1", "loop1")
  326. assert_raise(Errno::ELOOP) { realpath("#{dir}/loop1") }
  327. File.symlink("loop2", "loop3")
  328. File.symlink("loop3", "loop2")
  329. assert_raise(Errno::ELOOP) { realpath("#{dir}/loop2") }
  330. Dir.mkdir("b")
  331. File.symlink("b", "c")
  332. assert_equal("#{dir}/b", realpath("c"))
  333. assert_equal("#{dir}/b", realpath("c/../c"))
  334. assert_equal("#{dir}/b", realpath("c/../c/../c/."))
  335. File.symlink("..", "b/d")
  336. assert_equal("#{dir}/b", realpath("c/d/c/d/c"))
  337. File.symlink("#{dir}/b", "e")
  338. assert_equal("#{dir}/b", realpath("e"))
  339. Dir.mkdir("f")
  340. Dir.mkdir("f/g")
  341. File.symlink("f/g", "h")
  342. assert_equal("#{dir}/f/g", realpath("h"))
  343. File.chmod(0000, "f")
  344. next if File.readable?("f")
  345. assert_raise(Errno::EACCES) { realpath("h") }
  346. File.chmod(0755, "f")
  347. }
  348. end
  349. def realdirpath(path)
  350. Pathname.new(path).realdirpath.to_s
  351. end
  352. def test_realdirpath
  353. return if !has_symlink?
  354. Dir.mktmpdir('rubytest-pathname') {|dir|
  355. rdir = realpath(dir)
  356. assert_equal("#{rdir}/not-exist", realdirpath("#{dir}/not-exist"))
  357. assert_raise(Errno::ENOENT) { realdirpath("#{dir}/not-exist/not-exist-child") }
  358. File.symlink("not-exist-target", "#{dir}/not-exist")
  359. assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist"))
  360. File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2")
  361. assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist2"))
  362. File.open("#{dir}/exist-target", "w") {}
  363. File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist")
  364. assert_equal("#{rdir}/exist-target", realdirpath("#{dir}/exist"))
  365. File.symlink("loop", "#{dir}/loop")
  366. assert_raise(Errno::ELOOP) { realdirpath("#{dir}/loop") }
  367. }
  368. end
  369. def descend(path)
  370. Pathname.new(path).descend.map(&:to_s)
  371. end
  372. defassert(:descend, %w[/ /a /a/b /a/b/c], "/a/b/c")
  373. defassert(:descend, %w[a a/b a/b/c], "a/b/c")
  374. defassert(:descend, %w[. ./a ./a/b ./a/b/c], "./a/b/c")
  375. defassert(:descend, %w[a/], "a/")
  376. def ascend(path)
  377. Pathname.new(path).ascend.map(&:to_s)
  378. end
  379. defassert(:ascend, %w[/a/b/c /a/b /a /], "/a/b/c")
  380. defassert(:ascend, %w[a/b/c a/b a], "a/b/c")
  381. defassert(:ascend, %w[./a/b/c ./a/b ./a .], "./a/b/c")
  382. defassert(:ascend, %w[a/], "a/")
  383. def test_blockless_ascend_is_enumerator
  384. assert_kind_of(Enumerator, Pathname.new('a').ascend)
  385. end
  386. def test_blockless_descend_is_enumerator
  387. assert_kind_of(Enumerator, Pathname.new('a').descend)
  388. end
  389. def test_initialize
  390. p1 = Pathname.new('a')
  391. assert_equal('a', p1.to_s)
  392. p2 = Pathname.new(p1)
  393. assert_equal(p1, p2)
  394. end
  395. def test_initialize_nul
  396. assert_raise(ArgumentError) { Pathname.new("a\0") }
  397. end
  398. def test_global_constructor
  399. p = Pathname.new('a')
  400. assert_equal(p, Pathname('a'))
  401. assert_same(p, Pathname(p))
  402. end
  403. class AnotherStringLike # :nodoc:
  404. def initialize(s) @s = s end
  405. def to_str() @s end
  406. def ==(other) @s == other end
  407. end
  408. def test_equality
  409. obj = Pathname.new("a")
  410. str = "a"
  411. sym = :a
  412. ano = AnotherStringLike.new("a")
  413. assert_equal(false, obj == str)
  414. assert_equal(false, str == obj)
  415. assert_equal(false, obj == ano)
  416. assert_equal(false, ano == obj)
  417. assert_equal(false, obj == sym)
  418. assert_equal(false, sym == obj)
  419. obj2 = Pathname.new("a")
  420. assert_equal(true, obj == obj2)
  421. assert_equal(true, obj === obj2)
  422. assert_equal(true, obj.eql?(obj2))
  423. end
  424. def test_hashkey
  425. h = {}
  426. h[Pathname.new("a")] = 1
  427. h[Pathname.new("a")] = 2
  428. assert_equal(1, h.size)
  429. end
  430. def assert_pathname_cmp(e, s1, s2)
  431. p1 = Pathname.new(s1)
  432. p2 = Pathname.new(s2)
  433. r = p1 <=> p2
  434. assert(e == r,
  435. "#{p1.inspect} <=> #{p2.inspect}: <#{e}> expected but was <#{r}>")
  436. end
  437. def test_comparison
  438. assert_pathname_cmp( 0, "a", "a")
  439. assert_pathname_cmp( 1, "b", "a")
  440. assert_pathname_cmp(-1, "a", "b")
  441. ss = %w(
  442. a
  443. a/
  444. a/b
  445. a.
  446. a0
  447. )
  448. s1 = ss.shift
  449. ss.each {|s2|
  450. assert_pathname_cmp(-1, s1, s2)
  451. s1 = s2
  452. }
  453. end
  454. def test_comparison_string
  455. assert_equal(nil, Pathname.new("a") <=> "a")
  456. assert_equal(nil, "a" <=> Pathname.new("a"))
  457. end
  458. def pathsub(path, pat, repl) Pathname.new(path).sub(pat, repl).to_s end
  459. defassert(:pathsub, "a.o", "a.c", /\.c\z/, ".o")
  460. def pathsubext(path, repl) Pathname.new(path).sub_ext(repl).to_s end
  461. defassert(:pathsubext, 'a.o', 'a.c', '.o')
  462. defassert(:pathsubext, 'a.o', 'a.c++', '.o')
  463. defassert(:pathsubext, 'a.png', 'a.gif', '.png')
  464. defassert(:pathsubext, 'ruby.tar.bz2', 'ruby.tar.gz', '.bz2')
  465. defassert(:pathsubext, 'd/a.o', 'd/a.c', '.o')
  466. defassert(:pathsubext, 'foo', 'foo.exe', '')
  467. defassert(:pathsubext, 'lex.yy.o', 'lex.yy.c', '.o')
  468. defassert(:pathsubext, 'fooaa.o', 'fooaa', '.o')
  469. defassert(:pathsubext, 'd.e/aa.o', 'd.e/aa', '.o')
  470. defassert(:pathsubext, 'long_enough.bug-3664', 'long_enough.not_to_be_embedded[ruby-core:31640]', '.bug-3664')
  471. def test_sub_matchdata
  472. result = Pathname("abc.gif").sub(/\..*/) {
  473. assert_not_nil($~)
  474. assert_equal(".gif", $~[0])
  475. ".png"
  476. }
  477. assert_equal("abc.png", result.to_s)
  478. end
  479. def root?(path)
  480. Pathname.new(path).root?
  481. end
  482. defassert(:root?, true, "/")
  483. defassert(:root?, true, "//")
  484. defassert(:root?, true, "///")
  485. defassert(:root?, false, "")
  486. defassert(:root?, false, "a")
  487. def test_mountpoint?
  488. r = Pathname("/").mountpoint?
  489. assert_include([true, false], r)
  490. end
  491. def test_mountpoint_enoent
  492. r = Pathname("/nonexistent").mountpoint?
  493. assert_equal false, r
  494. end
  495. def test_destructive_update
  496. path = Pathname.new("a")
  497. path.to_s.replace "b"
  498. assert_equal(Pathname.new("a"), path)
  499. end
  500. def test_null_character
  501. assert_raise(ArgumentError) { Pathname.new("\0") }
  502. end
  503. def test_freeze
  504. obj = Pathname.new("a"); assert_same(obj, obj.freeze)
  505. assert_equal(false, Pathname.new("a" ) .frozen?)
  506. assert_equal(false, Pathname.new("a".freeze) .frozen?)
  507. assert_equal(true, Pathname.new("a" ).freeze .frozen?)
  508. assert_equal(true, Pathname.new("a".freeze).freeze .frozen?)
  509. assert_equal(false, Pathname.new("a" ) .to_s.frozen?)
  510. assert_equal(false, Pathname.new("a".freeze) .to_s.frozen?)
  511. assert_equal(false, Pathname.new("a" ).freeze.to_s.frozen?)
  512. assert_equal(false, Pathname.new("a".freeze).freeze.to_s.frozen?)
  513. end
  514. def test_to_s
  515. str = "a"
  516. obj = Pathname.new(str)
  517. assert_equal(str, obj.to_s)
  518. assert_not_same(str, obj.to_s)
  519. assert_not_same(obj.to_s, obj.to_s)
  520. end
  521. def test_kernel_open
  522. count = 0
  523. result = Kernel.open(Pathname.new(__FILE__)) {|f|
  524. assert(File.identical?(__FILE__, f))
  525. count += 1
  526. 2
  527. }
  528. assert_equal(1, count)
  529. assert_equal(2, result)
  530. end
  531. def test_each_filename
  532. result = []
  533. Pathname.new("/usr/bin/ruby").each_filename {|f| result << f }
  534. assert_equal(%w[usr bin ruby], result)
  535. assert_equal(%w[usr bin ruby], Pathname.new("/usr/bin/ruby").each_filename.to_a)
  536. end
  537. def test_kernel_pathname
  538. assert_equal(Pathname.new("a"), Pathname("a"))
  539. end
  540. def test_children
  541. with_tmpchdir('rubytest-pathname') {|dir|
  542. open("a", "w") {}
  543. open("b", "w") {}
  544. Dir.mkdir("d")
  545. open("d/x", "w") {}
  546. open("d/y", "w") {}
  547. assert_equal([Pathname("a"), Pathname("b"), Pathname("d")], Pathname(".").children.sort)
  548. assert_equal([Pathname("d/x"), Pathname("d/y")], Pathname("d").children.sort)
  549. assert_equal([Pathname("x"), Pathname("y")], Pathname("d").children(false).sort)
  550. }
  551. end
  552. def test_each_child
  553. with_tmpchdir('rubytest-pathname') {|dir|
  554. open("a", "w") {}
  555. open("b", "w") {}
  556. Dir.mkdir("d")
  557. open("d/x", "w") {}
  558. open("d/y", "w") {}
  559. a = []; Pathname(".").each_child {|v| a << v }; a.sort!
  560. assert_equal([Pathname("a"), Pathname("b"), Pathname("d")], a)
  561. a = []; Pathname("d").each_child {|v| a << v }; a.sort!
  562. assert_equal([Pathname("d/x"), Pathname("d/y")], a)
  563. a = []; Pathname("d").each_child(false) {|v| a << v }; a.sort!
  564. assert_equal([Pathname("x"), Pathname("y")], a)
  565. }
  566. end
  567. def test_each_line
  568. with_tmpchdir('rubytest-pathname') {|dir|
  569. open("a", "w") {|f| f.puts 1, 2 }
  570. a = []
  571. Pathname("a").each_line {|line| a << line }
  572. assert_equal(["1\n", "2\n"], a)
  573. a = []
  574. Pathname("a").each_line("2") {|line| a << line }
  575. assert_equal(["1\n2", "\n"], a)
  576. a = []
  577. Pathname("a").each_line(1) {|line| a << line }
  578. assert_equal(["1", "\n", "2", "\n"], a)
  579. a = []
  580. Pathname("a").each_line("2", 1) {|line| a << line }
  581. assert_equal(["1", "\n", "2", "\n"], a)
  582. a = []
  583. enum = Pathname("a").each_line
  584. enum.each {|line| a << line }
  585. assert_equal(["1\n", "2\n"], a)
  586. }
  587. end
  588. def test_readlines
  589. with_tmpchdir('rubytest-pathname') {|dir|
  590. open("a", "w") {|f| f.puts 1, 2 }
  591. a = Pathname("a").readlines
  592. assert_equal(["1\n", "2\n"], a)
  593. }
  594. end
  595. def test_readlines_opts
  596. with_tmpchdir('rubytest-pathname') {|dir|
  597. open("a", "w") {|f| f.puts 1, 2 }
  598. a = Pathname("a").readlines 1, chomp: true
  599. assert_equal(["1", "", "2", ""], a)
  600. }
  601. end
  602. def test_read
  603. with_tmpchdir('rubytest-pathname') {|dir|
  604. open("a", "w") {|f| f.puts 1, 2 }
  605. assert_equal("1\n2\n", Pathname("a").read)
  606. }
  607. end
  608. def test_binread
  609. with_tmpchdir('rubytest-pathname') {|dir|
  610. open("a", "w") {|f| f.write "abc" }
  611. str = Pathname("a").binread
  612. assert_equal("abc", str)
  613. assert_equal(Encoding::ASCII_8BIT, str.encoding)
  614. }
  615. end
  616. def test_write
  617. with_tmpchdir('rubytest-pathname') {|dir|
  618. path = Pathname("a")
  619. path.write "abc"
  620. assert_equal("abc", path.read)
  621. }
  622. end
  623. def test_write_opts
  624. with_tmpchdir('rubytest-pathname') {|dir|
  625. path = Pathname("a")
  626. path.write "abc", mode: "w"
  627. assert_equal("abc", path.read)
  628. }
  629. end
  630. def test_binwrite
  631. with_tmpchdir('rubytest-pathname') {|dir|
  632. path = Pathname("a")
  633. path.binwrite "abc\x80"
  634. assert_equal("abc\x80".b, path.binread)
  635. }
  636. end
  637. def test_binwrite_opts
  638. with_tmpchdir('rubytest-pathname') {|dir|
  639. path = Pathname("a")
  640. path.binwrite "abc\x80", mode: 'w'
  641. assert_equal("abc\x80".b, path.binread)
  642. }
  643. end
  644. def test_sysopen
  645. with_tmpchdir('rubytest-pathname') {|dir|
  646. open("a", "w") {|f| f.write "abc" }
  647. fd = Pathname("a").sysopen
  648. io = IO.new(fd)
  649. begin
  650. assert_equal("abc", io.read)
  651. ensure
  652. io.close
  653. end
  654. }
  655. end
  656. def test_atime
  657. assert_kind_of(Time, Pathname(__FILE__).atime)
  658. end
  659. def test_birthtime
  660. skip if RUBY_PLATFORM =~ /android/
  661. # Check under a (probably) local filesystem.
  662. # Remote filesystems often may not support birthtime.
  663. with_tmpchdir('rubytest-pathname') do |dir|
  664. open("a", "w") {}
  665. assert_kind_of(Time, Pathname("a").birthtime)
  666. rescue Errno::EPERM
  667. # Docker prohibits statx syscall by the default.
  668. skip("statx(2) is prohibited by seccomp")
  669. rescue Errno::ENOSYS
  670. skip("statx(2) is not supported on this filesystem")
  671. rescue NotImplementedError
  672. # assert_raise(NotImplementedError) do
  673. # File.birthtime("a")
  674. # end
  675. end
  676. end
  677. def test_ctime
  678. assert_kind_of(Time, Pathname(__FILE__).ctime)
  679. end
  680. def test_mtime
  681. assert_kind_of(Time, Pathname(__FILE__).mtime)
  682. end
  683. def test_chmod
  684. with_tmpchdir('rubytest-pathname') {|dir|
  685. open("a", "w") {|f| f.write "abc" }
  686. path = Pathname("a")
  687. old = path.stat.mode
  688. path.chmod(0444)
  689. assert_equal(0444, path.stat.mode & 0777)
  690. path.chmod(old)
  691. }
  692. end
  693. def test_lchmod
  694. return if !has_symlink?
  695. with_tmpchdir('rubytest-pathname') {|dir|
  696. open("a", "w") {|f| f.write "abc" }
  697. File.symlink("a", "l")
  698. path = Pathname("l")
  699. old = path.lstat.mode
  700. begin
  701. path.lchmod(0444)
  702. rescue NotImplementedError, Errno::EOPNOTSUPP
  703. next
  704. end
  705. assert_equal(0444, path.lstat.mode & 0777)
  706. path.chmod(old)
  707. }
  708. end
  709. def test_chown
  710. with_tmpchdir('rubytest-pathname') {|dir|
  711. open("a", "w") {|f| f.write "abc" }
  712. path = Pathname("a")
  713. old_uid = path.stat.uid
  714. old_gid = path.stat.gid
  715. begin
  716. path.chown(0, 0)
  717. rescue Errno::EPERM
  718. next
  719. end
  720. assert_equal(0, path.stat.uid)
  721. assert_equal(0, path.stat.gid)
  722. path.chown(old_uid, old_gid)
  723. }
  724. end
  725. def test_lchown
  726. return if !has_symlink?
  727. with_tmpchdir('rubytest-pathname') {|dir|
  728. open("a", "w") {|f| f.write "abc" }
  729. File.symlink("a", "l")
  730. path = Pathname("l")
  731. old_uid = path.stat.uid
  732. old_gid = path.stat.gid
  733. begin
  734. path.lchown(0, 0)
  735. rescue Errno::EPERM
  736. next
  737. end
  738. assert_equal(0, path.stat.uid)
  739. assert_equal(0, path.stat.gid)
  740. path.lchown(old_uid, old_gid)
  741. }
  742. end
  743. def test_fnmatch
  744. path = Pathname("a")
  745. assert_equal(true, path.fnmatch("*"))
  746. assert_equal(false, path.fnmatch("*.*"))
  747. assert_equal(false, Pathname(".foo").fnmatch("*"))
  748. assert_equal(true, Pathname(".foo").fnmatch("*", File::FNM_DOTMATCH))
  749. end
  750. def test_fnmatch?
  751. path = Pathname("a")
  752. assert_equal(true, path.fnmatch?("*"))
  753. assert_equal(false, path.fnmatch?("*.*"))
  754. end
  755. def test_ftype
  756. with_tmpchdir('rubytest-pathname') {|dir|
  757. open("f", "w") {|f| f.write "abc" }
  758. assert_equal("file", Pathname("f").ftype)
  759. Dir.mkdir("d")
  760. assert_equal("directory", Pathname("d").ftype)
  761. }
  762. end
  763. def test_make_link
  764. with_tmpchdir('rubytest-pathname') {|dir|
  765. open("a", "w") {|f| f.write "abc" }
  766. Pathname("l").make_link(Pathname("a"))
  767. assert_equal("abc", Pathname("l").read)
  768. }
  769. end
  770. def test_open
  771. with_tmpchdir('rubytest-pathname') {|dir|
  772. open("a", "w") {|f| f.write "abc" }
  773. path = Pathname("a")
  774. path.open {|f|
  775. assert_equal("abc", f.read)
  776. }
  777. path.open("r") {|f|
  778. assert_equal("abc", f.read)
  779. }
  780. path.open(mode: "r") {|f|
  781. assert_equal("abc", f.read)
  782. }
  783. Pathname("b").open("w", 0444) {|f| f.write "def" }
  784. assert_equal(0444 & ~File.umask, File.stat("b").mode & 0777)
  785. assert_equal("def", File.read("b"))
  786. Pathname("c").open("w", 0444, **{}) {|f| f.write "ghi" }
  787. assert_equal(0444 & ~File.umask, File.stat("c").mode & 0777)
  788. assert_equal("ghi", File.read("c"))
  789. g = path.open
  790. assert_equal("abc", g.read)
  791. g.close
  792. g = path.open(mode: "r")
  793. assert_equal("abc", g.read)
  794. g.close
  795. }
  796. end
  797. def test_readlink
  798. return if !has_symlink?
  799. with_tmpchdir('rubytest-pathname') {|dir|
  800. open("a", "w") {|f| f.write "abc" }
  801. File.symlink("a", "l")
  802. assert_equal(Pathname("a"), Pathname("l").readlink)
  803. }
  804. end
  805. def test_rename
  806. with_tmpchdir('rubytest-pathname') {|dir|
  807. open("a", "w") {|f| f.write "abc" }
  808. Pathname("a").rename(Pathname("b"))
  809. assert_equal("abc", File.read("b"))
  810. }
  811. end
  812. def test_stat
  813. with_tmpchdir('rubytest-pathname') {|dir|
  814. open("a", "w") {|f| f.write "abc" }
  815. s = Pathname("a").stat
  816. assert_equal(3, s.size)
  817. }
  818. end
  819. def test_lstat
  820. return if !has_symlink?
  821. with_tmpchdir('rubytest-pathname') {|dir|
  822. open("a", "w") {|f| f.write "abc" }
  823. File.symlink("a", "l")
  824. s = Pathname("l").lstat
  825. assert_equal(true, s.symlink?)
  826. s = Pathname("l").stat
  827. assert_equal(false, s.symlink?)
  828. assert_equal(3, s.size)
  829. s = Pathname("a").lstat
  830. assert_equal(false, s.symlink?)
  831. assert_equal(3, s.size)
  832. }
  833. end
  834. def test_make_symlink
  835. return if !has_symlink?
  836. with_tmpchdir('rubytest-pathname') {|dir|
  837. open("a", "w") {|f| f.write "abc" }
  838. Pathname("l").make_symlink(Pathname("a"))
  839. s = Pathname("l").lstat
  840. assert_equal(true, s.symlink?)
  841. }
  842. end
  843. def test_truncate
  844. with_tmpchdir('rubytest-pathname') {|dir|
  845. open("a", "w") {|f| f.write "abc" }
  846. Pathname("a").truncate(2)
  847. assert_equal("ab", File.read("a"))
  848. }
  849. end
  850. def test_utime
  851. with_tmpchdir('rubytest-pathname') {|dir|
  852. open("a", "w") {|f| f.write "abc" }
  853. atime = Time.utc(2000)
  854. mtime = Time.utc(1999)
  855. Pathname("a").utime(atime, mtime)
  856. s = File.stat("a")
  857. assert_equal(atime, s.atime)
  858. assert_equal(mtime, s.mtime)
  859. }
  860. end
  861. def test_basename
  862. assert_equal(Pathname("basename"), Pathname("dirname/basename").basename)
  863. assert_equal(Pathname("bar"), Pathname("foo/bar.x").basename(".x"))
  864. end
  865. def test_dirname
  866. assert_equal(Pathname("dirname"), Pathname("dirname/basename").dirname)
  867. end
  868. def test_extname
  869. assert_equal(".ext", Pathname("basename.ext").extname)
  870. end
  871. def test_expand_path
  872. drv = DOSISH_DRIVE_LETTER ? Dir.pwd.sub(%r(/.*), '') : ""
  873. assert_equal(Pathname(drv + "/a"), Pathname("/a").expand_path)
  874. assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path("/"))
  875. assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path(Pathname("/")))
  876. assert_equal(Pathname(drv + "/b"), Pathname("/b").expand_path(Pathname("/a")))
  877. assert_equal(Pathname(drv + "/a/b"), Pathname("b").expand_path(Pathname("/a")))
  878. end
  879. def test_split
  880. assert_equal([Pathname("dirname"), Pathname("basename")], Pathname("dirname/basename").split)
  881. end
  882. def test_blockdev?
  883. with_tmpchdir('rubytest-pathname') {|dir|
  884. open("f", "w") {|f| f.write "abc" }
  885. assert_equal(false, Pathname("f").blockdev?)
  886. }
  887. end
  888. def test_chardev?
  889. with_tmpchdir('rubytest-pathname') {|dir|
  890. open("f", "w") {|f| f.write "abc" }
  891. assert_equal(false, Pathname("f").chardev?)
  892. }
  893. end
  894. def test_executable?
  895. with_tmpchdir('rubytest-pathname') {|dir|
  896. open("f", "w") {|f| f.write "abc" }
  897. assert_equal(false, Pathname("f").executable?)
  898. }
  899. end
  900. def test_executable_real?
  901. with_tmpchdir('rubytest-pathname') {|dir|
  902. open("f", "w") {|f| f.write "abc" }
  903. assert_equal(false, Pathname("f").executable_real?)
  904. }
  905. end
  906. def test_exist?
  907. with_tmpchdir('rubytest-pathname') {|dir|
  908. open("f", "w") {|f| f.write "abc" }
  909. assert_equal(true, Pathname("f").exist?)
  910. }
  911. end
  912. def test_grpowned?
  913. skip "Unix file owner test" if DOSISH
  914. with_tmpchdir('rubytest-pathname') {|dir|
  915. open("f", "w") {|f| f.write "abc" }
  916. File.chown(-1, Process.gid, "f")
  917. assert_equal(true, Pathname("f").grpowned?)
  918. }
  919. end
  920. def test_directory?
  921. with_tmpchdir('rubytest-pathname') {|dir|
  922. open("f", "w") {|f| f.write "abc" }
  923. assert_equal(false, Pathname("f").directory?)
  924. Dir.mkdir("d")
  925. assert_equal(true, Pathname("d").directory?)
  926. }
  927. end
  928. def test_file?
  929. with_tmpchdir('rubytest-pathname') {|dir|
  930. open("f", "w") {|f| f.write "abc" }
  931. assert_equal(true, Pathname("f").file?)
  932. Dir.mkdir("d")
  933. assert_equal(false, Pathname("d").file?)
  934. }
  935. end
  936. def test_pipe?
  937. with_tmpchdir('rubytest-pathname') {|dir|
  938. open("f", "w") {|f| f.write "abc" }
  939. assert_equal(false, Pathname("f").pipe?)
  940. }
  941. end
  942. def test_socket?
  943. with_tmpchdir('rubytest-pathname') {|dir|
  944. open("f", "w") {|f| f.write "abc" }
  945. assert_equal(false, Pathname("f").socket?)
  946. }
  947. end
  948. def test_owned?
  949. with_tmpchdir('rubytest-pathname') {|dir|
  950. open("f", "w") {|f| f.write "abc" }
  951. assert_equal(true, Pathname("f").owned?)
  952. }
  953. end
  954. def test_readable?
  955. with_tmpchdir('rubytest-pathname') {|dir|
  956. open("f", "w") {|f| f.write "abc" }
  957. assert_equal(true, Pathname("f").readable?)
  958. }
  959. end
  960. def test_world_readable?
  961. skip "Unix file mode bit test" if DOSISH
  962. with_tmpchdir('rubytest-pathname') {|dir|
  963. open("f", "w") {|f| f.write "abc" }
  964. File.chmod(0400, "f")
  965. assert_equal(nil, Pathname("f").world_readable?)
  966. File.chmod(0444, "f")
  967. assert_equal(0444, Pathname("f").world_readable?)
  968. }
  969. end
  970. def test_readable_real?
  971. with_tmpchdir('rubytest-pathname') {|dir|
  972. open("f", "w") {|f| f.write "abc" }
  973. assert_equal(true, Pathname("f").readable_real?)
  974. }
  975. end
  976. def test_setuid?
  977. with_tmpchdir('rubytest-pathname') {|dir|
  978. open("f", "w") {|f| f.write "abc" }
  979. assert_equal(false, Pathname("f").setuid?)
  980. }
  981. end
  982. def test_setgid?
  983. with_tmpchdir('rubytest-pathname') {|dir|
  984. open("f", "w") {|f| f.write "abc" }
  985. assert_equal(false, Pathname("f").setgid?)
  986. }
  987. end
  988. def test_size
  989. with_tmpchdir('rubytest-pathname') {|dir|
  990. open("f", "w") {|f| f.write "abc" }
  991. assert_equal(3, Pathname("f").size)
  992. open("z", "w") {|f| }
  993. assert_equal(0, Pathname("z").size)
  994. assert_raise(Errno::ENOENT) { Pathname("not-exist").size }
  995. }
  996. end
  997. def test_size?
  998. with_tmpchdir('rubytest-pathname') {|dir|
  999. open("f", "w") {|f| f.write "abc" }
  1000. assert_equal(3, Pathname("f").size?)
  1001. open("z", "w") {|f| }
  1002. assert_equal(nil, Pathname("z").size?)
  1003. assert_equal(nil, Pathname("not-exist").size?)
  1004. }
  1005. end
  1006. def test_sticky?
  1007. skip "Unix file mode bit test" if DOSISH
  1008. with_tmpchdir('rubytest-pathname') {|dir|
  1009. open("f", "w") {|f| f.write "abc" }
  1010. assert_equal(false, Pathname("f").sticky?)
  1011. }
  1012. end
  1013. def test_symlink?
  1014. with_tmpchdir('rubytest-pathname') {|dir|
  1015. open("f", "w") {|f| f.write "abc" }
  1016. assert_equal(false, Pathname("f").symlink?)
  1017. }
  1018. end
  1019. def test_writable?
  1020. with_tmpchdir('rubytest-pathname') {|dir|
  1021. open("f", "w") {|f| f.write "abc" }
  1022. assert_equal(true, Pathname("f").writable?)
  1023. }
  1024. end
  1025. def test_world_writable?
  1026. skip "Unix file mode bit test" if DOSISH
  1027. with_tmpchdir('rubytest-pathname') {|dir|
  1028. open("f", "w") {|f| f.write "abc" }
  1029. File.chmod(0600, "f")
  1030. assert_equal(nil, Pathname("f").world_writable?)
  1031. File.chmod(0666, "f")
  1032. assert_equal(0666, Pathname("f").world_writable?)
  1033. }
  1034. end
  1035. def test_writable_real?
  1036. with_tmpchdir('rubytest-pathname') {|dir|
  1037. open("f", "w") {|f| f.write "abc" }
  1038. assert_equal(true, Pathname("f").writable?)
  1039. }
  1040. end
  1041. def test_zero?
  1042. with_tmpchdir('rubytest-pathname') {|dir|
  1043. open("f", "w") {|f| f.write "abc" }
  1044. assert_equal(false, Pathname("f").zero?)
  1045. open("z", "w") {|f| }
  1046. assert_equal(true, Pathname("z").zero?)
  1047. assert_equal(false, Pathname("not-exist").zero?)
  1048. }
  1049. end
  1050. def test_empty?
  1051. with_tmpchdir('rubytest-pathname') {|dir|
  1052. open("nonemptyfile", "w") {|f| f.write "abc" }
  1053. open("emptyfile", "w") {|f| }
  1054. Dir.mkdir("nonemptydir")
  1055. open("nonemptydir/somefile", "w") {|f| }
  1056. Dir.mkdir("emptydir")
  1057. assert_equal(true, Pathname("emptyfile").empty?)
  1058. assert_equal(false, Pathname("nonemptyfile").empty?)
  1059. assert_equal(true, Pathname("emptydir").empty?)
  1060. assert_equal(false, Pathname("nonemptydir").empty?)
  1061. }
  1062. end
  1063. def test_s_glob
  1064. with_tmpchdir('rubytest-pathname') {|dir|
  1065. open("f", "w") {|f| f.write "abc" }
  1066. Dir.mkdir("d")
  1067. assert_equal([Pathname("d"), Pathname("f")], Pathname.glob("*").sort)
  1068. a = []
  1069. Pathname.glob("*") {|path| a << path }
  1070. a.sort!
  1071. assert_equal([Pathname("d"), Pathname("f")], a)
  1072. }
  1073. end
  1074. def test_s_glob_3args
  1075. with_tmpchdir('rubytest-pathname') {|dir|
  1076. open("f", "w") {|f| f.write "abc" }
  1077. Dir.chdir("/") {
  1078. assert_equal(
  1079. [Pathname("."), Pathname(".."), Pathname("f")],
  1080. Pathname.glob("*", File::FNM_DOTMATCH, base: dir).sort)
  1081. }
  1082. }
  1083. end
  1084. def test_s_getwd
  1085. wd = Pathname.getwd
  1086. assert_kind_of(Pathname, wd)
  1087. end
  1088. def test_s_pwd
  1089. wd = Pathname.pwd
  1090. assert_kind_of(Pathname, wd)
  1091. end
  1092. def test_glob
  1093. with_tmpchdir('rubytest-pathname') {|dir|
  1094. Dir.mkdir("d")
  1095. open("d/f", "w") {|f| f.write "abc" }
  1096. Dir.mkdir("d/e")
  1097. assert_equal([Pathname("d/e"), Pathname("d/f")], Pathname("d").glob("*").sort)
  1098. a = []
  1099. Pathname("d").glob("*") {|path| a << path }
  1100. a.sort!
  1101. assert_equal([Pathname("d/e"), Pathname("d/f")], a)
  1102. }
  1103. end
  1104. def test_entries
  1105. with_tmpchdir('rubytest-pathname') {|dir|
  1106. open("a", "w") {}
  1107. open("b", "w") {}
  1108. assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], Pathname(".").entries.sort)
  1109. }
  1110. end
  1111. def test_each_entry
  1112. with_tmpchdir('rubytest-pathname') {|dir|
  1113. open("a", "w") {}
  1114. open("b", "w") {}
  1115. a = []
  1116. Pathname(".").each_entry {|v| a << v }
  1117. assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], a.sort)
  1118. }
  1119. end
  1120. def test_mkdir
  1121. with_tmpchdir('rubytest-pathname') {|dir|
  1122. Pathname("d").mkdir
  1123. assert(File.directory?("d"))
  1124. Pathname("e").mkdir(0770)
  1125. assert(File.directory?("e"))
  1126. }
  1127. end
  1128. def test_rmdir
  1129. with_tmpchdir('rubytest-pathname') {|dir|
  1130. Pathname("d").mkdir
  1131. assert(File.directory?("d"))
  1132. Pathname("d").rmdir
  1133. assert(!File.exist?("d"))
  1134. }
  1135. end
  1136. def test_opendir
  1137. with_tmpchdir('rubytest-pathname') {|dir|
  1138. open("a", "w") {}
  1139. open("b", "w") {}
  1140. a = []
  1141. Pathname(".").opendir {|d|
  1142. d.each {|e| a << e }
  1143. }
  1144. assert_equal([".", "..", "a", "b"], a.sort)
  1145. }
  1146. end
  1147. def test_find
  1148. with_tmpchdir('rubytest-pathname') {|dir|
  1149. open("a", "w") {}
  1150. open("b", "w") {}
  1151. Dir.mkdir("d")
  1152. open("d/x", "w") {}
  1153. open("d/y", "w") {}
  1154. a = []; Pathname(".").find {|v| a << v }; a.sort!
  1155. assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
  1156. a = []; Pathname("d").find {|v| a << v }; a.sort!
  1157. assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
  1158. a = Pathname(".").find.sort
  1159. assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
  1160. a = Pathname("d").find.sort
  1161. assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
  1162. begin
  1163. File.unlink("d/y")
  1164. File.chmod(0600, "d")
  1165. a = []; Pathname(".").find(ignore_error: true) {|v| a << v }; a.sort!
  1166. assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x")], a)
  1167. a = []; Pathname("d").find(ignore_error: true) {|v| a << v }; a.sort!
  1168. assert_equal([Pathname("d"), Pathname("d/x")], a)
  1169. skip "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
  1170. skip 'skipped in root privilege' if Process.uid == 0
  1171. a = [];
  1172. assert_raise_with_message(Errno::EACCES, %r{d/x}) do
  1173. Pathname(".").find(ignore_error: false) {|v| a << v }
  1174. end
  1175. a.sort!
  1176. assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x")], a)
  1177. a = [];
  1178. assert_raise_with_message(Errno::EACCES, %r{d/x}) do
  1179. Pathname("d").find(ignore_error: false) {|v| a << v }
  1180. end
  1181. a.sort!
  1182. assert_equal([Pathname("d"), Pathname("d/x")], a)
  1183. ensure
  1184. File.chmod(0700, "d")
  1185. end
  1186. }
  1187. end
  1188. def test_mkpath
  1189. with_tmpchdir('rubytest-pathname') {|dir|
  1190. Pathname("a/b/c/d").mkpath
  1191. assert(File.directory?("a/b/c/d"))
  1192. }
  1193. end
  1194. def test_rmtree
  1195. with_tmpchdir('rubytest-pathname') {|dir|
  1196. Pathname("a/b/c/d").mkpath
  1197. assert(File.exist?("a/b/c/d"))
  1198. Pathname("a").rmtree
  1199. assert(!File.exist?("a"))
  1200. }
  1201. end
  1202. def test_unlink
  1203. with_tmpchdir('rubytest-pathname') {|dir|
  1204. open("f", "w") {|f| f.write "abc" }
  1205. Pathname("f").unlink
  1206. assert(!File.exist?("f"))
  1207. Dir.mkdir("d")
  1208. Pathname("d").unlink
  1209. assert(!File.exist?("d"))
  1210. }
  1211. end
  1212. def test_matchop
  1213. assert_raise(NoMethodError) { Pathname("a") =~ /a/ }
  1214. end
  1215. def test_file_basename
  1216. assert_equal("bar", File.basename(Pathname.new("foo/bar")))
  1217. end
  1218. def test_file_dirname
  1219. assert_equal("foo", File.dirname(Pathname.new("foo/bar")))
  1220. end
  1221. def test_file_split
  1222. assert_equal(["foo", "bar"], File.split(Pathname.new("foo/bar")))
  1223. end
  1224. def test_file_extname
  1225. assert_equal(".baz", File.extname(Pathname.new("bar.baz")))
  1226. end
  1227. def test_file_fnmatch
  1228. assert(File.fnmatch("*.*", Pathname.new("bar.baz")))
  1229. end
  1230. def test_relative_path_from_casefold
  1231. assert_separately([], <<-'end;') # do
  1232. module File::Constants
  1233. remove_const :FNM_SYSCASE
  1234. FNM_SYSCASE = FNM_CASEFOLD
  1235. end
  1236. require 'pathname'
  1237. foo = Pathname.new("fo\u{f6}")
  1238. bar = Pathname.new("b\u{e4}r".encode("ISO-8859-1"))
  1239. assert_instance_of(Pathname, foo.relative_path_from(bar))
  1240. end;
  1241. end
  1242. def test_relative_path_from_mock
  1243. assert_equal(
  1244. Pathname.new("../bar"),
  1245. Pathname.new("/foo/bar").relative_path_from(Pathname.new("/foo/baz")))
  1246. assert_equal(
  1247. Pathname.new("../bar"),
  1248. Pathname.new("/foo/bar").relative_path_from("/foo/baz"))
  1249. obj = Object.new
  1250. def obj.cleanpath() Pathname.new("/foo/baz") end
  1251. def obj.is_a?(m) m == Pathname end
  1252. assert_equal(
  1253. Pathname.new("../bar"),
  1254. Pathname.new("/foo/bar").relative_path_from(obj))
  1255. end
  1256. end