pathname2 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,6 @@
1
+ == 1.4.3 - 3-Mar-2006
2
+ * Added the Pathname#realpath method.
3
+
1
4
  == 1.4.2 - 22-Feb-2006
2
5
  * Fixed the Pathname#relative_path_from method for Windows. This really only
3
6
  affected edge cases that you probably never saw anyway.
data/lib/pathname2.rb CHANGED
@@ -6,8 +6,8 @@
6
6
  # All functionality from File, FileTest, and Dir is included, using a facade
7
7
  # pattern.
8
8
  #
9
- # This class works on both Unix and Win32, including UNC path names. Note
10
- # that forward slashes are converted to backslashes on Win32 systems.
9
+ # This class works on both Unix and Windows, including UNC path names. Note
10
+ # that forward slashes are converted to backslashes on Windows systems.
11
11
  #
12
12
  # == Usage
13
13
  #
@@ -20,7 +20,7 @@
20
20
  # path1 + path2 # "/foo/bar/zap"
21
21
  # path1.dirname # "/foo/bar"
22
22
  #
23
- # # Win32
23
+ # # Windows
24
24
  # path1 = Pathname.new("C:\\foo\\bar\\baz")
25
25
  # path2 = Pathname.new("..\\zap")
26
26
  #
@@ -34,7 +34,7 @@
34
34
  # imperator on IRC (irc.freenode.net)
35
35
  #
36
36
  # == Copyright
37
- # Copyright (c) 2005 Daniel J. Berger.
37
+ # Copyright (c) 2005-2006 Daniel J. Berger.
38
38
  # Licensed under the same terms as Ruby itself.
39
39
  #
40
40
  require "facade"
@@ -48,41 +48,41 @@ class Pathname < String
48
48
 
49
49
  alias :_plus_ :+ # Used to prevent infinite loops in some cases
50
50
 
51
- if PLATFORM.match("mswin")
52
- require "Win32API"
53
- @@PathStripToRoot = Win32API.new("shlwapi","PathStripToRoot","P","L")
54
- @@PathIsUNC = Win32API.new("shlwapi","PathIsUNC","P","L")
55
- @@PathIsURL = Win32API.new("shlwapi","PathIsURL","P","L")
56
- @@PathCanonicalize = Win32API.new("shlwapi","PathCanonicalize","PP","L")
57
- @@PathAppend = Win32API.new("shlwapi","PathAppend","PP","L")
58
- @@PathIsRoot = Win32API.new("shlwapi","PathIsRoot","P","L")
59
- @@PathIsDirectory = Win32API.new("shlwapi","PathIsDirectory","P","L")
60
- @@PathIsRelative = Win32API.new("shlwapi","PathIsRelative","P","L")
61
- @@PathFileExists = Win32API.new("shlwapi","PathFileExists","P","L")
62
- @@PathUndecorate = Win32API.new("shlwapi","PathUndecorate","P","L")
51
+ if RUBY_PLATFORM.match('mswin')
52
+ require 'Win32API'
53
+ @@PathStripToRoot = Win32API.new('shlwapi', 'PathStripToRoot', 'P', 'L')
54
+ @@PathIsUNC = Win32API.new('shlwapi', 'PathIsUNC', 'P', 'L')
55
+ @@PathIsURL = Win32API.new('shlwapi', 'PathIsURL', 'P', 'L')
56
+ @@PathCanonicalize = Win32API.new('shlwapi', 'PathCanonicalize','PP','L')
57
+ @@PathAppend = Win32API.new('shlwapi', 'PathAppend', 'PP', 'L')
58
+ @@PathIsRoot = Win32API.new('shlwapi', 'PathIsRoot', 'P', 'L')
59
+ @@PathIsDirectory = Win32API.new('shlwapi', 'PathIsDirectory', 'P', 'L')
60
+ @@PathIsRelative = Win32API.new('shlwapi', 'PathIsRelative', 'P', 'L')
61
+ @@PathFileExists = Win32API.new('shlwapi', 'PathFileExists', 'P', 'L')
62
+ @@PathUndecorate = Win32API.new('shlwapi', 'PathUndecorate', 'P', 'L')
63
63
 
64
64
  @@GetShortPathName =
65
- Win32API.new("kernel32","GetShortPathName","PPL","L")
65
+ Win32API.new('kernel32', 'GetShortPathName', 'PPL', 'L')
66
66
 
67
67
  @@GetLongPathName =
68
- Win32API.new("kernel32","GetLongPathName","PPL","L")
68
+ Win32API.new('kernel32', 'GetLongPathName', 'PPL', 'L')
69
69
 
70
70
  @@PathGetDriveNumber =
71
- Win32API.new("shlwapi","PathGetDriveNumber","P","L")
71
+ Win32API.new('shlwapi', 'PathGetDriveNumber', 'P', 'L')
72
72
 
73
73
  @@PathCreateFromUrl =
74
- Win32API.new("shlwapi", "PathCreateFromUrl", "PPPL", "L")
74
+ Win32API.new('shlwapi', 'PathCreateFromUrl', 'PPPL', 'L')
75
75
 
76
76
  @@PathRemoveBackslash =
77
- Win32API.new("shlwapi", "PathRemoveBackslash", "P", "P")
77
+ Win32API.new('shlwapi', 'PathRemoveBackslash', 'P', 'P')
78
78
  end
79
79
 
80
- VERSION = "1.4.2"
80
+ VERSION = '1.4.3'
81
81
  MAX_PATH = 260
82
82
 
83
83
  # Creates and returns a new Pathname object.
84
84
  #
85
- # On Win32 systems, all forward slashes are replaced with backslashes.
85
+ # On Windows systems, all forward slashes are replaced with backslashes.
86
86
  def initialize(path)
87
87
  if path.length > MAX_PATH
88
88
  msg = "string too long. maximum string length is " + MAX_PATH.to_s
@@ -90,7 +90,7 @@ class Pathname < String
90
90
  end
91
91
 
92
92
  @sep = File::ALT_SEPARATOR || File::SEPARATOR
93
- @win = PLATFORM.match("mswin")
93
+ @win = RUBY_PLATFORM.match('mswin')
94
94
 
95
95
  # Handle File URL's for Windows
96
96
  if @win
@@ -109,6 +109,16 @@ class Pathname < String
109
109
  path = path.tr("/", @sep) if @win
110
110
  super(path)
111
111
  end
112
+
113
+ # Returns a real (absolute) pathname of +self+ in the actual filesystem.
114
+ #
115
+ # Unlike most Pathname methods, this one assumes that the path actually
116
+ # exists on your filesystem. If it doesn't, an error is raised.
117
+ def realpath
118
+ pwd = Pathname.new(Dir.pwd)
119
+ File.stat(self) # Check to ensure that the path exists
120
+ pwd + self
121
+ end
112
122
 
113
123
  # Returns the children of the directory, files and subdirectories, as an
114
124
  # array of Pathname objects. If you set +with_directory+ to +false+, then
@@ -132,7 +142,7 @@ class Pathname < String
132
142
  result
133
143
  end
134
144
 
135
- # Win32 only
145
+ # Windows only
136
146
  #
137
147
  # Removes the decoration from a path string. For example,
138
148
  # C:\Path\File[5].txt would become C:\Path\File.txt. Non-destructive.
@@ -146,7 +156,7 @@ class Pathname < String
146
156
  Pathname.new(buf.split(0.chr).first)
147
157
  end
148
158
 
149
- # Win32 only
159
+ # Windows only
150
160
  #
151
161
  # Performs the substitution of Pathname#undecorate in place.
152
162
  def undecorate!
@@ -160,7 +170,7 @@ class Pathname < String
160
170
  self
161
171
  end
162
172
 
163
- # Win32 only
173
+ # Windows only
164
174
  #
165
175
  # Returns the short path for a long path name. For example,
166
176
  # C:\Program Files\Java would return C:\Progra~1\Java.
@@ -174,7 +184,7 @@ class Pathname < String
174
184
  Pathname.new(buf.split(0.chr).first)
175
185
  end
176
186
 
177
- # Win32 only
187
+ # Windows only
178
188
  #
179
189
  # Returns the long path for a long path name. For example,
180
190
  # C:\Progra~1\Java would return C:\Program Files\Java.
@@ -333,7 +343,7 @@ class Pathname < String
333
343
  end
334
344
  end
335
345
 
336
- # Win32 only
346
+ # Windows only
337
347
  #
338
348
  # Determines if the string is a valid Universal Naming Convention (UNC)
339
349
  # for a server and share path.
@@ -345,7 +355,7 @@ class Pathname < String
345
355
  @@PathIsUNC.call(self) > 0
346
356
  end
347
357
 
348
- # Win32 only
358
+ # Windows only
349
359
  #
350
360
  # Returns the drive number that corresponds to the root, or nil if not
351
361
  # applicable.
data/test/tc_pathname.rb CHANGED
@@ -24,6 +24,7 @@ require "test/unit"
24
24
 
25
25
  class TC_Pathname < Test::Unit::TestCase
26
26
  def setup
27
+ @pwd = Dir.pwd
27
28
  @abs_path = Pathname.new("/usr/local/bin")
28
29
  @rel_path = Pathname.new("usr/local/bin")
29
30
  @trl_path = Pathname.new("/usr/local/bin/")
@@ -71,7 +72,13 @@ class TC_Pathname < Test::Unit::TestCase
71
72
  end
72
73
 
73
74
  def test_version
74
- assert_equal("1.4.2", Pathname::VERSION)
75
+ assert_equal("1.4.3", Pathname::VERSION)
76
+ end
77
+
78
+ def test_realpath
79
+ assert_respond_to(@abs_path, :realpath)
80
+ assert_equal(@pwd, Pathname.new('.').realpath)
81
+ assert_raises(Errno::ENOENT){ Pathname.new('../bogus').realpath }
75
82
  end
76
83
 
77
84
  # These tests taken directly from Tanaka's pathname.rb. The one failure
@@ -80,6 +80,12 @@ class TC_Pathname_MSWin < Test::Unit::TestCase
80
80
  Pathname.new(to).relative_path_from(from)
81
81
  }
82
82
  end
83
+
84
+ def test_realpath
85
+ assert_respond_to(@abs_path, :realpath)
86
+ assert_equal(@cur_path, Pathname.new('.').realpath)
87
+ assert_raises(Errno::ENOENT){ Pathname.new('../bogus').realpath }
88
+ end
83
89
 
84
90
  def test_relative_path_from
85
91
  assert_relpath("..\\a", "a", "b")
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.4.2
7
- date: 2006-02-22 00:00:00 -07:00
6
+ version: 1.4.3
7
+ date: 2006-03-03 00:00:00 -07:00
8
8
  summary: An alternate implementation of the Pathname class
9
9
  require_paths:
10
10
  - lib