pathname2 1.7.1-universal-mingw32 → 1.7.2-universal-mingw32

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