pathname2 1.6.4-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/pathname2.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+
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
+
14
+ spec.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
15
+ spec.rubyforge_project = 'shards'
16
+
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
+ end
@@ -0,0 +1,480 @@
1
+ ##############################################################################
2
+ # test_pathname.rb
3
+ #
4
+ # Test suite for the pathname package (Unix). This test suite should be run
5
+ # via the Rake tasks, i.e. 'rake test_pr' to test the pure Ruby version, or
6
+ # 'rake test_c' to test the C version.
7
+ ##############################################################################
8
+ require 'pathname2'
9
+ require 'rbconfig'
10
+ include Config
11
+
12
+ require 'rubygems'
13
+ gem 'test-unit'
14
+ require 'test/unit'
15
+
16
+ class MyPathname < Pathname; end
17
+
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.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
480
+ end