PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/pathname/test_pathname.rb

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