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