pathname2 1.6.3 → 1.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/pathname2.gemspec CHANGED
@@ -1,37 +1,37 @@
1
1
  require 'rubygems'
2
2
 
3
- Gem::Specification.new do |gem|
4
- gem.name = 'pathname2'
5
- gem.version = '1.6.3'
6
- gem.author = 'Daniel J. Berger'
7
- gem.license = 'Artistic 2.0'
8
- gem.email = 'djberg96@gmail.com'
9
- gem.homepage = 'http://www.rubyforge.org/projects/shards'
10
- gem.summary = 'An alternate implementation of the Pathname class'
11
- gem.has_rdoc = true
12
- gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') }
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'pathname2'
5
+ spec.version = '1.6.4'
6
+ spec.author = 'Daniel J. Berger'
7
+ spec.license = 'Artistic 2.0'
8
+ spec.email = 'djberg96@gmail.com'
9
+ spec.homepage = 'http://www.rubyforge.org/projects/shards'
10
+ spec.summary = 'An alternate implementation of the Pathname class'
11
+ spec.has_rdoc = true
12
+ spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
13
13
 
14
- gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
15
- gem.rubyforge_project = 'shards'
14
+ spec.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
15
+ spec.rubyforge_project = 'shards'
16
16
 
17
- gem.add_dependency('facade', '>= 1.0.4')
18
- gem.add_development_dependency('test-unit', '>= 2.0.3')
19
-
20
- if Config::CONFIG['host_os'] =~ /mswin|win32|dos|cygwin|mingw/i
21
- gem.test_file = 'test/test_pathname_windows.rb'
22
- gem.add_dependency('windows-pr', '>= 0.8.6')
23
- gem.platform = Gem::Platform::CURRENT
24
- else
25
- gem.test_file = 'test/test_pathname.rb'
26
- end
27
-
28
- gem.description = <<-EOF
29
- The pathname2 library provides an implementation of the Pathname
30
- class different from the one that ships as part of the Ruby standard
31
- library. It is a subclass of String, though several methods have been
32
- overridden to better fit a path context. In addition, it supports file
33
- URL's as paths, provides additional methods for Windows paths, and
34
- handles UNC paths on Windows properly. See the README file for more
35
- details.
36
- EOF
17
+ spec.add_dependency('facade', '>= 1.0.4')
18
+ spec.add_development_dependency('test-unit', '>= 2.1.2')
19
+
20
+ if File::ALT_SEPARATOR
21
+ spec.test_file = 'test/test_pathname_windows.rb'
22
+ spec.add_dependency('windows-pr', '>= 1.1.3')
23
+ spec.platform = Gem::Platform::CURRENT
24
+ else
25
+ spec.test_file = 'test/test_pathname.rb'
26
+ end
27
+
28
+ spec.description = <<-EOF
29
+ The pathname2 library provides an implementation of the Pathname
30
+ class different from the one that ships as part of the Ruby standard
31
+ library. It is a subclass of String, though several methods have been
32
+ overridden to better fit a path context. In addition, it supports file
33
+ URL's as paths, provides additional methods for Windows paths, and
34
+ handles UNC paths on Windows properly. See the README file for more
35
+ details.
36
+ EOF
37
37
  end
@@ -1,484 +1,480 @@
1
1
  ##############################################################################
2
- # tc_pathname.rb
2
+ # test_pathname.rb
3
3
  #
4
4
  # Test suite for the pathname package (Unix). This test suite should be run
5
5
  # via the Rake tasks, i.e. 'rake test_pr' to test the pure Ruby version, or
6
6
  # 'rake test_c' to test the C version.
7
7
  ##############################################################################
8
- require 'rubygems'
9
- gem 'test-unit'
10
-
11
- require 'test/unit'
12
8
  require 'pathname2'
13
9
  require 'rbconfig'
14
10
  include Config
15
11
 
12
+ require 'rubygems'
13
+ gem 'test-unit'
14
+ require 'test/unit'
15
+
16
16
  class MyPathname < Pathname; end
17
17
 
18
18
  class TC_Pathname < Test::Unit::TestCase
19
- def self.startup
20
- Dir.chdir(File.expand_path(File.dirname(__FILE__)))
21
- @@pwd = Dir.pwd
22
- end
23
-
24
- def setup
25
- @abs_path = Pathname.new('/usr/local/bin')
26
- @rel_path = Pathname.new('usr/local/bin')
27
- @trl_path = Pathname.new('/usr/local/bin/')
28
- @mul_path = Pathname.new('/usr/local/lib/local/lib')
29
- @rul_path = Pathname.new('usr/local/lib/local/lib')
30
- @url_path = Pathname.new('file:///foo%20bar/baz')
31
- @cur_path = Pathname.new(@@pwd)
32
-
33
- @abs_array = []
34
- @rel_array = []
35
-
36
- @mypath = MyPathname.new('/usr/bin')
37
- end
38
-
39
- # Convenience method to verify that the receiver was not modified
40
- # except perhaps slashes
41
- def assert_non_destructive
42
- assert_equal('/usr/local/bin', @abs_path)
43
- assert_equal('usr/local/bin', @rel_path)
44
- end
45
-
46
- # Convenience method for test_plus
47
- def assert_pathname_plus(a, b, c)
48
- a = Pathname.new(a)
49
- b = Pathname.new(b)
50
- c = Pathname.new(c)
51
- assert_equal(a, b + c)
52
- end
53
-
54
- # Convenience method for test_spaceship operator
55
- def assert_pathname_cmp(int, s1, s2)
56
- p1 = Pathname.new(s1)
57
- p2 = Pathname.new(s2)
58
- result = p1 <=> p2
59
- assert_equal(int, result)
60
- end
61
-
62
- # Convenience method for test_relative_path_from
63
- def assert_relpath(result, dest, base)
64
- assert_equal(result, Pathname.new(dest).relative_path_from(base))
65
- end
66
-
67
- # Convenience method for test_relative_path_from_expected_errors
68
- def assert_relpath_err(to, from)
69
- assert_raise(ArgumentError) {
70
- Pathname.new(to).relative_path_from(from)
71
- }
72
- end
73
-
74
- def test_version
75
- assert_equal('1.6.3', Pathname::VERSION)
76
- end
77
-
78
- def test_file_url_path
79
- assert_equal('/foo bar/baz', @url_path)
80
- end
81
-
82
- def test_realpath
83
- assert_respond_to(@abs_path, :realpath)
84
- assert_equal(@@pwd, Pathname.new('.').realpath)
85
- assert_kind_of(Pathname, Pathname.new('/dev/stdin').realpath)
86
- assert(Pathname.new('/dev/stdin') != Pathname.new('/dev/stdin').realpath)
87
- if CONFIG['host_os'] =~ /bsd|darwin|mac/i
88
- assert_raises(Errno::ENOENT){ Pathname.new('../blahblah/bogus').realpath }
89
- else
90
- assert_raises(Errno::ENOENT){ Pathname.new('../bogus').realpath }
91
- end
92
- end
93
-
94
- def test_realpath_platform
95
- case CONFIG['host_os']
96
- when /linux/i
97
- path1 = '/dev/stdin'
98
- assert_equal('/dev/pts/0', Pathname.new(path1).realpath)
99
- when /sunos|solaris/i
100
- path1 = '/dev/null'
101
- path2 = '/dev/stdin'
102
- path3 = '/dev/fd0' # Multiple symlinks
103
-
104
- assert_equal('/devices/pseudo/mm@0:null', Pathname.new(path1).realpath)
105
- assert_equal('/dev/fd/0', Pathname.new(path2).realpath)
106
- assert_equal('/devices/pci@1f,0/isa@7/dma@0,0/floppy@0,3f0:c',
107
- Pathname.new(path3).realpath
108
- )
109
- end
110
- end
111
-
112
- # These tests taken directly from Tanaka's pathname.rb. The one failure
113
- # (commented out) is due to the fact that Tanaka's cleanpath method returns
114
- # the cleanpath for '../a' as '../a' (i.e. it does nothing) whereas mine
115
- # converts '../a' into just 'a'. Which is correct? I vote mine, because
116
- # I don't see how you can get 'more relative' from a relative path not
117
- # already in the pathname.
118
- #
119
- def test_relative_path_from
120
- assert_relpath('../a', 'a', 'b')
121
- assert_relpath('../a', 'a', 'b/')
122
- assert_relpath('../a', 'a/', 'b')
123
- assert_relpath('../a', 'a/', 'b/')
124
- assert_relpath('../a', '/a', '/b')
125
- assert_relpath('../a', '/a', '/b/')
126
- assert_relpath('../a', '/a/', '/b')
127
- assert_relpath('../a', '/a/', '/b/')
128
-
129
- assert_relpath('../b', 'a/b', 'a/c')
130
- assert_relpath('../a', '../a', '../b')
131
-
132
- assert_relpath('a', 'a', '.')
133
- assert_relpath('..', '.', 'a')
134
-
135
- assert_relpath('.', '.', '.')
136
- assert_relpath('.', '..', '..')
137
- assert_relpath('..', '..', '.')
138
-
139
- assert_relpath('c/d', '/a/b/c/d', '/a/b')
140
- assert_relpath('../..', '/a/b', '/a/b/c/d')
141
- assert_relpath('../../../../e', '/e', '/a/b/c/d')
142
- assert_relpath('../b/c', 'a/b/c', 'a/d')
143
-
144
- assert_relpath('../a', '/../a', '/b')
145
- #assert_relpath('../../a', '../a', 'b') # fails
146
- assert_relpath('.', '/a/../../b', '/b')
147
- assert_relpath('..', 'a/..', 'a')
148
- assert_relpath('.', 'a/../b', 'b')
149
-
150
- assert_relpath('a', 'a', 'b/..')
151
- assert_relpath('b/c', 'b/c', 'b/..')
152
-
153
- assert_relpath_err('/', '.')
154
- assert_relpath_err('.', '/')
155
- assert_relpath_err('a', '..')
156
- assert_relpath_err('.', '..')
157
- end
158
-
159
- def test_parent
160
- assert_respond_to(@abs_path, :parent)
161
- assert_equal('/usr/local', @abs_path.parent)
162
- assert_equal('usr/local', @rel_path.parent)
163
- assert_equal('/', Pathname.new('/').parent)
164
- end
165
-
166
- def test_pstrip
167
- assert_respond_to(@trl_path, :pstrip)
168
- assert_nothing_raised{ @trl_path.pstrip }
169
- assert_equal('/usr/local/bin', @trl_path.pstrip)
170
- assert_equal('/usr/local/bin/', @trl_path)
171
- end
172
-
173
- def test_pstrip_bang
174
- assert_respond_to(@trl_path, :pstrip!)
175
- assert_nothing_raised{ @trl_path.pstrip! }
176
- assert_equal('/usr/local/bin', @trl_path.pstrip!)
177
- assert_equal('/usr/local/bin', @trl_path)
178
- end
179
-
180
- def test_ascend
181
- assert_respond_to(@abs_path, :ascend)
182
- assert_nothing_raised{ @abs_path.ascend{} }
183
-
184
- @abs_path.ascend{ |path| @abs_array.push(path) }
185
- @rel_path.ascend{ |path| @rel_array.push(path) }
186
-
187
- assert_equal('/usr/local/bin', @abs_array[0])
188
- assert_equal('/usr/local', @abs_array[1])
189
- assert_equal('/usr', @abs_array[2])
190
- assert_equal('/', @abs_array[3])
191
- assert_equal(4, @abs_array.length)
192
-
193
- assert_equal('usr/local/bin', @rel_array[0])
194
- assert_equal('usr/local', @rel_array[1])
195
- assert_equal('usr', @rel_array[2])
196
- assert_equal(3, @rel_array.length)
197
-
198
- assert_non_destructive
199
- end
200
-
201
- def test_descend
202
- assert_respond_to(@abs_path, :descend)
203
- assert_nothing_raised{ @abs_path.descend{} }
204
-
205
- @abs_path.descend{ |path| @abs_array.push(path) }
206
- @rel_path.descend{ |path| @rel_array.push(path) }
207
-
208
- assert_equal('/', @abs_array[0])
209
- assert_equal('/usr', @abs_array[1])
210
- assert_equal('/usr/local', @abs_array[2])
211
- assert_equal('/usr/local/bin', @abs_array[3])
212
- assert_equal(4, @abs_array.length)
213
-
214
- assert_equal('usr', @rel_array[0])
215
- assert_equal('usr/local', @rel_array[1])
216
- assert_equal('usr/local/bin', @rel_array[2])
217
- assert_equal(3, @rel_array.length)
218
-
219
- assert_non_destructive
220
- end
221
-
222
- def test_children_with_directory
223
- assert_respond_to(@cur_path, :children)
224
- assert_nothing_raised{ @cur_path.children }
225
- assert_kind_of(Array, @cur_path.children)
226
-
227
- children = @cur_path.children.sort.reject{ |f| f.include?('CVS') }
228
- assert_equal(
229
- [
230
- Dir.pwd + '/test_pathname.rb',
231
- Dir.pwd + '/test_pathname_windows.rb'
232
- ],
233
- children.sort
234
- )
235
- end
236
-
237
- def test_children_without_directory
238
- assert_nothing_raised{ @cur_path.children(false) }
239
-
240
- children = @cur_path.children(false).reject{ |f| f.include?('CVS') }
241
- assert_equal(['test_pathname.rb', 'test_pathname_windows.rb'], children.sort)
242
- end
243
-
244
- def test_unc
245
- assert_raises(NotImplementedError){ @abs_path.unc? }
246
- end
247
-
248
- def test_enumerable
249
- assert_respond_to(@abs_path, :each)
250
- end
251
-
252
- def test_root
253
- assert_respond_to(@abs_path, :root)
254
- assert_nothing_raised{ @abs_path.root }
255
- assert_nothing_raised{ @rel_path.root }
256
-
257
- assert_equal('/', @abs_path.root)
258
- assert_equal('.', @rel_path.root)
259
-
260
- assert_non_destructive
261
- end
262
-
263
- def test_root?
264
- assert_respond_to(@abs_path, :root?)
265
- assert_nothing_raised{ @abs_path.root? }
266
- assert_nothing_raised{ @rel_path.root? }
267
-
268
- path1 = Pathname.new('/')
269
- path2 = Pathname.new('a')
270
- assert_equal(true, path1.root?)
271
- assert_equal(false, path2.root?)
272
-
273
- assert_non_destructive
274
- end
275
-
276
- def test_absolute
277
- assert_respond_to(@abs_path, :absolute?)
278
- assert_nothing_raised{ @abs_path.absolute? }
279
- assert_nothing_raised{ @rel_path.absolute? }
280
-
281
- assert_equal(true, @abs_path.absolute?)
282
- assert_equal(false, @rel_path.absolute?)
283
-
284
- assert_equal(true, Pathname.new('/usr/bin/ruby').absolute?)
285
- assert_equal(false, Pathname.new('foo').absolute?)
286
- assert_equal(false, Pathname.new('foo/bar').absolute?)
287
- assert_equal(false, Pathname.new('../foo/bar').absolute?)
288
-
289
- assert_non_destructive
290
- end
291
-
292
- def test_relative
293
- assert_respond_to(@abs_path, :relative?)
294
- assert_nothing_raised{ @abs_path.relative? }
295
- assert_nothing_raised{ @rel_path.relative? }
296
-
297
- assert_equal(false, @abs_path.relative?)
298
- assert_equal(true, @rel_path.relative?)
299
-
300
- assert_equal(false, Pathname.new('/usr/bin/ruby').relative?)
301
- assert_equal(true, Pathname.new('foo').relative?)
302
- assert_equal(true, Pathname.new('foo/bar').relative?)
303
- assert_equal(true, Pathname.new('../foo/bar').relative?)
304
-
305
- assert_non_destructive
306
- end
307
-
308
- def test_to_a
309
- assert_respond_to(@abs_path, :to_a)
310
- assert_nothing_raised{ @abs_path.to_a }
311
- assert_nothing_raised{ @rel_path.to_a }
312
- assert_kind_of(Array, @abs_path.to_a)
313
- assert_equal(%w/usr local bin/, @abs_path.to_a)
314
-
315
- assert_non_destructive
316
- end
317
-
318
- def test_spaceship_operator
319
- assert_respond_to(@abs_path, :<=>)
320
-
321
- assert_pathname_cmp( 0, '/foo/bar', '/foo/bar')
322
- assert_pathname_cmp(-1, '/foo/bar', '/foo/zap')
323
- assert_pathname_cmp( 1, '/foo/zap', '/foo/bar')
324
- assert_pathname_cmp(-1, 'foo', 'foo/')
325
- assert_pathname_cmp(-1, 'foo/', 'foo/bar')
326
- end
327
-
328
- def test_plus_operator
329
- assert_respond_to(@abs_path, :+)
330
-
331
- # Standard stuff
332
- assert_pathname_plus('/foo/bar', '/foo', 'bar')
333
- assert_pathname_plus('foo/bar', 'foo', 'bar')
334
- assert_pathname_plus('foo', 'foo', '.')
335
- assert_pathname_plus('foo', '.', 'foo')
336
- assert_pathname_plus('/foo', 'bar', '/foo')
337
- assert_pathname_plus('foo', 'foo/bar', '..')
338
- assert_pathname_plus('/foo', '/', '../foo')
339
- assert_pathname_plus('foo/zap', 'foo/bar', '../zap')
340
- assert_pathname_plus('.', 'foo', '..')
341
- assert_pathname_plus('foo', '..', 'foo') # Auto clean
342
- assert_pathname_plus('foo', '..', '../foo') # Auto clean
343
-
344
- # Edge cases
345
- assert_pathname_plus('.', '.', '.')
346
- assert_pathname_plus('/', '/', '..')
347
- assert_pathname_plus('.', '..', '..')
348
- assert_pathname_plus('.', 'foo', '..')
349
-
350
- # Alias
351
- assert_equal('/foo/bar', Pathname.new('/foo') / Pathname.new('bar'))
352
- end
353
-
354
- # Any tests marked with '***' mean that this behavior is different than
355
- # the current implementation. It also means I disagree with the current
356
- # implementation.
357
- def test_clean
358
- # Standard stuff
359
- assert_equal('/a/b/c', Pathname.new('/a/b/c').cleanpath)
360
- assert_equal('b/c', Pathname.new('./b/c').cleanpath)
361
- assert_equal('a', Pathname.new('a/.').cleanpath) # ***
362
- assert_equal('a/c', Pathname.new('a/./c').cleanpath)
363
- assert_equal('a/b', Pathname.new('a/b/.').cleanpath) # ***
364
- assert_equal('.', Pathname.new('a/../.').cleanpath) # ***
365
- assert_equal('/a', Pathname.new('/a/b/..').cleanpath)
366
- assert_equal('/b', Pathname.new('/a/../b').cleanpath)
367
- assert_equal('d', Pathname.new('a/../../d').cleanpath) # ***
368
-
369
- # Edge cases
370
- assert_equal('', Pathname.new('').cleanpath)
371
- assert_equal('.', Pathname.new('.').cleanpath)
372
- assert_equal('..', Pathname.new('..').cleanpath)
373
- assert_equal('/', Pathname.new('/').cleanpath)
374
- assert_equal('/', Pathname.new('//').cleanpath)
375
-
376
- assert_non_destructive
377
- end
378
-
379
- def test_dirname_basic
380
- assert_respond_to(@abs_path, :dirname)
381
- assert_nothing_raised{ @abs_path.dirname }
382
- assert_kind_of(String, @abs_path.dirname)
383
- end
384
-
385
- def test_dirname
386
- assert_equal('/usr/local', @abs_path.dirname)
387
- assert_equal('/usr/local/bin', @abs_path.dirname(0))
388
- assert_equal('/usr/local', @abs_path.dirname(1))
389
- assert_equal('/usr', @abs_path.dirname(2))
390
- assert_equal('/', @abs_path.dirname(3))
391
- assert_equal('/', @abs_path.dirname(9))
392
- end
393
-
394
- def test_dirname_expected_errors
395
- assert_raise(ArgumentError){ @abs_path.dirname(-1) }
396
- end
397
-
398
- def test_facade_io
399
- assert_respond_to(@abs_path, :foreach)
400
- assert_respond_to(@abs_path, :read)
401
- assert_respond_to(@abs_path, :readlines)
402
- assert_respond_to(@abs_path, :sysopen)
403
- end
404
-
405
- def test_facade_file
406
- File.methods(false).each{ |method|
407
- assert_respond_to(@abs_path, method.to_sym)
408
- }
409
- end
410
-
411
- def test_facade_dir
412
- Dir.methods(false).each{ |method|
413
- assert_respond_to(@abs_path, method.to_sym)
414
- }
415
- end
416
-
417
- def test_facade_fileutils
418
- methods = FileUtils.public_instance_methods
419
- methods -= File.methods(false)
420
- methods -= Dir.methods(false)
421
- methods.delete_if{ |m| m =~ /stream/ }
422
- methods.delete('identical?')
423
-
424
- methods.each{ |method|
425
- assert_respond_to(@abs_path, method.to_sym)
426
- }
427
- end
428
-
429
- def test_facade_find
430
- assert_respond_to(@abs_path, :find)
431
- assert_nothing_raised{ @abs_path.find{} }
432
-
433
- Pathname.new(Dir.pwd).find{ |f|
434
- Find.prune if f.match('CVS')
435
- assert_kind_of(Pathname, f)
436
- }
437
- end
438
-
439
- # Ensures that subclasses return the subclass as the class, not a hard
440
- # coded Pathname.
441
- #
442
- def test_subclasses
443
- assert_kind_of(MyPathname, @mypath)
444
- assert_kind_of(MyPathname, @mypath + MyPathname.new('foo'))
445
- assert_kind_of(MyPathname, @mypath.realpath)
446
- assert_kind_of(MyPathname, @mypath.children.first)
447
- end
448
-
449
- # Test to ensure that the pn{ } shortcut works
450
- #
451
- def test_kernel_method
452
- assert_respond_to(Kernel, :pn)
453
- assert_nothing_raised{ pn{'/foo'} }
454
- assert_kind_of(Pathname, pn{'/foo'})
455
- assert_equal('/foo', pn{'/foo'})
456
- end
457
-
458
- def test_pwd_singleton_method
459
- assert_respond_to(Pathname, :pwd)
460
- assert_kind_of(String, Pathname.pwd)
461
- assert_equal(@@pwd, Pathname.pwd)
462
- end
463
-
464
- def teardown
465
- @abs_path = nil
466
- @rel_path = nil
467
- @trl_path = nil
468
- @mul_path = nil
469
- @rul_path = nil
470
- @cur_path = nil
471
- @abs_path = nil
472
- @rel_path = nil
473
- @cur_path = nil
474
-
475
- @mypath = nil
476
-
477
- @abs_array.clear
478
- @rel_array.clear
479
- end
480
-
481
- def self.shutdown
482
- @@pwd = nil
483
- end
19
+ def self.startup
20
+ Dir.chdir(File.expand_path(File.dirname(__FILE__)))
21
+ @@pwd = Dir.pwd
22
+ end
23
+
24
+ def setup
25
+ @abs_path = Pathname.new('/usr/local/bin')
26
+ @rel_path = Pathname.new('usr/local/bin')
27
+ @trl_path = Pathname.new('/usr/local/bin/')
28
+ @mul_path = Pathname.new('/usr/local/lib/local/lib')
29
+ @rul_path = Pathname.new('usr/local/lib/local/lib')
30
+ @url_path = Pathname.new('file:///foo%20bar/baz')
31
+ @cur_path = Pathname.new(@@pwd)
32
+
33
+ @abs_array = []
34
+ @rel_array = []
35
+
36
+ @mypath = MyPathname.new('/usr/bin')
37
+ end
38
+
39
+ # Convenience method to verify that the receiver was not modified
40
+ # except perhaps slashes
41
+ def assert_non_destructive
42
+ assert_equal('/usr/local/bin', @abs_path)
43
+ assert_equal('usr/local/bin', @rel_path)
44
+ end
45
+
46
+ # Convenience method for test_plus
47
+ def assert_pathname_plus(a, b, c)
48
+ a = Pathname.new(a)
49
+ b = Pathname.new(b)
50
+ c = Pathname.new(c)
51
+ assert_equal(a, b + c)
52
+ end
53
+
54
+ # Convenience method for test_spaceship operator
55
+ def assert_pathname_cmp(int, s1, s2)
56
+ p1 = Pathname.new(s1)
57
+ p2 = Pathname.new(s2)
58
+ result = p1 <=> p2
59
+ assert_equal(int, result)
60
+ end
61
+
62
+ # Convenience method for test_relative_path_from
63
+ def assert_relpath(result, dest, base)
64
+ assert_equal(result, Pathname.new(dest).relative_path_from(base))
65
+ end
66
+
67
+ # Convenience method for test_relative_path_from_expected_errors
68
+ def assert_relpath_err(to, from)
69
+ assert_raise(ArgumentError) {
70
+ Pathname.new(to).relative_path_from(from)
71
+ }
72
+ end
73
+
74
+ def test_version
75
+ assert_equal('1.6.4', Pathname::VERSION)
76
+ end
77
+
78
+ def test_file_url_path
79
+ assert_equal('/foo bar/baz', @url_path)
80
+ end
81
+
82
+ def test_realpath
83
+ assert_respond_to(@abs_path, :realpath)
84
+ assert_equal(@@pwd, Pathname.new('.').realpath)
85
+ assert_kind_of(Pathname, Pathname.new('/dev/stdin').realpath)
86
+ assert(Pathname.new('/dev/stdin') != Pathname.new('/dev/stdin').realpath)
87
+ if CONFIG['host_os'] =~ /bsd|darwin|mac/i
88
+ assert_raises(Errno::ENOENT){ Pathname.new('../blahblah/bogus').realpath }
89
+ else
90
+ assert_raises(Errno::ENOENT){ Pathname.new('../bogus').realpath }
91
+ end
92
+ end
93
+
94
+ def test_realpath_platform
95
+ case CONFIG['host_os']
96
+ when /linux/i
97
+ path1 = '/dev/stdin'
98
+ assert_true(['/dev/pts/0', '/dev/proc/self/fd/0'].include?(Pathname.new(path1).realpath))
99
+ when /sunos|solaris/i
100
+ path1 = '/dev/null'
101
+ path2 = '/dev/stdin'
102
+ path3 = '/dev/fd0' # Multiple symlinks
103
+
104
+ assert_equal('/devices/pseudo/mm@0:null', Pathname.new(path1).realpath)
105
+ assert_equal('/dev/fd/0', Pathname.new(path2).realpath)
106
+ assert_equal('/devices/pci@1f,0/isa@7/dma@0,0/floppy@0,3f0:c', Pathname.new(path3).realpath)
107
+ end
108
+ end
109
+
110
+ # These tests taken directly from Tanaka's pathname.rb. The one failure
111
+ # (commented out) is due to the fact that Tanaka's cleanpath method returns
112
+ # the cleanpath for '../a' as '../a' (i.e. it does nothing) whereas mine
113
+ # converts '../a' into just 'a'. Which is correct? I vote mine, because
114
+ # I don't see how you can get 'more relative' from a relative path not
115
+ # already in the pathname.
116
+ #
117
+ def test_relative_path_from
118
+ assert_relpath('../a', 'a', 'b')
119
+ assert_relpath('../a', 'a', 'b/')
120
+ assert_relpath('../a', 'a/', 'b')
121
+ assert_relpath('../a', 'a/', 'b/')
122
+ assert_relpath('../a', '/a', '/b')
123
+ assert_relpath('../a', '/a', '/b/')
124
+ assert_relpath('../a', '/a/', '/b')
125
+ assert_relpath('../a', '/a/', '/b/')
126
+
127
+ assert_relpath('../b', 'a/b', 'a/c')
128
+ assert_relpath('../a', '../a', '../b')
129
+
130
+ assert_relpath('a', 'a', '.')
131
+ assert_relpath('..', '.', 'a')
132
+
133
+ assert_relpath('.', '.', '.')
134
+ assert_relpath('.', '..', '..')
135
+ assert_relpath('..', '..', '.')
136
+
137
+ assert_relpath('c/d', '/a/b/c/d', '/a/b')
138
+ assert_relpath('../..', '/a/b', '/a/b/c/d')
139
+ assert_relpath('../../../../e', '/e', '/a/b/c/d')
140
+ assert_relpath('../b/c', 'a/b/c', 'a/d')
141
+
142
+ assert_relpath('../a', '/../a', '/b')
143
+ #assert_relpath('../../a', '../a', 'b') # fails
144
+ assert_relpath('.', '/a/../../b', '/b')
145
+ assert_relpath('..', 'a/..', 'a')
146
+ assert_relpath('.', 'a/../b', 'b')
147
+
148
+ assert_relpath('a', 'a', 'b/..')
149
+ assert_relpath('b/c', 'b/c', 'b/..')
150
+
151
+ assert_relpath_err('/', '.')
152
+ assert_relpath_err('.', '/')
153
+ assert_relpath_err('a', '..')
154
+ assert_relpath_err('.', '..')
155
+ end
156
+
157
+ def test_parent
158
+ assert_respond_to(@abs_path, :parent)
159
+ assert_equal('/usr/local', @abs_path.parent)
160
+ assert_equal('usr/local', @rel_path.parent)
161
+ assert_equal('/', Pathname.new('/').parent)
162
+ end
163
+
164
+ def test_pstrip
165
+ assert_respond_to(@trl_path, :pstrip)
166
+ assert_nothing_raised{ @trl_path.pstrip }
167
+ assert_equal('/usr/local/bin', @trl_path.pstrip)
168
+ assert_equal('/usr/local/bin/', @trl_path)
169
+ end
170
+
171
+ def test_pstrip_bang
172
+ assert_respond_to(@trl_path, :pstrip!)
173
+ assert_nothing_raised{ @trl_path.pstrip! }
174
+ assert_equal('/usr/local/bin', @trl_path.pstrip!)
175
+ assert_equal('/usr/local/bin', @trl_path)
176
+ end
177
+
178
+ def test_ascend
179
+ assert_respond_to(@abs_path, :ascend)
180
+ assert_nothing_raised{ @abs_path.ascend{} }
181
+
182
+ @abs_path.ascend{ |path| @abs_array.push(path) }
183
+ @rel_path.ascend{ |path| @rel_array.push(path) }
184
+
185
+ assert_equal('/usr/local/bin', @abs_array[0])
186
+ assert_equal('/usr/local', @abs_array[1])
187
+ assert_equal('/usr', @abs_array[2])
188
+ assert_equal('/', @abs_array[3])
189
+ assert_equal(4, @abs_array.length)
190
+
191
+ assert_equal('usr/local/bin', @rel_array[0])
192
+ assert_equal('usr/local', @rel_array[1])
193
+ assert_equal('usr', @rel_array[2])
194
+ assert_equal(3, @rel_array.length)
195
+
196
+ assert_non_destructive
197
+ end
198
+
199
+ def test_descend
200
+ assert_respond_to(@abs_path, :descend)
201
+ assert_nothing_raised{ @abs_path.descend{} }
202
+
203
+ @abs_path.descend{ |path| @abs_array.push(path) }
204
+ @rel_path.descend{ |path| @rel_array.push(path) }
205
+
206
+ assert_equal('/', @abs_array[0])
207
+ assert_equal('/usr', @abs_array[1])
208
+ assert_equal('/usr/local', @abs_array[2])
209
+ assert_equal('/usr/local/bin', @abs_array[3])
210
+ assert_equal(4, @abs_array.length)
211
+
212
+ assert_equal('usr', @rel_array[0])
213
+ assert_equal('usr/local', @rel_array[1])
214
+ assert_equal('usr/local/bin', @rel_array[2])
215
+ assert_equal(3, @rel_array.length)
216
+
217
+ assert_non_destructive
218
+ end
219
+
220
+ def test_children_with_directory
221
+ assert_respond_to(@cur_path, :children)
222
+ assert_nothing_raised{ @cur_path.children }
223
+ assert_kind_of(Array, @cur_path.children)
224
+
225
+ children = @cur_path.children.sort.reject{ |f| f.include?('CVS') }
226
+ assert_equal(
227
+ [
228
+ Dir.pwd + '/test_pathname.rb',
229
+ Dir.pwd + '/test_pathname_windows.rb'
230
+ ],
231
+ children.sort
232
+ )
233
+ end
234
+
235
+ def test_children_without_directory
236
+ assert_nothing_raised{ @cur_path.children(false) }
237
+
238
+ children = @cur_path.children(false).reject{ |f| f.include?('CVS') }
239
+ assert_equal(['test_pathname.rb', 'test_pathname_windows.rb'], children.sort)
240
+ end
241
+
242
+ def test_unc
243
+ assert_raises(NotImplementedError){ @abs_path.unc? }
244
+ end
245
+
246
+ def test_enumerable
247
+ assert_respond_to(@abs_path, :each)
248
+ end
249
+
250
+ def test_root
251
+ assert_respond_to(@abs_path, :root)
252
+ assert_nothing_raised{ @abs_path.root }
253
+ assert_nothing_raised{ @rel_path.root }
254
+
255
+ assert_equal('/', @abs_path.root)
256
+ assert_equal('.', @rel_path.root)
257
+
258
+ assert_non_destructive
259
+ end
260
+
261
+ def test_root?
262
+ assert_respond_to(@abs_path, :root?)
263
+ assert_nothing_raised{ @abs_path.root? }
264
+ assert_nothing_raised{ @rel_path.root? }
265
+
266
+ path1 = Pathname.new('/')
267
+ path2 = Pathname.new('a')
268
+ assert_equal(true, path1.root?)
269
+ assert_equal(false, path2.root?)
270
+
271
+ assert_non_destructive
272
+ end
273
+
274
+ def test_absolute
275
+ assert_respond_to(@abs_path, :absolute?)
276
+ assert_nothing_raised{ @abs_path.absolute? }
277
+ assert_nothing_raised{ @rel_path.absolute? }
278
+
279
+ assert_equal(true, @abs_path.absolute?)
280
+ assert_equal(false, @rel_path.absolute?)
281
+
282
+ assert_equal(true, Pathname.new('/usr/bin/ruby').absolute?)
283
+ assert_equal(false, Pathname.new('foo').absolute?)
284
+ assert_equal(false, Pathname.new('foo/bar').absolute?)
285
+ assert_equal(false, Pathname.new('../foo/bar').absolute?)
286
+
287
+ assert_non_destructive
288
+ end
289
+
290
+ def test_relative
291
+ assert_respond_to(@abs_path, :relative?)
292
+ assert_nothing_raised{ @abs_path.relative? }
293
+ assert_nothing_raised{ @rel_path.relative? }
294
+
295
+ assert_equal(false, @abs_path.relative?)
296
+ assert_equal(true, @rel_path.relative?)
297
+
298
+ assert_equal(false, Pathname.new('/usr/bin/ruby').relative?)
299
+ assert_equal(true, Pathname.new('foo').relative?)
300
+ assert_equal(true, Pathname.new('foo/bar').relative?)
301
+ assert_equal(true, Pathname.new('../foo/bar').relative?)
302
+
303
+ assert_non_destructive
304
+ end
305
+
306
+ def test_to_a
307
+ assert_respond_to(@abs_path, :to_a)
308
+ assert_nothing_raised{ @abs_path.to_a }
309
+ assert_nothing_raised{ @rel_path.to_a }
310
+ assert_kind_of(Array, @abs_path.to_a)
311
+ assert_equal(%w/usr local bin/, @abs_path.to_a)
312
+
313
+ assert_non_destructive
314
+ end
315
+
316
+ def test_spaceship_operator
317
+ assert_respond_to(@abs_path, :<=>)
318
+
319
+ assert_pathname_cmp( 0, '/foo/bar', '/foo/bar')
320
+ assert_pathname_cmp(-1, '/foo/bar', '/foo/zap')
321
+ assert_pathname_cmp( 1, '/foo/zap', '/foo/bar')
322
+ assert_pathname_cmp(-1, 'foo', 'foo/')
323
+ assert_pathname_cmp(-1, 'foo/', 'foo/bar')
324
+ end
325
+
326
+ def test_plus_operator
327
+ assert_respond_to(@abs_path, :+)
328
+
329
+ # Standard stuff
330
+ assert_pathname_plus('/foo/bar', '/foo', 'bar')
331
+ assert_pathname_plus('foo/bar', 'foo', 'bar')
332
+ assert_pathname_plus('foo', 'foo', '.')
333
+ assert_pathname_plus('foo', '.', 'foo')
334
+ assert_pathname_plus('/foo', 'bar', '/foo')
335
+ assert_pathname_plus('foo', 'foo/bar', '..')
336
+ assert_pathname_plus('/foo', '/', '../foo')
337
+ assert_pathname_plus('foo/zap', 'foo/bar', '../zap')
338
+ assert_pathname_plus('.', 'foo', '..')
339
+ assert_pathname_plus('foo', '..', 'foo') # Auto clean
340
+ assert_pathname_plus('foo', '..', '../foo') # Auto clean
341
+
342
+ # Edge cases
343
+ assert_pathname_plus('.', '.', '.')
344
+ assert_pathname_plus('/', '/', '..')
345
+ assert_pathname_plus('.', '..', '..')
346
+ assert_pathname_plus('.', 'foo', '..')
347
+
348
+ # Alias
349
+ assert_equal('/foo/bar', Pathname.new('/foo') / Pathname.new('bar'))
350
+ end
351
+
352
+ # Any tests marked with '***' mean that this behavior is different than
353
+ # the current implementation. It also means I disagree with the current
354
+ # implementation.
355
+ def test_clean
356
+ # Standard stuff
357
+ assert_equal('/a/b/c', Pathname.new('/a/b/c').cleanpath)
358
+ assert_equal('b/c', Pathname.new('./b/c').cleanpath)
359
+ assert_equal('a', Pathname.new('a/.').cleanpath) # ***
360
+ assert_equal('a/c', Pathname.new('a/./c').cleanpath)
361
+ assert_equal('a/b', Pathname.new('a/b/.').cleanpath) # ***
362
+ assert_equal('.', Pathname.new('a/../.').cleanpath) # ***
363
+ assert_equal('/a', Pathname.new('/a/b/..').cleanpath)
364
+ assert_equal('/b', Pathname.new('/a/../b').cleanpath)
365
+ assert_equal('d', Pathname.new('a/../../d').cleanpath) # ***
366
+
367
+ # Edge cases
368
+ assert_equal('', Pathname.new('').cleanpath)
369
+ assert_equal('.', Pathname.new('.').cleanpath)
370
+ assert_equal('..', Pathname.new('..').cleanpath)
371
+ assert_equal('/', Pathname.new('/').cleanpath)
372
+ assert_equal('/', Pathname.new('//').cleanpath)
373
+
374
+ assert_non_destructive
375
+ end
376
+
377
+ def test_dirname_basic
378
+ assert_respond_to(@abs_path, :dirname)
379
+ assert_nothing_raised{ @abs_path.dirname }
380
+ assert_kind_of(String, @abs_path.dirname)
381
+ end
382
+
383
+ def test_dirname
384
+ assert_equal('/usr/local', @abs_path.dirname)
385
+ assert_equal('/usr/local/bin', @abs_path.dirname(0))
386
+ assert_equal('/usr/local', @abs_path.dirname(1))
387
+ assert_equal('/usr', @abs_path.dirname(2))
388
+ assert_equal('/', @abs_path.dirname(3))
389
+ assert_equal('/', @abs_path.dirname(9))
390
+ end
391
+
392
+ def test_dirname_expected_errors
393
+ assert_raise(ArgumentError){ @abs_path.dirname(-1) }
394
+ end
395
+
396
+ def test_facade_io
397
+ assert_respond_to(@abs_path, :foreach)
398
+ assert_respond_to(@abs_path, :read)
399
+ assert_respond_to(@abs_path, :readlines)
400
+ assert_respond_to(@abs_path, :sysopen)
401
+ end
402
+
403
+ def test_facade_file
404
+ File.methods(false).each{ |method|
405
+ assert_respond_to(@abs_path, method.to_sym)
406
+ }
407
+ end
408
+
409
+ def test_facade_dir
410
+ Dir.methods(false).each{ |method|
411
+ assert_respond_to(@abs_path, method.to_sym)
412
+ }
413
+ end
414
+
415
+ def test_facade_fileutils
416
+ methods = FileUtils.public_instance_methods
417
+ methods -= File.methods(false)
418
+ methods -= Dir.methods(false)
419
+ methods.delete_if{ |m| m =~ /stream/ }
420
+ methods.delete('identical?')
421
+
422
+ methods.each{ |method|
423
+ assert_respond_to(@abs_path, method.to_sym)
424
+ }
425
+ end
426
+
427
+ def test_facade_find
428
+ assert_respond_to(@abs_path, :find)
429
+ assert_nothing_raised{ @abs_path.find{} }
430
+
431
+ Pathname.new(Dir.pwd).find{ |f|
432
+ Find.prune if f.match('CVS')
433
+ assert_kind_of(Pathname, f)
434
+ }
435
+ end
436
+
437
+ # Ensures that subclasses return the subclass as the class, not a hard
438
+ # coded Pathname.
439
+ #
440
+ def test_subclasses
441
+ assert_kind_of(MyPathname, @mypath)
442
+ assert_kind_of(MyPathname, @mypath + MyPathname.new('foo'))
443
+ assert_kind_of(MyPathname, @mypath.realpath)
444
+ assert_kind_of(MyPathname, @mypath.children.first)
445
+ end
446
+
447
+ # Test to ensure that the pn{ } shortcut works
448
+ #
449
+ def test_kernel_method
450
+ assert_respond_to(Kernel, :pn)
451
+ assert_nothing_raised{ pn{'/foo'} }
452
+ assert_kind_of(Pathname, pn{'/foo'})
453
+ assert_equal('/foo', pn{'/foo'})
454
+ end
455
+
456
+ def test_pwd_singleton_method
457
+ assert_respond_to(Pathname, :pwd)
458
+ assert_kind_of(String, Pathname.pwd)
459
+ assert_equal(@@pwd, Pathname.pwd)
460
+ end
461
+
462
+ def teardown
463
+ @abs_path = nil
464
+ @rel_path = nil
465
+ @trl_path = nil
466
+ @mul_path = nil
467
+ @rul_path = nil
468
+ @cur_path = nil
469
+ @abs_path = nil
470
+ @rel_path = nil
471
+ @cur_path = nil
472
+ @mypath = nil
473
+ @abs_array.clear
474
+ @rel_array.clear
475
+ end
476
+
477
+ def self.shutdown
478
+ @@pwd = nil
479
+ end
484
480
  end