pathname2 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +4 -0
- data/MANIFEST +10 -10
- data/lib/pathname2.rb +48 -23
- data/test/tc_pathname.rb +30 -23
- data/test/tc_pathname_win.rb +15 -2
- metadata +2 -2
data/CHANGES
CHANGED
data/MANIFEST
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
CHANGES
|
2
|
-
MANIFEST
|
3
|
-
README
|
4
|
-
install.rb
|
5
|
-
pathname2.gempsec
|
6
|
-
|
7
|
-
lib/pathname2.rb
|
8
|
-
|
9
|
-
test/tc_pathname.rb
|
10
|
-
test/tc_pathname_win.rb
|
1
|
+
CHANGES
|
2
|
+
MANIFEST
|
3
|
+
README
|
4
|
+
install.rb
|
5
|
+
pathname2.gempsec
|
6
|
+
|
7
|
+
lib/pathname2.rb
|
8
|
+
|
9
|
+
test/tc_pathname.rb
|
10
|
+
test/tc_pathname_win.rb
|
data/lib/pathname2.rb
CHANGED
@@ -46,7 +46,7 @@ class Pathname < String
|
|
46
46
|
facade File
|
47
47
|
facade Dir
|
48
48
|
|
49
|
-
if PLATFORM.match("
|
49
|
+
if PLATFORM.match("mswin")
|
50
50
|
require "Win32API"
|
51
51
|
@@PathStripToRoot = Win32API.new("shlwapi","PathStripToRoot","P","L")
|
52
52
|
@@PathIsUNC = Win32API.new("shlwapi","PathIsUNC","P","L")
|
@@ -69,7 +69,7 @@ class Pathname < String
|
|
69
69
|
Win32API.new("shlwapi", "PathRemoveBackslash", "P", "P")
|
70
70
|
end
|
71
71
|
|
72
|
-
VERSION = "1.2.
|
72
|
+
VERSION = "1.2.1"
|
73
73
|
MAX_PATH = 260
|
74
74
|
|
75
75
|
# Creates and returns a new Pathname object.
|
@@ -81,11 +81,11 @@ class Pathname < String
|
|
81
81
|
raise PathnameError, msg
|
82
82
|
end
|
83
83
|
|
84
|
-
@sep
|
85
|
-
@
|
84
|
+
@sep = File::ALT_SEPARATOR || File::SEPARATOR
|
85
|
+
@win = PLATFORM.match("mswin")
|
86
86
|
|
87
87
|
# Handle File URL's for Windows
|
88
|
-
if @
|
88
|
+
if @win
|
89
89
|
if @@PathIsURL.call(path) > 0
|
90
90
|
buf = 0.chr * MAX_PATH
|
91
91
|
len = [buf.length].pack("l")
|
@@ -98,7 +98,7 @@ class Pathname < String
|
|
98
98
|
end
|
99
99
|
|
100
100
|
# Convert forward slashes to backslashes on Win32
|
101
|
-
path = path.tr("/", @sep) if @
|
101
|
+
path = path.tr("/", @sep) if @win
|
102
102
|
super(path)
|
103
103
|
end
|
104
104
|
|
@@ -107,7 +107,7 @@ class Pathname < String
|
|
107
107
|
# Removes the decoration from a path string. For example,
|
108
108
|
# C:\Path\File[5].txt would become C:\Path\File.txt.
|
109
109
|
def undecorate
|
110
|
-
unless @
|
110
|
+
unless @win
|
111
111
|
raise NotImplementedError, "not supported on this platform"
|
112
112
|
end
|
113
113
|
buf = 0.chr * MAX_PATH
|
@@ -118,7 +118,7 @@ class Pathname < String
|
|
118
118
|
|
119
119
|
# Removes trailing slash, if present.
|
120
120
|
def pstrip
|
121
|
-
if @
|
121
|
+
if @win
|
122
122
|
@@PathRemoveBackslash.call(self)
|
123
123
|
self.strip!
|
124
124
|
else
|
@@ -150,18 +150,26 @@ class Pathname < String
|
|
150
150
|
# on the first iteration, "/foo" on the second, "/foo/bar on the third and
|
151
151
|
# finally "/foo/bar/baz".
|
152
152
|
def descend
|
153
|
-
if
|
154
|
-
|
153
|
+
if root?
|
154
|
+
yield root
|
155
|
+
return
|
156
|
+
end
|
157
|
+
|
158
|
+
if @win
|
159
|
+
path = unc? ? "#{root}\\" : ""
|
155
160
|
else
|
156
161
|
path = absolute? ? root : ""
|
157
162
|
end
|
158
163
|
|
159
164
|
# Yield the root directory if an absolute path (and not Windows)
|
160
|
-
unless
|
165
|
+
unless @win && !unc?
|
161
166
|
yield root if absolute?
|
162
167
|
end
|
163
168
|
|
164
169
|
self.each{ |element|
|
170
|
+
if @win && unc?
|
171
|
+
next if root.to_a.include?(element)
|
172
|
+
end
|
165
173
|
path << element << @sep
|
166
174
|
yield Pathname.new(path.chop)
|
167
175
|
}
|
@@ -172,21 +180,38 @@ class Pathname < String
|
|
172
180
|
#
|
173
181
|
# For example, if the path is "/foo/bar/baz", then "/foo/bar/baz" would
|
174
182
|
# be yielded on the first iteration, "/foo/bar" on the second, "/foo" on
|
175
|
-
# the third, and
|
183
|
+
# the third, and finally "/".
|
176
184
|
def ascend
|
185
|
+
if root?
|
186
|
+
yield root
|
187
|
+
return
|
188
|
+
end
|
189
|
+
|
177
190
|
n = self.to_a.length
|
178
191
|
|
179
192
|
while n > 0
|
180
193
|
path = self.to_a[0..n-1].join(@sep)
|
181
194
|
if self.absolute?
|
182
|
-
|
195
|
+
if @win && unc?
|
196
|
+
path = "\\\\" << path
|
197
|
+
end
|
198
|
+
unless @win
|
199
|
+
path = root << path
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
path = Pathname.new(path)
|
204
|
+
yield path
|
205
|
+
|
206
|
+
if @win && unc?
|
207
|
+
break if path.root?
|
183
208
|
end
|
184
|
-
|
209
|
+
|
185
210
|
n -= 1
|
186
211
|
end
|
187
212
|
|
188
213
|
# Yield the root directory if an absolute path (and not Windows)
|
189
|
-
unless
|
214
|
+
unless @win
|
190
215
|
yield root if absolute?
|
191
216
|
end
|
192
217
|
end
|
@@ -199,7 +224,7 @@ class Pathname < String
|
|
199
224
|
# is a UNC path.
|
200
225
|
def root
|
201
226
|
dir = "."
|
202
|
-
if @
|
227
|
+
if @win
|
203
228
|
buf = 0.chr * MAX_PATH
|
204
229
|
buf[0..self.length-1] = self
|
205
230
|
|
@@ -218,7 +243,7 @@ class Pathname < String
|
|
218
243
|
|
219
244
|
# Returns whether or not the path consists only of a root directory.
|
220
245
|
def root?
|
221
|
-
if @
|
246
|
+
if @win
|
222
247
|
@@PathIsRoot.call(self) > 0
|
223
248
|
else
|
224
249
|
self == root
|
@@ -230,7 +255,7 @@ class Pathname < String
|
|
230
255
|
# Determines if the string is a valid Universal Naming Convention (UNC)
|
231
256
|
# for a server and share path.
|
232
257
|
def unc?
|
233
|
-
unless @
|
258
|
+
unless @win
|
234
259
|
raise NotImplementedError, "not supported on this platform"
|
235
260
|
end
|
236
261
|
|
@@ -244,7 +269,7 @@ class Pathname < String
|
|
244
269
|
#
|
245
270
|
# For example, Pathname.new("C:\\foo").drive_number would return 2.
|
246
271
|
def drive_number
|
247
|
-
unless @
|
272
|
+
unless @win
|
248
273
|
raise NotImplementedError, "not supported on this platform"
|
249
274
|
end
|
250
275
|
|
@@ -277,7 +302,7 @@ class Pathname < String
|
|
277
302
|
return self if string == "."
|
278
303
|
|
279
304
|
# Use the builtin PathAppend method if on Windows - much easier
|
280
|
-
if @
|
305
|
+
if @win
|
281
306
|
buf = 0.chr * MAX_PATH
|
282
307
|
buf[0..self.length-1] = self
|
283
308
|
@@PathAppend.call(buf, string << 0.chr)
|
@@ -291,7 +316,7 @@ class Pathname < String
|
|
291
316
|
array = self.to_a + string.to_a
|
292
317
|
new_string = array.join(@sep)
|
293
318
|
|
294
|
-
unless self.relative? || @
|
319
|
+
unless self.relative? || @win
|
295
320
|
new_string = @sep + new_string # Add root path back if needed
|
296
321
|
end
|
297
322
|
|
@@ -305,7 +330,7 @@ class Pathname < String
|
|
305
330
|
|
306
331
|
# Returns whether or not the path is a relative path.
|
307
332
|
def relative?
|
308
|
-
if @
|
333
|
+
if @win
|
309
334
|
@@PathIsRelative.call(self) > 0
|
310
335
|
else
|
311
336
|
root == "."
|
@@ -316,7 +341,7 @@ class Pathname < String
|
|
316
341
|
def clean
|
317
342
|
return self if self.empty?
|
318
343
|
|
319
|
-
if @
|
344
|
+
if @win
|
320
345
|
path = 0.chr * MAX_PATH
|
321
346
|
if @@PathCanonicalize.call(path, self) > 0
|
322
347
|
return Pathname.new(path.split(0.chr).first)
|
data/test/tc_pathname.rb
CHANGED
@@ -14,6 +14,9 @@ class TC_Pathname < Test::Unit::TestCase
|
|
14
14
|
def setup
|
15
15
|
@abs_path = Pathname.new("/usr/local/bin")
|
16
16
|
@rel_path = Pathname.new("usr/local/bin")
|
17
|
+
|
18
|
+
@abs_array = []
|
19
|
+
@rel_array = []
|
17
20
|
end
|
18
21
|
|
19
22
|
# Convenience method for test_plus
|
@@ -33,45 +36,45 @@ class TC_Pathname < Test::Unit::TestCase
|
|
33
36
|
end
|
34
37
|
|
35
38
|
def test_version
|
36
|
-
assert_equal("1.2.
|
39
|
+
assert_equal("1.2.1", Pathname::VERSION)
|
37
40
|
end
|
38
41
|
|
39
42
|
def test_ascend
|
40
43
|
assert_respond_to(@abs_path, :ascend)
|
41
44
|
assert_nothing_raised{ @abs_path.ascend{} }
|
42
45
|
|
43
|
-
abs_array
|
44
|
-
rel_array
|
45
|
-
@abs_path.ascend{ |path| abs_array.push(path) }
|
46
|
-
@rel_path.ascend{ |path| rel_array.push(path) }
|
46
|
+
@abs_path.ascend{ |path| @abs_array.push(path) }
|
47
|
+
@rel_path.ascend{ |path| @rel_array.push(path) }
|
47
48
|
|
48
|
-
assert_equal("/usr/local/bin", abs_array[0])
|
49
|
-
assert_equal("/usr/local", abs_array[1])
|
50
|
-
assert_equal("/usr", abs_array[2])
|
51
|
-
assert_equal("/", abs_array[3])
|
49
|
+
assert_equal("/usr/local/bin", @abs_array[0])
|
50
|
+
assert_equal("/usr/local", @abs_array[1])
|
51
|
+
assert_equal("/usr", @abs_array[2])
|
52
|
+
assert_equal("/", @abs_array[3])
|
53
|
+
assert_equal(4, @abs_array.length)
|
52
54
|
|
53
|
-
assert_equal("usr/local/bin", rel_array[0])
|
54
|
-
assert_equal("usr/local", rel_array[1])
|
55
|
-
assert_equal("usr", rel_array[2])
|
55
|
+
assert_equal("usr/local/bin", @rel_array[0])
|
56
|
+
assert_equal("usr/local", @rel_array[1])
|
57
|
+
assert_equal("usr", @rel_array[2])
|
58
|
+
assert_equal(3, @rel_array.length)
|
56
59
|
end
|
57
60
|
|
58
61
|
def test_descend
|
59
62
|
assert_respond_to(@abs_path, :descend)
|
60
63
|
assert_nothing_raised{ @abs_path.descend{} }
|
61
64
|
|
62
|
-
abs_array
|
63
|
-
rel_array
|
64
|
-
@abs_path.descend{ |path| abs_array.push(path) }
|
65
|
-
@rel_path.descend{ |path| rel_array.push(path) }
|
65
|
+
@abs_path.descend{ |path| @abs_array.push(path) }
|
66
|
+
@rel_path.descend{ |path| @rel_array.push(path) }
|
66
67
|
|
67
|
-
assert_equal("/", abs_array[0])
|
68
|
-
assert_equal("/usr", abs_array[1])
|
69
|
-
assert_equal("/usr/local", abs_array[2])
|
70
|
-
assert_equal("/usr/local/bin", abs_array[3])
|
68
|
+
assert_equal("/", @abs_array[0])
|
69
|
+
assert_equal("/usr", @abs_array[1])
|
70
|
+
assert_equal("/usr/local", @abs_array[2])
|
71
|
+
assert_equal("/usr/local/bin", @abs_array[3])
|
72
|
+
assert_equal(4, @abs_array.length)
|
71
73
|
|
72
|
-
assert_equal("usr", rel_array[0])
|
73
|
-
assert_equal("usr/local", rel_array[1])
|
74
|
-
assert_equal("usr/local/bin", rel_array[2])
|
74
|
+
assert_equal("usr", @rel_array[0])
|
75
|
+
assert_equal("usr/local", @rel_array[1])
|
76
|
+
assert_equal("usr/local/bin", @rel_array[2])
|
77
|
+
assert_equal(3, @rel_array.length)
|
75
78
|
end
|
76
79
|
|
77
80
|
def test_unc
|
@@ -227,5 +230,9 @@ class TC_Pathname < Test::Unit::TestCase
|
|
227
230
|
|
228
231
|
def teardown
|
229
232
|
@abs_path = nil
|
233
|
+
@rel_path = nil
|
234
|
+
|
235
|
+
@abs_array.clear
|
236
|
+
@rel_array.clear
|
230
237
|
end
|
231
238
|
end
|
data/test/tc_pathname_win.rb
CHANGED
@@ -24,10 +24,11 @@ class TC_Pathname < Test::Unit::TestCase
|
|
24
24
|
|
25
25
|
@abs_array = []
|
26
26
|
@rel_array = []
|
27
|
+
@unc_array = []
|
27
28
|
end
|
28
29
|
|
29
30
|
def test_version
|
30
|
-
assert_equal("1.2.
|
31
|
+
assert_equal("1.2.1", Pathname::VERSION)
|
31
32
|
end
|
32
33
|
|
33
34
|
# Convenience method for test_plus
|
@@ -57,6 +58,7 @@ class TC_Pathname < Test::Unit::TestCase
|
|
57
58
|
|
58
59
|
@bpath.descend{ |path| @abs_array.push(path) }
|
59
60
|
@npath.descend{ |path| @rel_array.push(path) }
|
61
|
+
@upath.descend{ |path| @unc_array.push(path) }
|
60
62
|
|
61
63
|
assert_equal("C:", @abs_array[0])
|
62
64
|
assert_equal("C:\\Program Files", @abs_array[1])
|
@@ -66,6 +68,9 @@ class TC_Pathname < Test::Unit::TestCase
|
|
66
68
|
assert_equal("foo", @rel_array[0])
|
67
69
|
assert_equal("foo\\bar", @rel_array[1])
|
68
70
|
assert_equal("foo\\bar\\baz", @rel_array[2])
|
71
|
+
|
72
|
+
assert_equal("\\\\foo\\bar", @unc_array[0])
|
73
|
+
assert_equal("\\\\foo\\bar\\baz", @unc_array[1])
|
69
74
|
end
|
70
75
|
|
71
76
|
def test_ascend
|
@@ -74,15 +79,22 @@ class TC_Pathname < Test::Unit::TestCase
|
|
74
79
|
|
75
80
|
@bpath.ascend{ |path| @abs_array.push(path) }
|
76
81
|
@npath.ascend{ |path| @rel_array.push(path) }
|
82
|
+
@upath.ascend{ |path| @unc_array.push(path) }
|
77
83
|
|
78
84
|
assert_equal("C:\\Program Files\\Windows NT\\Accessories", @abs_array[0])
|
79
85
|
assert_equal("C:\\Program Files\\Windows NT", @abs_array[1])
|
80
86
|
assert_equal("C:\\Program Files", @abs_array[2])
|
81
87
|
assert_equal("C:", @abs_array[3])
|
88
|
+
assert_equal(4, @abs_array.length)
|
82
89
|
|
83
90
|
assert_equal("foo\\bar\\baz", @rel_array[0])
|
84
91
|
assert_equal("foo\\bar", @rel_array[1])
|
85
|
-
assert_equal("foo", @rel_array[2])
|
92
|
+
assert_equal("foo", @rel_array[2])
|
93
|
+
assert_equal(3, @rel_array.length)
|
94
|
+
|
95
|
+
assert_equal("\\\\foo\\bar\\baz", @unc_array[0])
|
96
|
+
assert_equal("\\\\foo\\bar", @unc_array[1])
|
97
|
+
assert_equal(2, @unc_array.length)
|
86
98
|
end
|
87
99
|
|
88
100
|
# Fails due to PathIsURL for some reason
|
@@ -273,5 +285,6 @@ class TC_Pathname < Test::Unit::TestCase
|
|
273
285
|
|
274
286
|
@abs_array.clear
|
275
287
|
@rel_array.clear
|
288
|
+
@unc_array.clear
|
276
289
|
end
|
277
290
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: pathname2
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.2.
|
7
|
-
date: 2005-
|
6
|
+
version: 1.2.1
|
7
|
+
date: 2005-09-01 00:00:00 -06:00
|
8
8
|
summary: An alternate implementation of the Pathname class
|
9
9
|
require_paths:
|
10
10
|
- lib
|