pathname2 1.6.3-x86-mswin32-60

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 |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') }
13
+
14
+ gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
15
+ gem.rubyforge_project = 'shards'
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
37
+ end
@@ -0,0 +1,484 @@
1
+ ##############################################################################
2
+ # tc_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 'rubygems'
9
+ gem 'test-unit'
10
+
11
+ require 'test/unit'
12
+ require 'pathname2'
13
+ require 'rbconfig'
14
+ include Config
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.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
484
+ end