faster_pathname 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1295 @@
1
+ require 'test/unit'
2
+ require 'pathname'
3
+
4
+ require 'fileutils'
5
+ require 'tmpdir'
6
+ require 'enumerator'
7
+
8
+ require_relative './envutil_19'
9
+
10
+ class 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
+ assert_equal("abc", Pathname("a").binread)
714
+ }
715
+ end
716
+
717
+ def test_sysopen
718
+ with_tmpchdir('rubytest-pathname') {|dir|
719
+ open("a", "w") {|f| f.write "abc" }
720
+ fd = Pathname("a").sysopen
721
+ io = IO.new(fd)
722
+ begin
723
+ assert_equal("abc", io.read)
724
+ ensure
725
+ io.close
726
+ end
727
+ }
728
+ end
729
+
730
+ def test_atime
731
+ assert_kind_of(Time, Pathname(__FILE__).atime)
732
+ end
733
+
734
+ def test_ctime
735
+ assert_kind_of(Time, Pathname(__FILE__).ctime)
736
+ end
737
+
738
+ def test_mtime
739
+ assert_kind_of(Time, Pathname(__FILE__).mtime)
740
+ end
741
+
742
+ def test_chmod
743
+ with_tmpchdir('rubytest-pathname') {|dir|
744
+ open("a", "w") {|f| f.write "abc" }
745
+ path = Pathname("a")
746
+ old = path.stat.mode
747
+ path.chmod(0444)
748
+ assert_equal(0444, path.stat.mode & 0777)
749
+ path.chmod(old)
750
+ }
751
+ end
752
+
753
+ def test_lchmod
754
+ return if !has_symlink?
755
+ with_tmpchdir('rubytest-pathname') {|dir|
756
+ open("a", "w") {|f| f.write "abc" }
757
+ File.symlink("a", "l")
758
+ path = Pathname("l")
759
+ old = path.lstat.mode
760
+ begin
761
+ path.lchmod(0444)
762
+ rescue NotImplementedError
763
+ next
764
+ end
765
+ assert_equal(0444, path.lstat.mode & 0777)
766
+ path.chmod(old)
767
+ }
768
+ end
769
+
770
+ def test_chown
771
+ with_tmpchdir('rubytest-pathname') {|dir|
772
+ open("a", "w") {|f| f.write "abc" }
773
+ path = Pathname("a")
774
+ old_uid = path.stat.uid
775
+ old_gid = path.stat.gid
776
+ begin
777
+ path.chown(0, 0)
778
+ rescue Errno::EPERM
779
+ next
780
+ end
781
+ assert_equal(0, path.stat.uid)
782
+ assert_equal(0, path.stat.gid)
783
+ path.chown(old_uid, old_gid)
784
+ }
785
+ end
786
+
787
+ def test_lchown
788
+ return if !has_symlink?
789
+ with_tmpchdir('rubytest-pathname') {|dir|
790
+ open("a", "w") {|f| f.write "abc" }
791
+ File.symlink("a", "l")
792
+ path = Pathname("l")
793
+ old_uid = path.stat.uid
794
+ old_gid = path.stat.gid
795
+ begin
796
+ path.lchown(0, 0)
797
+ rescue Errno::EPERM
798
+ next
799
+ end
800
+ assert_equal(0, path.stat.uid)
801
+ assert_equal(0, path.stat.gid)
802
+ path.lchown(old_uid, old_gid)
803
+ }
804
+ end
805
+
806
+ def test_fnmatch
807
+ path = Pathname("a")
808
+ assert_equal(true, path.fnmatch("*"))
809
+ assert_equal(false, path.fnmatch("*.*"))
810
+ assert_equal(false, Pathname(".foo").fnmatch("*"))
811
+ assert_equal(true, Pathname(".foo").fnmatch("*", File::FNM_DOTMATCH))
812
+ end
813
+
814
+ def test_fnmatch?
815
+ path = Pathname("a")
816
+ assert_equal(true, path.fnmatch?("*"))
817
+ assert_equal(false, path.fnmatch?("*.*"))
818
+ end
819
+
820
+ def test_ftype
821
+ with_tmpchdir('rubytest-pathname') {|dir|
822
+ open("f", "w") {|f| f.write "abc" }
823
+ assert_equal("file", Pathname("f").ftype)
824
+ Dir.mkdir("d")
825
+ assert_equal("directory", Pathname("d").ftype)
826
+ }
827
+ end
828
+
829
+ def test_make_link
830
+ with_tmpchdir('rubytest-pathname') {|dir|
831
+ open("a", "w") {|f| f.write "abc" }
832
+ Pathname("l").make_link(Pathname("a"))
833
+ assert_equal("abc", Pathname("l").read)
834
+ }
835
+ end
836
+
837
+ def test_open
838
+ with_tmpchdir('rubytest-pathname') {|dir|
839
+ open("a", "w") {|f| f.write "abc" }
840
+ path = Pathname("a")
841
+
842
+ path.open {|f|
843
+ assert_equal("abc", f.read)
844
+ }
845
+
846
+ path.open("r") {|f|
847
+ assert_equal("abc", f.read)
848
+ }
849
+
850
+ Pathname("b").open("w", 0444) {|f| f.write "def" }
851
+ assert_equal(0444, File.stat("b").mode & 0777)
852
+ assert_equal("def", File.read("b"))
853
+
854
+ Pathname("c").open("w", 0444, {}) {|f| f.write "ghi" }
855
+ assert_equal(0444, File.stat("c").mode & 0777)
856
+ assert_equal("ghi", File.read("c"))
857
+
858
+ g = path.open
859
+ assert_equal("abc", g.read)
860
+ g.close
861
+ }
862
+ end
863
+
864
+ def test_readlink
865
+ return if !has_symlink?
866
+ with_tmpchdir('rubytest-pathname') {|dir|
867
+ open("a", "w") {|f| f.write "abc" }
868
+ File.symlink("a", "l")
869
+ assert_equal(Pathname("a"), Pathname("l").readlink)
870
+ }
871
+ end
872
+
873
+ def test_rename
874
+ with_tmpchdir('rubytest-pathname') {|dir|
875
+ open("a", "w") {|f| f.write "abc" }
876
+ Pathname("a").rename(Pathname("b"))
877
+ assert_equal("abc", File.read("b"))
878
+ }
879
+ end
880
+
881
+ def test_stat
882
+ with_tmpchdir('rubytest-pathname') {|dir|
883
+ open("a", "w") {|f| f.write "abc" }
884
+ s = Pathname("a").stat
885
+ assert_equal(3, s.size)
886
+ }
887
+ end
888
+
889
+ def test_lstat
890
+ return if !has_symlink?
891
+ with_tmpchdir('rubytest-pathname') {|dir|
892
+ open("a", "w") {|f| f.write "abc" }
893
+ File.symlink("a", "l")
894
+ s = Pathname("l").lstat
895
+ assert_equal(true, s.symlink?)
896
+ s = Pathname("l").stat
897
+ assert_equal(false, s.symlink?)
898
+ assert_equal(3, s.size)
899
+ s = Pathname("a").lstat
900
+ assert_equal(false, s.symlink?)
901
+ assert_equal(3, s.size)
902
+ }
903
+ end
904
+
905
+ def test_make_symlink
906
+ return if !has_symlink?
907
+ with_tmpchdir('rubytest-pathname') {|dir|
908
+ open("a", "w") {|f| f.write "abc" }
909
+ Pathname("l").make_symlink(Pathname("a"))
910
+ s = Pathname("l").lstat
911
+ assert_equal(true, s.symlink?)
912
+ }
913
+ end
914
+
915
+ def test_truncate
916
+ with_tmpchdir('rubytest-pathname') {|dir|
917
+ open("a", "w") {|f| f.write "abc" }
918
+ Pathname("a").truncate(2)
919
+ assert_equal("ab", File.read("a"))
920
+ }
921
+ end
922
+
923
+ def test_utime
924
+ with_tmpchdir('rubytest-pathname') {|dir|
925
+ open("a", "w") {|f| f.write "abc" }
926
+ atime = Time.utc(2000)
927
+ mtime = Time.utc(1999)
928
+ Pathname("a").utime(atime, mtime)
929
+ s = File.stat("a")
930
+ assert_equal(atime, s.atime)
931
+ assert_equal(mtime, s.mtime)
932
+ }
933
+ end
934
+
935
+ def test_basename
936
+ assert_equal(Pathname("basename"), Pathname("dirname/basename").basename)
937
+ assert_equal(Pathname("bar"), Pathname("foo/bar.x").basename(".x"))
938
+ end
939
+
940
+ def test_dirname
941
+ assert_equal(Pathname("dirname"), Pathname("dirname/basename").dirname)
942
+ end
943
+
944
+ def test_extname
945
+ assert_equal(".ext", Pathname("basename.ext").extname)
946
+ end
947
+
948
+ def test_expand_path
949
+ drv = DOSISH_DRIVE_LETTER ? Dir.pwd.sub(%r(/.*), '') : ""
950
+ assert_equal(Pathname(drv + "/a"), Pathname("/a").expand_path)
951
+ assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path("/"))
952
+ assert_equal(Pathname(drv + "/a"), Pathname("a").expand_path(Pathname("/")))
953
+ assert_equal(Pathname(drv + "/b"), Pathname("/b").expand_path(Pathname("/a")))
954
+ assert_equal(Pathname(drv + "/a/b"), Pathname("b").expand_path(Pathname("/a")))
955
+ end
956
+
957
+ def test_split
958
+ assert_equal([Pathname("dirname"), Pathname("basename")], Pathname("dirname/basename").split)
959
+ end
960
+
961
+ def test_blockdev?
962
+ with_tmpchdir('rubytest-pathname') {|dir|
963
+ open("f", "w") {|f| f.write "abc" }
964
+ assert_equal(false, Pathname("f").blockdev?)
965
+ }
966
+ end
967
+
968
+ def test_chardev?
969
+ with_tmpchdir('rubytest-pathname') {|dir|
970
+ open("f", "w") {|f| f.write "abc" }
971
+ assert_equal(false, Pathname("f").chardev?)
972
+ }
973
+ end
974
+
975
+ def test_executable?
976
+ with_tmpchdir('rubytest-pathname') {|dir|
977
+ open("f", "w") {|f| f.write "abc" }
978
+ assert_equal(false, Pathname("f").executable?)
979
+ }
980
+ end
981
+
982
+ def test_executable_real?
983
+ with_tmpchdir('rubytest-pathname') {|dir|
984
+ open("f", "w") {|f| f.write "abc" }
985
+ assert_equal(false, Pathname("f").executable_real?)
986
+ }
987
+ end
988
+
989
+ def test_exist?
990
+ with_tmpchdir('rubytest-pathname') {|dir|
991
+ open("f", "w") {|f| f.write "abc" }
992
+ assert_equal(true, Pathname("f").exist?)
993
+ }
994
+ end
995
+
996
+ def test_grpowned?
997
+ skip "Unix file owner test" if DOSISH
998
+ with_tmpchdir('rubytest-pathname') {|dir|
999
+ open("f", "w") {|f| f.write "abc" }
1000
+ File.chown(-1, Process.gid, "f")
1001
+ assert_equal(true, Pathname("f").grpowned?)
1002
+ }
1003
+ end
1004
+
1005
+ def test_directory?
1006
+ with_tmpchdir('rubytest-pathname') {|dir|
1007
+ open("f", "w") {|f| f.write "abc" }
1008
+ assert_equal(false, Pathname("f").directory?)
1009
+ Dir.mkdir("d")
1010
+ assert_equal(true, Pathname("d").directory?)
1011
+ }
1012
+ end
1013
+
1014
+ def test_file?
1015
+ with_tmpchdir('rubytest-pathname') {|dir|
1016
+ open("f", "w") {|f| f.write "abc" }
1017
+ assert_equal(true, Pathname("f").file?)
1018
+ Dir.mkdir("d")
1019
+ assert_equal(false, Pathname("d").file?)
1020
+ }
1021
+ end
1022
+
1023
+ def test_pipe?
1024
+ with_tmpchdir('rubytest-pathname') {|dir|
1025
+ open("f", "w") {|f| f.write "abc" }
1026
+ assert_equal(false, Pathname("f").pipe?)
1027
+ }
1028
+ end
1029
+
1030
+ def test_socket?
1031
+ with_tmpchdir('rubytest-pathname') {|dir|
1032
+ open("f", "w") {|f| f.write "abc" }
1033
+ assert_equal(false, Pathname("f").socket?)
1034
+ }
1035
+ end
1036
+
1037
+ def test_owned?
1038
+ with_tmpchdir('rubytest-pathname') {|dir|
1039
+ open("f", "w") {|f| f.write "abc" }
1040
+ assert_equal(true, Pathname("f").owned?)
1041
+ }
1042
+ end
1043
+
1044
+ def test_readable?
1045
+ with_tmpchdir('rubytest-pathname') {|dir|
1046
+ open("f", "w") {|f| f.write "abc" }
1047
+ assert_equal(true, Pathname("f").readable?)
1048
+ }
1049
+ end
1050
+
1051
+ def test_world_readable?
1052
+ skip "Unix file mode bit test" if DOSISH
1053
+ with_tmpchdir('rubytest-pathname') {|dir|
1054
+ open("f", "w") {|f| f.write "abc" }
1055
+ File.chmod(0400, "f")
1056
+ assert_equal(nil, Pathname("f").world_readable?)
1057
+ File.chmod(0444, "f")
1058
+ assert_equal(0444, Pathname("f").world_readable?)
1059
+ }
1060
+ end
1061
+
1062
+ def test_readable_real?
1063
+ with_tmpchdir('rubytest-pathname') {|dir|
1064
+ open("f", "w") {|f| f.write "abc" }
1065
+ assert_equal(true, Pathname("f").readable_real?)
1066
+ }
1067
+ end
1068
+
1069
+ def test_setuid?
1070
+ with_tmpchdir('rubytest-pathname') {|dir|
1071
+ open("f", "w") {|f| f.write "abc" }
1072
+ assert_equal(false, Pathname("f").setuid?)
1073
+ }
1074
+ end
1075
+
1076
+ def test_setgid?
1077
+ with_tmpchdir('rubytest-pathname') {|dir|
1078
+ open("f", "w") {|f| f.write "abc" }
1079
+ assert_equal(false, Pathname("f").setgid?)
1080
+ }
1081
+ end
1082
+
1083
+ def test_size
1084
+ with_tmpchdir('rubytest-pathname') {|dir|
1085
+ open("f", "w") {|f| f.write "abc" }
1086
+ assert_equal(3, Pathname("f").size)
1087
+ open("z", "w") {|f| }
1088
+ assert_equal(0, Pathname("z").size)
1089
+ assert_raise(Errno::ENOENT) { Pathname("not-exist").size }
1090
+ }
1091
+ end
1092
+
1093
+ def test_size?
1094
+ with_tmpchdir('rubytest-pathname') {|dir|
1095
+ open("f", "w") {|f| f.write "abc" }
1096
+ assert_equal(3, Pathname("f").size?)
1097
+ open("z", "w") {|f| }
1098
+ assert_equal(nil, Pathname("z").size?)
1099
+ assert_equal(nil, Pathname("not-exist").size?)
1100
+ }
1101
+ end
1102
+
1103
+ def test_sticky?
1104
+ skip "Unix file mode bit test" if DOSISH
1105
+ with_tmpchdir('rubytest-pathname') {|dir|
1106
+ open("f", "w") {|f| f.write "abc" }
1107
+ assert_equal(false, Pathname("f").sticky?)
1108
+ }
1109
+ end
1110
+
1111
+ def test_symlink?
1112
+ with_tmpchdir('rubytest-pathname') {|dir|
1113
+ open("f", "w") {|f| f.write "abc" }
1114
+ assert_equal(false, Pathname("f").symlink?)
1115
+ }
1116
+ end
1117
+
1118
+ def test_writable?
1119
+ with_tmpchdir('rubytest-pathname') {|dir|
1120
+ open("f", "w") {|f| f.write "abc" }
1121
+ assert_equal(true, Pathname("f").writable?)
1122
+ }
1123
+ end
1124
+
1125
+ def test_world_writable?
1126
+ skip "Unix file mode bit test" if DOSISH
1127
+ with_tmpchdir('rubytest-pathname') {|dir|
1128
+ open("f", "w") {|f| f.write "abc" }
1129
+ File.chmod(0600, "f")
1130
+ assert_equal(nil, Pathname("f").world_writable?)
1131
+ File.chmod(0666, "f")
1132
+ assert_equal(0666, Pathname("f").world_writable?)
1133
+ }
1134
+ end
1135
+
1136
+ def test_writable_real?
1137
+ with_tmpchdir('rubytest-pathname') {|dir|
1138
+ open("f", "w") {|f| f.write "abc" }
1139
+ assert_equal(true, Pathname("f").writable?)
1140
+ }
1141
+ end
1142
+
1143
+ def test_zero?
1144
+ with_tmpchdir('rubytest-pathname') {|dir|
1145
+ open("f", "w") {|f| f.write "abc" }
1146
+ assert_equal(false, Pathname("f").zero?)
1147
+ open("z", "w") {|f| }
1148
+ assert_equal(true, Pathname("z").zero?)
1149
+ assert_equal(false, Pathname("not-exist").zero?)
1150
+ }
1151
+ end
1152
+
1153
+ def test_s_glob
1154
+ with_tmpchdir('rubytest-pathname') {|dir|
1155
+ open("f", "w") {|f| f.write "abc" }
1156
+ Dir.mkdir("d")
1157
+ assert_equal([Pathname("d"), Pathname("f")], Pathname.glob("*").sort)
1158
+ a = []
1159
+ Pathname.glob("*") {|path| a << path }
1160
+ a.sort!
1161
+ assert_equal([Pathname("d"), Pathname("f")], a)
1162
+ }
1163
+ end
1164
+
1165
+ def test_s_getwd
1166
+ wd = Pathname.getwd
1167
+ assert_kind_of(Pathname, wd)
1168
+ end
1169
+
1170
+ def test_s_pwd
1171
+ wd = Pathname.pwd
1172
+ assert_kind_of(Pathname, wd)
1173
+ end
1174
+
1175
+ def test_entries
1176
+ with_tmpchdir('rubytest-pathname') {|dir|
1177
+ open("a", "w") {}
1178
+ open("b", "w") {}
1179
+ assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], Pathname(".").entries.sort)
1180
+ }
1181
+ end
1182
+
1183
+ def test_each_entry
1184
+ with_tmpchdir('rubytest-pathname') {|dir|
1185
+ open("a", "w") {}
1186
+ open("b", "w") {}
1187
+ a = []
1188
+ Pathname(".").each_entry {|v| a << v }
1189
+ assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], a.sort)
1190
+ }
1191
+ end
1192
+
1193
+ def test_mkdir
1194
+ with_tmpchdir('rubytest-pathname') {|dir|
1195
+ Pathname("d").mkdir
1196
+ assert(File.directory?("d"))
1197
+ Pathname("e").mkdir(0770)
1198
+ assert(File.directory?("e"))
1199
+ }
1200
+ end
1201
+
1202
+ def test_rmdir
1203
+ with_tmpchdir('rubytest-pathname') {|dir|
1204
+ Pathname("d").mkdir
1205
+ assert(File.directory?("d"))
1206
+ Pathname("d").rmdir
1207
+ assert(!File.exists?("d"))
1208
+ }
1209
+ end
1210
+
1211
+ def test_opendir
1212
+ with_tmpchdir('rubytest-pathname') {|dir|
1213
+ open("a", "w") {}
1214
+ open("b", "w") {}
1215
+ a = []
1216
+ Pathname(".").opendir {|d|
1217
+ d.each {|e| a << e }
1218
+ }
1219
+ assert_equal([".", "..", "a", "b"], a.sort)
1220
+ }
1221
+ end
1222
+
1223
+ def test_find
1224
+ with_tmpchdir('rubytest-pathname') {|dir|
1225
+ open("a", "w") {}
1226
+ open("b", "w") {}
1227
+ Dir.mkdir("d")
1228
+ open("d/x", "w") {}
1229
+ open("d/y", "w") {}
1230
+ a = []; Pathname(".").find {|v| a << v }; a.sort!
1231
+ assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
1232
+ a = []; Pathname("d").find {|v| a << v }; a.sort!
1233
+ assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
1234
+ }
1235
+ end
1236
+
1237
+ def test_mkpath
1238
+ with_tmpchdir('rubytest-pathname') {|dir|
1239
+ Pathname("a/b/c/d").mkpath
1240
+ assert(File.directory?("a/b/c/d"))
1241
+ }
1242
+ end
1243
+
1244
+ def test_rmtree
1245
+ with_tmpchdir('rubytest-pathname') {|dir|
1246
+ Pathname("a/b/c/d").mkpath
1247
+ assert(File.exist?("a/b/c/d"))
1248
+ Pathname("a").rmtree
1249
+ assert(!File.exist?("a"))
1250
+ }
1251
+ end
1252
+
1253
+ def test_unlink
1254
+ with_tmpchdir('rubytest-pathname') {|dir|
1255
+ open("f", "w") {|f| f.write "abc" }
1256
+ Pathname("f").unlink
1257
+ assert(!File.exist?("f"))
1258
+ Dir.mkdir("d")
1259
+ Pathname("d").unlink
1260
+ assert(!File.exist?("d"))
1261
+ }
1262
+ end
1263
+
1264
+ def test_matchop
1265
+ assert_raise(NoMethodError) { Pathname("a") =~ /a/ }
1266
+ end
1267
+
1268
+ def test_file_basename
1269
+ assert_equal("bar", File.basename(Pathname.new("foo/bar")))
1270
+ end
1271
+
1272
+ def test_file_dirname
1273
+ assert_equal("foo", File.dirname(Pathname.new("foo/bar")))
1274
+ end
1275
+
1276
+ def test_file_split
1277
+ assert_equal(["foo", "bar"], File.split(Pathname.new("foo/bar")))
1278
+ end
1279
+
1280
+ def test_file_extname
1281
+ assert_equal(".baz", File.extname(Pathname.new("bar.baz")))
1282
+ end
1283
+
1284
+ def test_file_fnmatch
1285
+ assert(File.fnmatch("*.*", Pathname.new("bar.baz")))
1286
+ end
1287
+
1288
+ def test_file_join
1289
+ assert_equal("foo/bar", File.join(Pathname.new("foo"), Pathname.new("bar")))
1290
+ lambda {
1291
+ $SAFE = 1
1292
+ assert_equal("foo/bar", File.join(Pathname.new("foo"), Pathname.new("bar").taint))
1293
+ }.call
1294
+ end
1295
+ end