pathname2 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,17 @@
1
+ == 1.4.0 -
2
+ * Added destructive and non-destructive methods for some methods - pstrip,
3
+ pstrip!, undecorate, undecorate!, clean and clean!.
4
+ * Added a C extension version of this package. You can use the C version
5
+ instead of the pure Ruby version instead. See the README for more details.
6
+ * Fixed bug in the root method where the result wasn't guaranteed to be a
7
+ Pathname class.
8
+ * Fixed bugs in Windows version where the receiver was inadvertantly modified
9
+ in some cases, and added tests to check for this in the future.
10
+ * Modified the Dir.glob facade so that it (temporarily) changes to the path
11
+ directory, globs on that path, then returns to the original directory.
12
+ * Added the bench_pathname.rb script to let you benchmark all Pathname
13
+ methods.
14
+
1
15
  == 1.3.1 - 21-Nov-2005
2
16
  * Added the Pathname#children method.
3
17
  * Added tests for the Pathname#children method.
data/MANIFEST CHANGED
@@ -4,9 +4,13 @@ README
4
4
  install.rb
5
5
  pathname2.gempsec
6
6
 
7
+ examples/bench_all.rb
7
8
  examples/bench_plus.rb
8
9
  examples/example_pathname.rb
9
10
 
11
+ ext/extconf.rb
12
+ ext/pathname2.c
13
+
10
14
  lib/pathname2.rb
11
15
 
12
16
  test/tc_pathname.rb
data/README CHANGED
@@ -5,7 +5,7 @@
5
5
  * Ruby 1.8.0 or later
6
6
  * facade 1.0.0 or later (available on the RAA)
7
7
 
8
- == Installation
8
+ == Installation, pure Ruby
9
9
  === Manual Installation
10
10
  ruby test/tc_pathname.rb (Unix, optional)
11
11
  ruby test/tc_pathname_win.rb (Windows, optional)
@@ -15,6 +15,13 @@
15
15
  ruby test/tc_pathname.rb (Unix, optional)
16
16
  ruby test/tc_pathname_win.rb (Windows, optional)
17
17
  gem install pathname2-<version>.gem
18
+
19
+ == Installation, C extension
20
+ cd to the 'ext' directory.
21
+ ruby extconf.rb
22
+ make
23
+ run appropriate test (optional)
24
+ make install
18
25
 
19
26
  == Synopsis
20
27
  require "pathname2"
@@ -28,7 +35,7 @@
28
35
  path1.dirname # "/foo/bar"
29
36
  path1.to_a # ['foo','bar','baz']
30
37
 
31
- # Win32
38
+ # Windows
32
39
  path1 = "C:/foo/bar/baz"
33
40
  path2 = "../zap"
34
41
 
@@ -39,7 +46,7 @@
39
46
  == Win32 Notes
40
47
  All forward slashes are converted to backslashes for Pathname objects.
41
48
 
42
- == Differences between Unix and Win32
49
+ == Differences between Unix and Windows
43
50
  If your pathname consists solely of ".", or "..", the return
44
51
  value for Pathname#clean will be different. On Win32, "\\" is returned,
45
52
  while on Unix "." is returned. I consider this an extreme edge case and
@@ -73,8 +80,12 @@
73
80
  not the one defined in the FileUtils module.
74
81
 
75
82
  == Known Issues
76
- You cannot pass a frozen string to the constructor on Win32 systems.
77
- There is an unresolved issue with Win32API and frozen objects.
83
+ You cannot pass a frozen string to the constructor on Windows using the
84
+ pure Ruby version. There is an unresolved issue with Win32API and frozen
85
+ objects.
86
+
87
+ Pathname#glob is not yet implemented in the C version.
88
+ Pathname#find is not implemented properly in the C version.
78
89
 
79
90
  == License
80
91
  Ruby's
data/lib/pathname2.rb CHANGED
@@ -45,6 +45,8 @@ class Pathname < String
45
45
  extend Facade
46
46
  facade File
47
47
  facade Dir
48
+
49
+ alias :_plus_ :+ # Used to prevent infinite loops in some cases
48
50
 
49
51
  if PLATFORM.match("mswin")
50
52
  require "Win32API"
@@ -75,7 +77,7 @@ class Pathname < String
75
77
  Win32API.new("shlwapi", "PathRemoveBackslash", "P", "P")
76
78
  end
77
79
 
78
- VERSION = "1.3.1"
80
+ VERSION = "1.4.0"
79
81
  MAX_PATH = 260
80
82
 
81
83
  # Creates and returns a new Pathname object.
@@ -103,7 +105,7 @@ class Pathname < String
103
105
  end
104
106
  end
105
107
 
106
- # Convert forward slashes to backslashes on Win32
108
+ # Convert forward slashes to backslashes on Windows
107
109
  path = path.tr("/", @sep) if @win
108
110
  super(path)
109
111
  end
@@ -133,7 +135,7 @@ class Pathname < String
133
135
  # Win32 only
134
136
  #
135
137
  # Removes the decoration from a path string. For example,
136
- # C:\Path\File[5].txt would become C:\Path\File.txt.
138
+ # C:\Path\File[5].txt would become C:\Path\File.txt. Non-destructive.
137
139
  def undecorate
138
140
  unless @win
139
141
  raise NotImplementedError, "not supported on this platform"
@@ -144,6 +146,20 @@ class Pathname < String
144
146
  Pathname.new(buf.split(0.chr).first)
145
147
  end
146
148
 
149
+ # Win32 only
150
+ #
151
+ # Performs the substitution of Pathname#undecorate in place.
152
+ def undecorate!
153
+ unless @win
154
+ raise NotImplementedError, "not supported on this platform"
155
+ end
156
+ buf = 0.chr * MAX_PATH
157
+ buf[0..self.length-1] = self
158
+ @@PathUndecorate.call(buf)
159
+ replace(buf.split(0.chr).first)
160
+ self
161
+ end
162
+
147
163
  # Win32 only
148
164
  #
149
165
  # Returns the short path for a long path name. For example,
@@ -172,8 +188,23 @@ class Pathname < String
172
188
  Pathname.new(buf.split(0.chr).first)
173
189
  end
174
190
 
175
- # Removes trailing slash, if present.
191
+ # Removes trailing slash, if present. Non-destructive.
176
192
  def pstrip
193
+ str = self.dup
194
+ if @win
195
+ @@PathRemoveBackslash.call(str)
196
+ str.strip!
197
+ else
198
+ if str[-1] == @sep
199
+ str.strip!
200
+ str.chop!
201
+ end
202
+ end
203
+ Pathname.new(str)
204
+ end
205
+
206
+ # Removes trailing slash, if present. Destructive.
207
+ def pstrip!
177
208
  if @win
178
209
  @@PathRemoveBackslash.call(self)
179
210
  strip!
@@ -275,9 +306,9 @@ class Pathname < String
275
306
  # Returns the root directory of the path, or '.' if there is no root
276
307
  # directory.
277
308
  #
278
- # On Unix, this means the '/' character. On Win32 systems, this can
279
- # refer to the drive letter, or the server and share path if the path
280
- # is a UNC path.
309
+ # On Unix, this means the '/' character. On Windows, this can refer
310
+ # to the drive letter, or the server and share path if the path is a
311
+ # UNC path.
281
312
  def root
282
313
  dir = "."
283
314
  if @win
@@ -285,12 +316,12 @@ class Pathname < String
285
316
  buf[0..self.length-1] = self
286
317
 
287
318
  if @@PathStripToRoot.call(buf) > 0
288
- dir = Pathname.new(buf.split(0.chr).first)
319
+ dir = buf.split(0.chr).first
289
320
  end
290
321
  else
291
322
  dir = "/" if self =~ /^\//
292
323
  end
293
- dir
324
+ Pathname.new(dir)
294
325
  end
295
326
 
296
327
  # Returns whether or not the path consists only of a root directory.
@@ -352,12 +383,13 @@ class Pathname < String
352
383
 
353
384
  # Any path plus "." is the same directory
354
385
  return self if string == "."
386
+ return string if self == "."
355
387
 
356
388
  # Use the builtin PathAppend method if on Windows - much easier
357
389
  if @win
358
390
  buf = 0.chr * MAX_PATH
359
391
  buf[0..self.length-1] = self
360
- @@PathAppend.call(buf, string << 0.chr)
392
+ @@PathAppend.call(buf, string)
361
393
  buf.strip!
362
394
  return Pathname.new(buf) # PathAppend cleans automatically
363
395
  end
@@ -369,7 +401,8 @@ class Pathname < String
369
401
  new_string = array.join(@sep)
370
402
 
371
403
  unless relative? || @win
372
- new_string = @sep + new_string # Add root path back if needed
404
+ temp = @sep + new_string # Add root path back if needed
405
+ new_string.replace(temp)
373
406
  end
374
407
 
375
408
  Pathname.new(new_string).clean
@@ -411,11 +444,39 @@ class Pathname < String
411
444
  end
412
445
  }
413
446
  final = final.join(@sep)
414
- final = root + final if root != "."
447
+ final = root._plus_(final) if root != "."
415
448
  final = "." if final.empty?
416
449
  Pathname.new(final)
417
450
  end
418
451
  alias :cleanpath :clean
452
+
453
+ # Removes unnecessary '.' paths and ellides '..' paths appropriately.
454
+ # Modifies the receiver in place.
455
+ def clean!
456
+ return self if self.empty?
457
+
458
+ if @win
459
+ path = 0.chr * MAX_PATH
460
+ if @@PathCanonicalize.call(path, self) > 0
461
+ replace(path.split(0.chr).first)
462
+ end
463
+ return self
464
+ end
465
+
466
+ final = []
467
+ to_a.each{ |element|
468
+ next if element == "."
469
+ final.push(element)
470
+ if element == ".." && self != ".."
471
+ 2.times{ final.pop }
472
+ end
473
+ }
474
+ final = final.join(@sep)
475
+ final = root + final if root != "."
476
+ final = "." if final.empty?
477
+ replace(Pathname.new(final))
478
+ self
479
+ end
419
480
 
420
481
  #-- Find facade
421
482
 
@@ -434,7 +495,7 @@ class Pathname < String
434
495
  if self == "."
435
496
  Find.find(self){ |f| yield Pathname.new(f.sub(%r{\A\./}, '')) }
436
497
  else
437
- Find.find(self) {|f| yield Pathname.new(f) }
498
+ Find.find(self){ |f| yield Pathname.new(f) }
438
499
  end
439
500
  end
440
501
 
@@ -464,11 +525,13 @@ class Pathname < String
464
525
 
465
526
  # Dir.glob
466
527
  def glob(*args)
467
- if block_given?
468
- Dir.glob(*args){ |file| yield Pathname.new(file) }
469
- else
470
- Dir.glob(*args).map{ |file| Pathname.new(file) }
471
- end
528
+ Dir.chdir(self){
529
+ if block_given?
530
+ Dir.glob(*args){ |file| yield Pathname.new(file) }
531
+ else
532
+ Dir.glob(*args).map{ |file| Pathname.new(file) }
533
+ end
534
+ }
472
535
  end
473
536
 
474
537
  # Dir.chdir
data/test/tc_pathname.rb CHANGED
@@ -1,11 +1,18 @@
1
- ##############################################
1
+ ##############################################################################
2
2
  # tc_pathname.rb
3
3
  #
4
- # Test suite for the pathname package (Unix)
5
- ##############################################
4
+ # Test suite for the pathname package (Unix). If you pass a 'c' as command
5
+ # line option, it will test against the C extension. Otherwise, it will run
6
+ # the tests against the pure Ruby version.
7
+ ##############################################################################
6
8
  Dir.chdir ".." if File.basename(Dir.pwd) == "test"
7
- $LOAD_PATH.unshift Dir.pwd
8
- $LOAD_PATH.unshift Dir.pwd + "/lib"
9
+
10
+ # Test against the C extension if a 'c' is provided as an argument.
11
+ if ARGV[0] && ARGV[0].chomp.downcase == "c"
12
+ $LOAD_PATH.unshift Dir.pwd + "/ext"
13
+ else
14
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
15
+ end
9
16
 
10
17
  if PLATFORM.match("mswin")
11
18
  STDERR.puts("Please run 'tc_pathname_win.rb' instead.")
@@ -19,10 +26,18 @@ class TC_Pathname < Test::Unit::TestCase
19
26
  def setup
20
27
  @abs_path = Pathname.new("/usr/local/bin")
21
28
  @rel_path = Pathname.new("usr/local/bin")
29
+ @cur_path = Pathname.new(Dir.pwd)
22
30
 
23
31
  @abs_array = []
24
32
  @rel_array = []
25
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
26
41
 
27
42
  # Convenience method for test_plus
28
43
  def assert_pathname_plus(a, b, c)
@@ -41,7 +56,7 @@ class TC_Pathname < Test::Unit::TestCase
41
56
  end
42
57
 
43
58
  def test_version
44
- assert_equal("1.3.1", Pathname::VERSION)
59
+ assert_equal("1.4.0", Pathname::VERSION)
45
60
  end
46
61
 
47
62
  def test_ascend
@@ -61,6 +76,8 @@ class TC_Pathname < Test::Unit::TestCase
61
76
  assert_equal("usr/local", @rel_array[1])
62
77
  assert_equal("usr", @rel_array[2])
63
78
  assert_equal(3, @rel_array.length)
79
+
80
+ assert_non_destructive
64
81
  end
65
82
 
66
83
  def test_descend
@@ -80,35 +97,48 @@ class TC_Pathname < Test::Unit::TestCase
80
97
  assert_equal("usr/local", @rel_array[1])
81
98
  assert_equal("usr/local/bin", @rel_array[2])
82
99
  assert_equal(3, @rel_array.length)
100
+
101
+ assert_non_destructive
83
102
  end
84
103
 
85
104
  def test_children
86
- Dir.chdir("..") if File.basename(Dir.pwd) == "test"
87
- @cur_path = Pathname.new(Dir.pwd)
88
-
89
105
  assert_respond_to(@cur_path, :children)
90
106
  assert_nothing_raised{ @cur_path.children }
91
107
  assert_kind_of(Array, @cur_path.children)
92
108
 
109
+ # Delete Eclipse related files
110
+ children = @cur_path.children
111
+ children.delete_if{ |e| File.basename(e) == "CVS" }
112
+ children.delete_if{ |e| File.basename(e) == ".cvsignore" }
113
+ children.delete_if{ |e| File.basename(e) == ".project" }
114
+
93
115
  assert_equal(
94
116
  [
95
117
  Dir.pwd + "/CHANGES",
96
118
  Dir.pwd + "/MANIFEST",
97
119
  Dir.pwd + "/README",
98
- Dir.pwd + "/install.rb",
99
- Dir.pwd + "/pathname2.gemspec",
100
120
  Dir.pwd + "/examples",
121
+ Dir.pwd + "/ext",
122
+ Dir.pwd + "/install.rb",
101
123
  Dir.pwd + "/lib",
124
+ Dir.pwd + "/pathname2.gemspec",
102
125
  Dir.pwd + "/test"
103
126
  ],
104
- @cur_path.children
127
+ children.sort
105
128
  )
129
+
130
+ # Delete Eclipse related files
131
+ children = @cur_path.children(false)
132
+ children.delete("CVS")
133
+ children.delete(".cvsignore")
134
+ children.delete(".project")
135
+
106
136
  assert_equal(
107
137
  [
108
- "CHANGES","MANIFEST","README","install.rb",
109
- "pathname2.gemspec","examples", "lib", "test"
138
+ "CHANGES", "MANIFEST", "README", "examples", "ext",
139
+ "install.rb", "lib", "pathname2.gemspec", "test"
110
140
  ],
111
- @cur_path.children(false)
141
+ children.sort
112
142
  )
113
143
  end
114
144
 
@@ -123,47 +153,67 @@ class TC_Pathname < Test::Unit::TestCase
123
153
  def test_root
124
154
  assert_respond_to(@abs_path, :root)
125
155
  assert_nothing_raised{ @abs_path.root }
156
+ assert_nothing_raised{ @rel_path.root }
157
+
126
158
  assert_equal("/", @abs_path.root)
159
+ assert_equal(".", @rel_path.root)
127
160
 
128
- path1 = Pathname.new("foo")
129
- path2 = Pathname.new("foo/bar/baz")
130
- assert_equal(".", path1.root)
131
- assert_equal(".", path2.root)
161
+ assert_non_destructive
132
162
  end
133
163
 
134
164
  def test_root?
135
165
  assert_respond_to(@abs_path, :root?)
136
166
  assert_nothing_raised{ @abs_path.root? }
167
+ assert_nothing_raised{ @rel_path.root? }
137
168
 
138
169
  path1 = Pathname.new("/")
139
170
  path2 = Pathname.new("a")
140
171
  assert_equal(true, path1.root?)
141
172
  assert_equal(false, path2.root?)
173
+
174
+ assert_non_destructive
142
175
  end
143
176
 
144
177
  def test_absolute
145
178
  assert_respond_to(@abs_path, :absolute?)
146
179
  assert_nothing_raised{ @abs_path.absolute? }
180
+ assert_nothing_raised{ @rel_path.absolute? }
181
+
182
+ assert_equal(true, @abs_path.absolute?)
183
+ assert_equal(false, @rel_path.absolute?)
184
+
147
185
  assert_equal(true, Pathname.new("/usr/bin/ruby").absolute?)
148
186
  assert_equal(false, Pathname.new("foo").absolute?)
149
187
  assert_equal(false, Pathname.new("foo/bar").absolute?)
150
188
  assert_equal(false, Pathname.new("../foo/bar").absolute?)
189
+
190
+ assert_non_destructive
151
191
  end
152
192
 
153
193
  def test_relative
154
194
  assert_respond_to(@abs_path, :relative?)
155
195
  assert_nothing_raised{ @abs_path.relative? }
196
+ assert_nothing_raised{ @rel_path.relative? }
197
+
198
+ assert_equal(false, @abs_path.relative?)
199
+ assert_equal(true, @rel_path.relative?)
200
+
156
201
  assert_equal(false, Pathname.new("/usr/bin/ruby").relative?)
157
202
  assert_equal(true, Pathname.new("foo").relative?)
158
203
  assert_equal(true, Pathname.new("foo/bar").relative?)
159
204
  assert_equal(true, Pathname.new("../foo/bar").relative?)
205
+
206
+ assert_non_destructive
160
207
  end
161
208
 
162
209
  def test_to_a
163
210
  assert_respond_to(@abs_path, :to_a)
164
211
  assert_nothing_raised{ @abs_path.to_a }
212
+ assert_nothing_raised{ @rel_path.to_a }
165
213
  assert_kind_of(Array, @abs_path.to_a)
166
214
  assert_equal(%w/usr local bin/, @abs_path.to_a)
215
+
216
+ assert_non_destructive
167
217
  end
168
218
 
169
219
  def test_spaceship_operator
@@ -202,7 +252,7 @@ class TC_Pathname < Test::Unit::TestCase
202
252
  # Any tests marked with "***" mean that this behavior is different than
203
253
  # the current implementation. It also means I disagree with the current
204
254
  # implementation.
205
- def test_cleanpath
255
+ def test_clean
206
256
  # Standard stuff
207
257
  assert_equal("/a/b/c", Pathname.new("/a/b/c").cleanpath)
208
258
  assert_equal("b/c", Pathname.new("./b/c").cleanpath)
@@ -220,6 +270,8 @@ class TC_Pathname < Test::Unit::TestCase
220
270
  assert_equal("..", Pathname.new("..").cleanpath)
221
271
  assert_equal('/', Pathname.new('/').cleanpath)
222
272
  assert_equal('/', Pathname.new('//').cleanpath)
273
+
274
+ assert_non_destructive
223
275
  end
224
276
 
225
277
  def test_facade_io
@@ -253,6 +305,7 @@ class TC_Pathname < Test::Unit::TestCase
253
305
  }
254
306
  end
255
307
 
308
+ # Doesn't work properly in 1.4.0
256
309
  def test_facade_find
257
310
  assert_respond_to(@abs_path, :find)
258
311
  assert_nothing_raised{ @abs_path.find{} }
@@ -266,6 +319,7 @@ class TC_Pathname < Test::Unit::TestCase
266
319
  def teardown
267
320
  @abs_path = nil
268
321
  @rel_path = nil
322
+ @cur_path = nil
269
323
 
270
324
  @abs_array.clear
271
325
  @rel_array.clear
@@ -1,11 +1,20 @@
1
- ######################################################
1
+ ##########################################################################
2
2
  # tc_pathname_win.rb
3
3
  #
4
- # MS Windows test suite for the Pathname class.
5
- ######################################################
4
+ # MS Windows test suite for the Pathname class. To test explicitly
5
+ # against the C extension pass the letter 'c' as an argument.
6
+ ###########################################################################
6
7
  Dir.chdir ".." if File.basename(Dir.pwd) == "test"
7
8
  $LOAD_PATH.unshift Dir.pwd
8
- $LOAD_PATH.unshift Dir.pwd + "/lib"
9
+ $c_extension = false
10
+
11
+ # If the user passes a 'c', test against the C extension
12
+ if ARGV[0] && ARGV[0].downcase == "c"
13
+ $c_extension = true
14
+ $LOAD_PATH.unshift Dir.pwd + "/ext"
15
+ else
16
+ $LOAD_PATH.unshift Dir.pwd + "/lib"
17
+ end
9
18
 
10
19
  unless PLATFORM.match("mswin")
11
20
  STDERR.puts("Please run 'tc_pathname.rb' instead.")
@@ -19,6 +28,7 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
19
28
  def setup
20
29
  @fpath = Pathname.new("C:/Program Files/Windows NT/Accessories")
21
30
  @bpath = Pathname.new("C:\\Program Files\\Windows NT\\Accessories")
31
+ @dpath = Pathname.new("C:\\Program Files\\File[5].txt")
22
32
  @spath = Pathname.new("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1")
23
33
  @upath = Pathname.new("\\\\foo\\bar\\baz")
24
34
  @npath = Pathname.new("foo\\bar\\baz")
@@ -27,6 +37,11 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
27
37
  @ypath = Pathname.new("\\\\foo")
28
38
  @zpath = Pathname.new("\\\\")
29
39
  @epath = Pathname.new("")
40
+ @ppath = Pathname.new("C:\\foo\\bar\\")
41
+ @cpath = Pathname.new("C:\\foo\\..\\bar\\.\\baz")
42
+ @tpath = Pathname.new("C:\\foo\\bar")
43
+
44
+ @cur_path = Pathname.new(Dir.pwd)
30
45
 
31
46
  @abs_array = []
32
47
  @rel_array = []
@@ -34,7 +49,7 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
34
49
  end
35
50
 
36
51
  def test_version
37
- assert_equal("1.3.1", Pathname::VERSION)
52
+ assert_equal("1.4.0", Pathname::VERSION)
38
53
  end
39
54
 
40
55
  # Convenience method for test_plus
@@ -53,11 +68,31 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
53
68
  assert_equal(int, result)
54
69
  end
55
70
 
71
+ # Convenience method to verify that the receiver was not modified
72
+ # except perhaps slashes
73
+ def assert_non_destructive
74
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath)
75
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath)
76
+ assert_equal("C:\\Program Files\\File[5].txt", @dpath)
77
+ assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @spath)
78
+ assert_equal("\\\\foo\\bar\\baz", @upath)
79
+ assert_equal("foo\\bar\\baz", @npath)
80
+ assert_equal("Z:\\", @rpath)
81
+ assert_equal("\\\\foo\\bar", @xpath)
82
+ assert_equal("\\\\foo", @ypath)
83
+ assert_equal("\\\\", @zpath)
84
+ assert_equal("", @epath)
85
+ assert_equal("C:\\foo\\bar\\", @ppath)
86
+ assert_equal("C:\\foo\\..\\bar\\.\\baz", @cpath)
87
+ end
88
+
56
89
  def test_short_path
57
90
  assert_respond_to(@bpath, :short_path)
58
91
  assert_nothing_raised{ @bpath.short_path }
59
92
  assert_kind_of(Pathname, @bpath.short_path)
60
93
  assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @bpath.short_path)
94
+
95
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath)
61
96
  end
62
97
 
63
98
  def test_long_path
@@ -69,13 +104,108 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
69
104
  "C:\\Program Files\\Windows NT\\Accessories",
70
105
  @spath.long_path
71
106
  )
107
+
108
+ assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @spath)
109
+ end
110
+
111
+ def test_undecorate
112
+ assert_respond_to(@dpath, :undecorate)
113
+ assert_nothing_raised{ @dpath.undecorate }
114
+ assert_kind_of(Pathname, @dpath.undecorate)
115
+
116
+ assert_equal('C:\Program Files\File.txt', @dpath.undecorate)
117
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File').undecorate)
118
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File[12]').undecorate)
119
+ assert_equal('C:\Path\[3].txt',
120
+ Pathname.new('C:\Path\[3].txt').undecorate
121
+ )
122
+ assert_equal('\\foo\bar.txt',Pathname.new('\\foo\bar[5].txt').undecorate)
123
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar[5]').undecorate)
124
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar').undecorate)
125
+
126
+ assert_equal("C:\\Program Files\\File[5].txt", @dpath)
127
+ end
128
+
129
+ def test_undecorate_bang
130
+ assert_respond_to(@dpath, :undecorate!)
131
+ assert_nothing_raised{ @dpath.undecorate! }
132
+ assert_kind_of(Pathname, @dpath.undecorate!)
133
+
134
+ assert_equal('C:\Program Files\File.txt', @dpath.undecorate!)
135
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File').undecorate!)
136
+ assert_equal('C:\Path\File', Pathname.new('C:\Path\File[12]').undecorate!)
137
+ assert_equal('C:\Path\[3].txt',
138
+ Pathname.new('C:\Path\[3].txt').undecorate!
139
+ )
140
+ assert_equal('\\foo\bar.txt',Pathname.new('\\foo\bar[5].txt').undecorate!)
141
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar[5]').undecorate!)
142
+ assert_equal('\\foo\bar', Pathname.new('\\foo\bar').undecorate!)
143
+
144
+ assert_equal('C:\Program Files\File.txt', @dpath)
145
+ end
146
+
147
+ def test_unc
148
+ assert_respond_to(@upath, :unc?)
149
+ assert_nothing_raised{ @upath.unc? }
150
+
151
+ assert_equal(true, @upath.unc?)
152
+ assert_equal(true, @xpath.unc?)
153
+ assert_equal(true, @ypath.unc?)
154
+ assert_equal(true, @zpath.unc?)
155
+ assert_equal(false, @fpath.unc?)
156
+ assert_equal(false, @bpath.unc?)
157
+ assert_equal(false, @dpath.unc?)
158
+ assert_equal(false, @spath.unc?)
159
+ assert_equal(false, @epath.unc?)
160
+
161
+ # Arguably a bug in the PathIsUNC() function since drive letters
162
+ # are, in fact, a legal part of a UNC path (for historical reasons).
163
+ assert_equal(false, Pathname.new("C:\\\\foo\\bar\\baz").unc?)
164
+
165
+ assert_non_destructive
166
+ end
167
+
168
+ def test_pstrip
169
+ assert_respond_to(@ppath, :pstrip)
170
+ assert_nothing_raised{ @ppath.pstrip }
171
+ assert_nothing_raised{ @fpath.pstrip }
172
+ assert_kind_of(Pathname, @ppath.pstrip)
173
+
174
+ assert_equal('C:\foo', Pathname.new("C:\\foo\\").pstrip)
175
+ assert_equal('C:\foo', Pathname.new("C:\\foo").pstrip)
176
+ assert_equal("", Pathname.new("").pstrip)
177
+
178
+ assert_equal("C:\\foo\\bar\\", @ppath)
72
179
  end
73
180
 
181
+ def test_pstrip_bang
182
+ assert_respond_to(@ppath, :pstrip!)
183
+ assert_nothing_raised{ @ppath.pstrip! }
184
+ assert_nothing_raised{ @fpath.pstrip! }
185
+ assert_kind_of(Pathname, @ppath.pstrip!)
186
+
187
+ assert_equal('C:\foo', Pathname.new("C:\\foo\\").pstrip!)
188
+ assert_equal('C:\foo', Pathname.new("C:\\foo").pstrip!)
189
+ assert_equal("", Pathname.new("").pstrip!)
190
+
191
+ assert_equal("C:\\foo\\bar", @ppath)
192
+ end
193
+
194
+ def test_exists
195
+ assert_respond_to(@fpath, :exists?)
196
+ assert_nothing_raised{ @fpath.exists? }
197
+ assert_equal(true, Pathname.new("C:\\").exists?)
198
+ assert_equal(false, Pathname.new("X:\\foo\\bar\\baz").exists?)
199
+ end
200
+
74
201
  def test_each
202
+ array = []
203
+
75
204
  assert_respond_to(@fpath, :each)
76
- assert_nothing_raised{ @fpath.each{ |e| } }
205
+ assert_nothing_raised{ @fpath.each{ |e| array.push(e) } }
206
+ assert_equal(["C:", "Program Files", "Windows NT", "Accessories"], array)
77
207
  end
78
-
208
+
79
209
  def test_descend
80
210
  assert_respond_to(@bpath, :descend)
81
211
  assert_nothing_raised{ @bpath.descend{} }
@@ -95,8 +225,10 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
95
225
 
96
226
  assert_equal("\\\\foo\\bar", @unc_array[0])
97
227
  assert_equal("\\\\foo\\bar\\baz", @unc_array[1])
228
+
229
+ assert_non_destructive
98
230
  end
99
-
231
+
100
232
  def test_ascend
101
233
  assert_respond_to(@bpath, :ascend)
102
234
  assert_nothing_raised{ @bpath.ascend{} }
@@ -118,16 +250,20 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
118
250
 
119
251
  assert_equal("\\\\foo\\bar\\baz", @unc_array[0])
120
252
  assert_equal("\\\\foo\\bar", @unc_array[1])
121
- assert_equal(2, @unc_array.length)
253
+ assert_equal(2, @unc_array.length)
254
+
255
+ assert_non_destructive
256
+ end
257
+
258
+ # Fails due to PathIsURL for some reason in pure Ruby
259
+ def test_immutability
260
+ if $c_extension
261
+ path = "C:\\Program Files\\foo\\bar".freeze
262
+ assert_equal(true, path.frozen?)
263
+ assert_nothing_raised{ Pathname.new(path) }
264
+ assert_nothing_raised{ Pathname.new(path).root }
265
+ end
122
266
  end
123
-
124
- # Fails due to PathIsURL for some reason
125
- #def test_immutability
126
- # path = "C:\\Program Files\\foo\\bar".freeze
127
- # assert_equal(true, path.frozen?)
128
- # assert_nothing_raised{ Pathname.new(path) }
129
- # assert_nothing_raised{ Pathname.new(path).root }
130
- #end
131
267
 
132
268
  def test_plus_operator
133
269
  # Standard stuff
@@ -144,9 +280,24 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
144
280
  assert_pathname_plus("\\\\", "\\\\", "")
145
281
  assert_pathname_plus("\\\\foo\\baz", "\\\\foo\\bar", "\\..\\baz")
146
282
  assert_pathname_plus("\\\\", "\\\\", "..\\..\\..\\..")
283
+
284
+ # Pathname + String
285
+ assert_nothing_raised{ @tpath + "bar" }
286
+ assert_equal('C:\foo\bar\baz', @tpath + 'baz')
287
+ assert_equal('C:\foo\bar', @tpath)
288
+
289
+ # Ensure neither left nor right operand are modified
290
+ assert_nothing_raised{ @tpath + @npath }
291
+ assert_equal('C:\foo\bar\foo\bar\baz', @tpath + @npath)
292
+ assert_equal('C:\foo\bar', @tpath)
293
+ assert_equal('foo\bar\baz', @npath)
147
294
  end
148
-
295
+
149
296
  def test_clean
297
+ assert_respond_to(@cpath, :clean)
298
+ assert_nothing_raised{ @cpath.clean }
299
+ assert_kind_of(Pathname, @cpath.clean)
300
+
150
301
  # Our preset stuff
151
302
  assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath.clean)
152
303
  assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath.clean)
@@ -157,6 +308,7 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
157
308
  assert_equal("\\\\foo", @ypath.clean)
158
309
  assert_equal("\\\\", @zpath.clean)
159
310
  assert_equal("", @epath.clean)
311
+ assert_equal("C:\\bar\\baz", @cpath.clean)
160
312
 
161
313
  # Standard stuff
162
314
  assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean)
@@ -170,8 +322,43 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
170
322
  # Edge cases
171
323
  assert_equal("\\", Pathname.new(".").clean)
172
324
  assert_equal("\\", Pathname.new("..").clean)
325
+
326
+ assert_non_destructive
173
327
  end
174
-
328
+
329
+ def test_clean_bang
330
+ assert_respond_to(@cpath, :clean!)
331
+ assert_nothing_raised{ @cpath.clean! }
332
+ assert_kind_of(Pathname, @cpath.clean!)
333
+
334
+ # Our preset stuff
335
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @fpath.clean!)
336
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @bpath.clean!)
337
+ assert_equal("\\\\foo\\bar\\baz", @upath.clean!)
338
+ assert_equal("foo\\bar\\baz", @npath.clean!)
339
+ assert_equal("Z:\\", @rpath.clean!)
340
+ assert_equal("\\\\foo\\bar", @xpath.clean!)
341
+ assert_equal("\\\\foo", @ypath.clean!)
342
+ assert_equal("\\\\", @zpath.clean!)
343
+ assert_equal("", @epath.clean!)
344
+ assert_equal("C:\\bar\\baz", @cpath.clean!)
345
+
346
+ # Standard stuff
347
+ assert_equal("C:\\a\\c", Pathname.new("C:\\a\\.\\b\\..\\c").clean!)
348
+ assert_equal("C:\\a", Pathname.new("C:\\.\\a").clean!)
349
+ assert_equal("C:\\a\\b", Pathname.new("C:\\a\\.\\b").clean!)
350
+ assert_equal("C:\\b", Pathname.new("C:\\a\\..\\b").clean!)
351
+ assert_equal("C:\\a", Pathname.new("C:\\a\\.").clean!)
352
+ assert_equal("C:\\d", Pathname.new("C:\\..\\..\\..\\d").clean!)
353
+ assert_equal("C:\\a\\", Pathname.new("C:\\a\\").clean!)
354
+
355
+ # Edge cases
356
+ assert_equal("\\", Pathname.new(".").clean!)
357
+ assert_equal("\\", Pathname.new("..").clean!)
358
+
359
+ assert_equal("C:\\bar\\baz", @cpath)
360
+ end
361
+
175
362
  def test_absolute
176
363
  assert_equal(true, @fpath.absolute?)
177
364
  assert_equal(true, @bpath.absolute?)
@@ -182,6 +369,8 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
182
369
  assert_equal(true, @ypath.absolute?)
183
370
  assert_equal(true, @zpath.absolute?)
184
371
  assert_equal(false, @epath.absolute?)
372
+
373
+ assert_non_destructive
185
374
  end
186
375
 
187
376
  def test_relative
@@ -194,10 +383,12 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
194
383
  assert_equal(false, @ypath.relative?)
195
384
  assert_equal(false, @zpath.relative?)
196
385
  assert_equal(true, @epath.relative?)
386
+
387
+ assert_non_destructive
197
388
  end
198
389
 
199
390
  def test_root
200
- assert_equal("C:\\", @fpath.root)
391
+ assert_equal("C:\\", @fpath.root)
201
392
  assert_equal("C:\\", @bpath.root)
202
393
  assert_equal("\\\\foo\\bar", @upath.root)
203
394
  assert_equal(".", @npath.root)
@@ -210,6 +401,8 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
210
401
  # Edge cases
211
402
  assert_equal(".", Pathname.new("..").root)
212
403
  assert_equal(".", Pathname.new(".").root)
404
+
405
+ assert_non_destructive
213
406
  end
214
407
 
215
408
  def test_drive_number
@@ -226,6 +419,8 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
226
419
  # Edge cases
227
420
  assert_equal(nil, Pathname.new("..").drive_number)
228
421
  assert_equal(nil, Pathname.new(".").drive_number)
422
+
423
+ assert_non_destructive
229
424
  end
230
425
 
231
426
  def test_to_a
@@ -239,6 +434,9 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
239
434
  assert_equal(["foo"], @ypath.to_a)
240
435
  assert_equal([], @zpath.to_a)
241
436
  assert_equal([], @epath.to_a)
437
+ assert_equal(["C:", "foo", "bar"], @ppath.to_a)
438
+
439
+ assert_non_destructive
242
440
  end
243
441
 
244
442
  def test_is_root
@@ -251,8 +449,13 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
251
449
  assert_equal(true, @ypath.root?)
252
450
  assert_equal(true, @zpath.root?)
253
451
  assert_equal(false, @epath.root?)
452
+ assert_equal(false, @ppath.root?)
453
+
454
+ assert_non_destructive
254
455
  end
255
-
456
+
457
+ # These are the methods from IO we have to explicitly define since
458
+ # they aren't handled by Facade.
256
459
  def test_facade_io
257
460
  assert_respond_to(@fpath, :foreach)
258
461
  assert_respond_to(@fpath, :read)
@@ -297,17 +500,21 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
297
500
  end
298
501
 
299
502
  def test_children
300
- Dir.chdir("..") if File.basename(Dir.pwd) == "test"
301
- @cur_path = Pathname.new(Dir.pwd)
302
-
303
503
  assert_respond_to(@cur_path, :children)
304
504
  assert_nothing_raised{ @cur_path.children }
305
505
  assert_kind_of(Array, @cur_path.children)
306
506
 
507
+ # Delete Eclipse related files
508
+ children = @cur_path.children
509
+ children.delete_if{ |e| File.basename(e) == "CVS" }
510
+ children.delete_if{ |e| File.basename(e) == ".cvsignore" }
511
+ children.delete_if{ |e| File.basename(e) == ".project" }
512
+
307
513
  assert_equal(
308
514
  [
309
515
  Dir.pwd + "/CHANGES",
310
516
  Dir.pwd + "/examples",
517
+ Dir.pwd + "/ext",
311
518
  Dir.pwd + "/install.rb",
312
519
  Dir.pwd + "/lib",
313
520
  Dir.pwd + "/MANIFEST",
@@ -315,20 +522,28 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
315
522
  Dir.pwd + "/README",
316
523
  Dir.pwd + "/test"
317
524
  ].map{ |e| e.tr("/","\\") },
318
- @cur_path.children
525
+ children
319
526
  )
527
+
528
+ # Delete Eclipse related files
529
+ children = @cur_path.children(false)
530
+ children.delete("CVS")
531
+ children.delete(".cvsignore")
532
+ children.delete(".project")
533
+
320
534
  assert_equal(
321
535
  [
322
- "CHANGES","examples","install.rb","lib",
536
+ "CHANGES","examples","ext","install.rb","lib",
323
537
  "MANIFEST", "pathname2.gemspec", "README", "test"
324
538
  ],
325
- @cur_path.children(false)
539
+ children
326
540
  )
327
541
  end
328
-
542
+
329
543
  def teardown
330
544
  @fpath = nil
331
545
  @bpath = nil
546
+ @dpath = nil
332
547
  @spath = nil
333
548
  @upath = nil
334
549
  @npath = nil
@@ -337,6 +552,11 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
337
552
  @ypath = nil
338
553
  @zpath = nil
339
554
  @epath = nil
555
+ @ppath = nil
556
+ @cpath = nil
557
+ @tpath = nil
558
+
559
+ @cur_path = nil
340
560
 
341
561
  @abs_array.clear
342
562
  @rel_array.clear
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: pathname2
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.3.1
7
- date: 2005-11-21 00:00:00 -07:00
6
+ version: 1.4.0
7
+ date: 2005-12-19
8
8
  summary: An alternate implementation of the Pathname class
9
9
  require_paths:
10
10
  - lib
@@ -24,17 +24,15 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
24
24
  version: 0.0.0
25
25
  version:
26
26
  platform: ruby
27
- signing_key:
28
- cert_chain:
29
27
  authors:
30
28
  - Daniel J. Berger
31
29
  files:
32
30
  - lib/pathname2.rb
31
+ - README
33
32
  - CHANGES
34
33
  - MANIFEST
35
- - README
36
- - test/tc_pathname.rb
37
34
  - test/tc_pathname_win.rb
35
+ - test/tc_pathname.rb
38
36
  test_files:
39
37
  - test/tc_pathname.rb
40
38
  rdoc_options: []