pathname2 1.7.2-universal-mingw32 → 1.8.4-universal-mingw32

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