File: test_pathname.rb

package info (click to toggle)
ruby-pathname2 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 340 kB
  • sloc: ruby: 2,033; makefile: 2
file content (486 lines) | stat: -rw-r--r-- 14,962 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
##############################################################################
# test_pathname.rb
#
# Test suite for the pathname library on unixy platforms. This test suite
# should be run via the test rake task.
##############################################################################
require 'pathname2'
require 'fileutils'
require 'rbconfig'
require 'test-unit'
include RbConfig

class MyPathname < Pathname; end

class TC_Pathname < Test::Unit::TestCase
  def self.startup
    Dir.chdir(File.expand_path(File.dirname(__FILE__)))
    @@pwd = Dir.pwd
  end

  def setup
    @abs_path = Pathname.new('/usr/local/bin')
    @rel_path = Pathname.new('usr/local/bin')
    @trl_path = Pathname.new('/usr/local/bin/')
    @mul_path = Pathname.new('/usr/local/lib/local/lib')
    @rul_path = Pathname.new('usr/local/lib/local/lib')
    @url_path = Pathname.new('file:///foo%20bar/baz')
    @cur_path = Pathname.new(@@pwd)

    @abs_array = []
    @rel_array = []

    @mypath = MyPathname.new('/usr/bin')

    @test_file  = 'realpath_test.txt'
    @link_file  = 'realpath_symlink.txt'
    @link_file2 = 'realpath_symlink2.txt'
  end

  # Convenience method to verify that the receiver was not modified
  # except perhaps slashes
  def assert_non_destructive
    assert_equal('/usr/local/bin', @abs_path)
    assert_equal('usr/local/bin', @rel_path)
  end

  # Convenience method for test_plus
  def assert_pathname_plus(a, b, c)
    a = Pathname.new(a)
    b = Pathname.new(b)
    c = Pathname.new(c)
    assert_equal(a, b + c)
  end

  # Convenience method for test_spaceship operator
  def assert_pathname_cmp(int, s1, s2)
    p1 = Pathname.new(s1)
    p2 = Pathname.new(s2)
    result = p1 <=> p2
    assert_equal(int, result)
  end

  # Convenience method for test_relative_path_from
  def assert_relpath(result, dest, base)
    assert_equal(result, Pathname.new(dest).relative_path_from(base))
  end

  # Convenience method for test_relative_path_from_expected_errors
  def assert_relpath_err(to, from)
    assert_raise(ArgumentError) { Pathname.new(to).relative_path_from(from) }
  end

  test "url_path returns expected result" do
    assert_equal('/foo bar/baz', @url_path)
  end

  test "realpath basic functionality" do
    FileUtils.touch(@test_file) && File.symlink(@test_file, @link_file)
    assert_respond_to(@abs_path, :realpath)
    assert_equal(@@pwd, Pathname.new('.').realpath)
    assert_kind_of(Pathname, Pathname.new(@link_file).realpath)
  end

  test "realpath returns expected result for simple symlink" do
    FileUtils.touch(@test_file) && File.symlink(@test_file, @link_file)
    assert_true(Pathname.new(@link_file) != Pathname.new(@link_file).realpath)
    assert_raises(Errno::ENOENT){ Pathname.new('../bogus').realpath }
  end

  test "realpath returns expected result for nested symlink" do
    FileUtils.touch(@test_file) && File.symlink(@test_file, @link_file) && File.symlink(@link_file, @link_file2)
    assert_true(Pathname.new(@link_file) != Pathname.new(@link_file2).realpath)
    assert_equal(Pathname.new(@link_file).realpath, Pathname.new(@link_file2).realpath)
  end

  # These tests taken directly from Tanaka's pathname.rb. The one failure
  # (commented out) is due to the fact that Tanaka's cleanpath method returns
  # the cleanpath for '../a' as '../a' (i.e. it does nothing) whereas mine
  # converts '../a' into just 'a'.  Which is correct? I vote mine, because
  # I don't see how you can get 'more relative' from a relative path not
  # already in the pathname.
  #
  def test_relative_path_from
    assert_relpath('../a', 'a', 'b')
    assert_relpath('../a', 'a', 'b/')
    assert_relpath('../a', 'a/', 'b')
    assert_relpath('../a', 'a/', 'b/')
    assert_relpath('../a', '/a', '/b')
    assert_relpath('../a', '/a', '/b/')
    assert_relpath('../a', '/a/', '/b')
    assert_relpath('../a', '/a/', '/b/')

    assert_relpath('../b', 'a/b', 'a/c')
    assert_relpath('../a', '../a', '../b')

    assert_relpath('a', 'a', '.')
    assert_relpath('..', '.', 'a')

    assert_relpath('.', '.', '.')
    assert_relpath('.', '..', '..')
    assert_relpath('..', '..', '.')

    assert_relpath('c/d', '/a/b/c/d', '/a/b')
    assert_relpath('../..', '/a/b', '/a/b/c/d')
    assert_relpath('../../../../e', '/e', '/a/b/c/d')
    assert_relpath('../b/c', 'a/b/c', 'a/d')

    assert_relpath('../a', '/../a', '/b')
    #assert_relpath('../../a', '../a', 'b') # fails
    assert_relpath('.', '/a/../../b', '/b')
    assert_relpath('..', 'a/..', 'a')
    assert_relpath('.', 'a/../b', 'b')

    assert_relpath('a', 'a', 'b/..')
    assert_relpath('b/c', 'b/c', 'b/..')

    assert_relpath_err('/', '.')
    assert_relpath_err('.', '/')
    assert_relpath_err('a', '..')
    assert_relpath_err('.', '..')
  end

  def test_parent
    assert_respond_to(@abs_path, :parent)
    assert_equal('/usr/local', @abs_path.parent)
    assert_equal('usr/local', @rel_path.parent)
    assert_equal('/', Pathname.new('/').parent)
  end

  def test_pstrip
    assert_respond_to(@trl_path, :pstrip)
    assert_nothing_raised{ @trl_path.pstrip }
    assert_equal('/usr/local/bin', @trl_path.pstrip)
    assert_equal('/usr/local/bin/', @trl_path)
  end

  def test_pstrip_bang
    assert_respond_to(@trl_path, :pstrip!)
    assert_nothing_raised{ @trl_path.pstrip! }
    assert_equal('/usr/local/bin', @trl_path.pstrip!)
    assert_equal('/usr/local/bin', @trl_path)
  end

  def test_ascend
    assert_respond_to(@abs_path, :ascend)
    assert_nothing_raised{ @abs_path.ascend{} }

    @abs_path.ascend{ |path| @abs_array.push(path) }
    @rel_path.ascend{ |path| @rel_array.push(path) }

    assert_equal('/usr/local/bin', @abs_array[0])
    assert_equal('/usr/local', @abs_array[1])
    assert_equal('/usr', @abs_array[2])
    assert_equal('/', @abs_array[3])
    assert_equal(4, @abs_array.length)

    assert_equal('usr/local/bin', @rel_array[0])
    assert_equal('usr/local', @rel_array[1])
    assert_equal('usr', @rel_array[2])
    assert_equal(3, @rel_array.length)

    assert_non_destructive
  end

  def test_descend
    assert_respond_to(@abs_path, :descend)
    assert_nothing_raised{ @abs_path.descend{} }

    @abs_path.descend{ |path| @abs_array.push(path) }
    @rel_path.descend{ |path| @rel_array.push(path) }

    assert_equal('/', @abs_array[0])
    assert_equal('/usr', @abs_array[1])
    assert_equal('/usr/local', @abs_array[2])
    assert_equal('/usr/local/bin', @abs_array[3])
    assert_equal(4, @abs_array.length)

    assert_equal('usr', @rel_array[0])
    assert_equal('usr/local', @rel_array[1])
    assert_equal('usr/local/bin', @rel_array[2])
    assert_equal(3, @rel_array.length)

    assert_non_destructive
  end

  def test_children_with_directory
    assert_respond_to(@cur_path, :children)
    assert_nothing_raised{ @cur_path.children }
    assert_kind_of(Array, @cur_path.children)

    children = @cur_path.children.sort.reject{ |f| f.include?('git') || f.include?('.swp') }
    assert_equal(
       [
          Dir.pwd + '/test_pathname.rb',
          Dir.pwd + '/test_version.rb',
          Dir.pwd + '/windows'
       ],
       children.sort
    )
  end

  def test_children_without_directory
    assert_nothing_raised{ @cur_path.children(false) }

    children = @cur_path.children(false).reject{ |f| f.include?('git') || f.include?('.swp') }
    assert_equal(['test_pathname.rb', 'test_version.rb', 'windows'], children.sort)
  end

  def test_unc
    assert_raises(NotImplementedError){ @abs_path.unc? }
  end

  def test_enumerable
    assert_respond_to(@abs_path, :each)
  end

  def test_root
    assert_respond_to(@abs_path, :root)
    assert_nothing_raised{ @abs_path.root }
    assert_nothing_raised{ @rel_path.root }

    assert_equal('/', @abs_path.root)
    assert_equal('.', @rel_path.root)

    assert_non_destructive
  end

  def test_root?
    assert_respond_to(@abs_path, :root?)
    assert_nothing_raised{ @abs_path.root? }
    assert_nothing_raised{ @rel_path.root? }

    path1 = Pathname.new('/')
    path2 = Pathname.new('a')
    assert_equal(true, path1.root?)
    assert_equal(false, path2.root?)

    assert_non_destructive
  end

  def test_absolute
    assert_respond_to(@abs_path, :absolute?)
    assert_nothing_raised{ @abs_path.absolute? }
    assert_nothing_raised{ @rel_path.absolute? }

    assert_equal(true, @abs_path.absolute?)
    assert_equal(false, @rel_path.absolute?)

    assert_equal(true, Pathname.new('/usr/bin/ruby').absolute?)
    assert_equal(false, Pathname.new('foo').absolute?)
    assert_equal(false, Pathname.new('foo/bar').absolute?)
    assert_equal(false, Pathname.new('../foo/bar').absolute?)

    assert_non_destructive
  end

  def test_relative
    assert_respond_to(@abs_path, :relative?)
    assert_nothing_raised{ @abs_path.relative? }
    assert_nothing_raised{ @rel_path.relative? }

    assert_equal(false, @abs_path.relative?)
    assert_equal(true, @rel_path.relative?)

    assert_equal(false, Pathname.new('/usr/bin/ruby').relative?)
    assert_equal(true, Pathname.new('foo').relative?)
    assert_equal(true, Pathname.new('foo/bar').relative?)
    assert_equal(true, Pathname.new('../foo/bar').relative?)

    assert_non_destructive
  end

  def test_to_a
    assert_respond_to(@abs_path, :to_a)
    assert_nothing_raised{ @abs_path.to_a }
    assert_nothing_raised{ @rel_path.to_a }
    assert_kind_of(Array, @abs_path.to_a)
    assert_equal(%w/usr local bin/, @abs_path.to_a)

    assert_non_destructive
  end

  def test_spaceship_operator
    assert_respond_to(@abs_path, :<=>)

    assert_pathname_cmp( 0, '/foo/bar', '/foo/bar')
    assert_pathname_cmp(-1, '/foo/bar', '/foo/zap')
    assert_pathname_cmp( 1, '/foo/zap', '/foo/bar')
    assert_pathname_cmp(-1, 'foo', 'foo/')
    assert_pathname_cmp(-1, 'foo/', 'foo/bar')
  end

  def test_plus_operator
    assert_respond_to(@abs_path, :+)

    # Standard stuff
    assert_pathname_plus('/foo/bar', '/foo', 'bar')
    assert_pathname_plus('foo/bar', 'foo', 'bar')
    assert_pathname_plus('foo', 'foo', '.')
    assert_pathname_plus('foo', '.', 'foo')
    assert_pathname_plus('/foo', 'bar', '/foo')
    assert_pathname_plus('foo', 'foo/bar', '..')
    assert_pathname_plus('/foo', '/', '../foo')
    assert_pathname_plus('foo/zap', 'foo/bar', '../zap')
    assert_pathname_plus('.', 'foo', '..')
    assert_pathname_plus('foo', '..', 'foo')     # Auto clean
    assert_pathname_plus('foo', '..', '../foo')  # Auto clean

    # Edge cases
    assert_pathname_plus('.', '.', '.')
    assert_pathname_plus('/', '/', '..')
    assert_pathname_plus('.', '..',  '..')
    assert_pathname_plus('.', 'foo', '..')

    # Alias
    assert_equal('/foo/bar', Pathname.new('/foo') / Pathname.new('bar'))
  end

  # Any tests marked with '***' mean that this behavior is different than
  # the current implementation.  It also means I disagree with the current
  # implementation.
  def test_clean
    # Standard stuff
    assert_equal('/a/b/c', Pathname.new('/a/b/c').cleanpath)
    assert_equal('b/c', Pathname.new('./b/c').cleanpath)
    assert_equal('a', Pathname.new('a/.').cleanpath)         # ***
    assert_equal('a/c', Pathname.new('a/./c').cleanpath)
    assert_equal('a/b', Pathname.new('a/b/.').cleanpath)     # ***
    assert_equal('.', Pathname.new('a/../.').cleanpath)      # ***
    assert_equal('/a', Pathname.new('/a/b/..').cleanpath)
    assert_equal('/b', Pathname.new('/a/../b').cleanpath)
    assert_equal('d', Pathname.new('a/../../d').cleanpath)   # ***

    # Edge cases
    assert_equal('', Pathname.new('').cleanpath)
    assert_equal('.', Pathname.new('.').cleanpath)
    assert_equal('..', Pathname.new('..').cleanpath)
    assert_equal('/', Pathname.new('/').cleanpath)
    assert_equal('/', Pathname.new('//').cleanpath)

    assert_non_destructive
  end

  def test_dirname_basic
    assert_respond_to(@abs_path, :dirname)
    assert_nothing_raised{ @abs_path.dirname }
    assert_kind_of(String, @abs_path.dirname)
  end

  def test_dirname
    assert_equal('/usr/local', @abs_path.dirname)
    assert_equal('/usr/local/bin', @abs_path.dirname(0))
    assert_equal('/usr/local', @abs_path.dirname(1))
    assert_equal('/usr', @abs_path.dirname(2))
    assert_equal('/', @abs_path.dirname(3))
    assert_equal('/', @abs_path.dirname(9))
  end

  def test_dirname_expected_errors
    assert_raise(ArgumentError){ @abs_path.dirname(-1) }
  end

  def test_facade_io
    assert_respond_to(@abs_path, :foreach)
    assert_respond_to(@abs_path, :read)
    assert_respond_to(@abs_path, :readlines)
    assert_respond_to(@abs_path, :sysopen)
  end

  def test_facade_file
    File.methods(false).each{ |method|
      assert_respond_to(@abs_path, method.to_sym)
    }
  end

  def test_facade_dir
    Dir.methods(false).each{ |method|
      assert_respond_to(@abs_path, method.to_sym)
    }
  end

  def test_facade_fileutils
    methods = FileUtils.public_instance_methods
    methods -= File.methods(false)
    methods -= Dir.methods(false)
    methods.delete_if{ |m| m.to_s =~ /stream/ }
    methods.delete(:identical?)
    methods.delete(:sh)
    methods.delete(:ruby)
    methods.delete(:safe_ln)
    methods.delete(:split_all)

    methods.each{ |method|
      assert_respond_to(@abs_path, method.to_sym)
    }
  end

  def test_facade_find
    assert_respond_to(@abs_path, :find)
    assert_nothing_raised{ @abs_path.find{} }

    Pathname.new(Dir.pwd).find{ |f|
      Find.prune if f.match('CVS')
      assert_kind_of(Pathname, f)
    }
  end

  # Ensures that subclasses return the subclass as the class, not a hard
  # coded Pathname.
  #
  def test_subclasses
    assert_kind_of(MyPathname, @mypath)
    assert_kind_of(MyPathname, @mypath + MyPathname.new('foo'))
    assert_kind_of(MyPathname, @mypath.realpath)
    assert_kind_of(MyPathname, @mypath.children.first)
  end

  # Test to ensure that the pn{ } shortcut works
  #
  def test_kernel_method
    assert_respond_to(Kernel, :pn)
    assert_nothing_raised{ pn{'/foo'} }
    assert_kind_of(Pathname, pn{'/foo'})
    assert_equal('/foo', pn{'/foo'})
  end

  def test_pwd_singleton_method
    assert_respond_to(Pathname, :pwd)
    assert_kind_of(String, Pathname.pwd)
    assert_equal(@@pwd, Pathname.pwd)
  end

  test "String#to_path instance method is implemented" do
    string = "/usr/local/bin"
    assert_respond_to(string, :to_path)
    assert_nothing_raised{ string.to_path }
    assert_kind_of(Pathname, string.to_path)
  end

  def teardown
    @abs_path = nil
    @rel_path = nil
    @trl_path = nil
    @mul_path = nil
    @rul_path = nil
    @cur_path = nil
    @abs_path = nil
    @rel_path = nil
    @cur_path = nil
    @mypath = nil
    @abs_array.clear
    @rel_array.clear

    File.delete(@link_file2) if File.exist?(@link_file2)
    File.delete(@link_file) if File.exist?(@link_file)
    File.delete(@test_file) if File.exist?(@test_file)

    @link_file2 = nil
    @link_file  = nil
    @test_file  = nil
  end

  def self.shutdown
    @@pwd = nil
  end
end