win32-xpath 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,71 +1,71 @@
1
- require 'win32/xpath'
2
- require 'tmpdir'
3
-
4
- #p File.expand_path("~djberge", "foo")
5
- #p File.expand_path("~/foo")
6
- #p File.expand_path("", "~djberge")
7
- #p File.expand_path("~djberge")
8
- #p File.expand_path("~djberge/foo/bar")
9
- #p File.expand_path("~bogus")
10
-
11
- ENV['HOME'] = nil
12
- ENV['USERPROFILE'] = nil
13
- ENV['HOMEDRIVE'] = "D:/"
14
- #ENV['USERPROFILE'] = "C:/bogus"
15
- p File.expand_path("~")
16
-
17
- #p File.expand_path("foo", "~")
18
- #p File.expand_path("foo", "~")
19
- #p File.expand_path("", "~")
20
- #p File.expand_path("", "~")
21
- #p File.expand_path("foo", "~/bar")
22
- #p File.expand_path("foo", "~/bar")
23
- #p File.expand_path("")
24
- #p File.expand_path("C:/foo/bar")
25
- #p File.expand_path("C:/foo/bar///")
26
- #p File.expand_path('foo', Dir.tmpdir)
27
- #p File.expand_path("C:/foo/bar", "D:/foo")
28
- #p File.expand_path("foo")
29
- #p File.expand_path("foo", "bar")
30
-
31
- #ENV['HOME'] = nil
32
- #ENV['USERPROFILE'] = nil
33
- #p File.expand_path("~")
34
-
35
- #path = "../a"
36
- #tmp = Dir.tmpdir
37
-
38
- #p path
39
- #p tmp
40
-
41
- #100.times{
42
- # File.expand_path(path, tmp)
43
- # File.expand_path('foo', tmp)
44
- #}
45
-
46
- #p path
47
- #p tmp
48
-
49
- #p File.expand_path('../a', tmp)
50
-
51
- #p File.expand_path('../a', 'foo')
52
- #p File.expand_path('../a', 'foo')
53
- #p File.expand_path('../a', 'C:/foo')
54
- #p File.expand_path('../a', 'C:/foo')
55
-
56
-
57
- =begin
58
- p File.expand_path("/foo").tainted?
59
- p File.expand_path("foo").tainted? # True
60
- p File.expand_path("foo".taint).tainted? # True
61
- p File.expand_path("C:/foo").tainted? # False
62
- p File.expand_path("C:/foo".taint).tainted? # True
63
- =end
64
-
65
- =begin
66
- p File.expand_path("~")
67
- p File.expand_path("~/foo")
68
- p File.expand_path("//foo/bar")
69
- p File.expand_path("//foo/bar//")
70
- p File.expand_path("//foo/bar//")
71
- =end
1
+ require 'win32/xpath'
2
+ require 'tmpdir'
3
+
4
+ #p File.expand_path("~djberge", "foo")
5
+ #p File.expand_path("~/foo")
6
+ #p File.expand_path("", "~djberge")
7
+ #p File.expand_path("~djberge")
8
+ #p File.expand_path("~djberge/foo/bar")
9
+ #p File.expand_path("~bogus")
10
+
11
+ ENV['HOME'] = nil
12
+ ENV['USERPROFILE'] = nil
13
+ ENV['HOMEDRIVE'] = "D:/"
14
+ #ENV['USERPROFILE'] = "C:/bogus"
15
+ p File.expand_path("~")
16
+
17
+ #p File.expand_path("foo", "~")
18
+ #p File.expand_path("foo", "~")
19
+ #p File.expand_path("", "~")
20
+ #p File.expand_path("", "~")
21
+ #p File.expand_path("foo", "~/bar")
22
+ #p File.expand_path("foo", "~/bar")
23
+ #p File.expand_path("")
24
+ #p File.expand_path("C:/foo/bar")
25
+ #p File.expand_path("C:/foo/bar///")
26
+ #p File.expand_path('foo', Dir.tmpdir)
27
+ #p File.expand_path("C:/foo/bar", "D:/foo")
28
+ #p File.expand_path("foo")
29
+ #p File.expand_path("foo", "bar")
30
+
31
+ #ENV['HOME'] = nil
32
+ #ENV['USERPROFILE'] = nil
33
+ #p File.expand_path("~")
34
+
35
+ #path = "../a"
36
+ #tmp = Dir.tmpdir
37
+
38
+ #p path
39
+ #p tmp
40
+
41
+ #100.times{
42
+ # File.expand_path(path, tmp)
43
+ # File.expand_path('foo', tmp)
44
+ #}
45
+
46
+ #p path
47
+ #p tmp
48
+
49
+ #p File.expand_path('../a', tmp)
50
+
51
+ #p File.expand_path('../a', 'foo')
52
+ #p File.expand_path('../a', 'foo')
53
+ #p File.expand_path('../a', 'C:/foo')
54
+ #p File.expand_path('../a', 'C:/foo')
55
+
56
+
57
+ =begin
58
+ p File.expand_path("/foo").tainted?
59
+ p File.expand_path("foo").tainted? # True
60
+ p File.expand_path("foo".taint).tainted? # True
61
+ p File.expand_path("C:/foo").tainted? # False
62
+ p File.expand_path("C:/foo".taint).tainted? # True
63
+ =end
64
+
65
+ =begin
66
+ p File.expand_path("~")
67
+ p File.expand_path("~/foo")
68
+ p File.expand_path("//foo/bar")
69
+ p File.expand_path("//foo/bar//")
70
+ p File.expand_path("//foo/bar//")
71
+ =end
@@ -1,90 +1,90 @@
1
- require 'ffi'
2
-
3
- class Windows
4
- extend FFI::Library
5
- typedef :ulong, :dword
6
-
7
- ffi_lib :shlwapi
8
-
9
- attach_function :PathCanonicalizeA, [:buffer_out, :string], :bool
10
- attach_function :PathStripToRootA, [:pointer], :bool
11
- attach_function :PathIsRelativeA, [:string], :bool
12
- attach_function :PathAppendA, [:buffer_out, :string], :bool
13
- attach_function :PathSkipRootA, [:string], :string
14
- attach_function :PathIsNetworkPathA, [:string], :bool
15
-
16
- ffi_lib :kernel32
17
- attach_function :GetFullPathNameA, [:string, :dword, :buffer_out, :pointer], :dword
18
-
19
- def xpath(path, dir=nil)
20
- path = path.tr("/", "\\")
21
-
22
- buf = 0.chr * 512
23
-
24
- p path
25
- p PathIsRelativeA(path)
26
-
27
- if !PathCanonicalizeA(buf, path)
28
- raise
29
- end
30
-
31
- p buf.strip
32
-
33
- #p dir
34
- #p path
35
-
36
- #regex = /\A(\w):([^\\]+)(.*)/i
37
- =begin
38
- if m = regex.match(path)
39
- drive = m.captures[0]
40
- path = m.captures[1..-1].join
41
- p drive
42
- p path
43
- end
44
- =end
45
-
46
- #p PathIsNetworkPathA(path)
47
- #p PathSkipRootA(path)
48
- #p PathIsRelativeA(path)
49
-
50
- =begin
51
- buf = 0.chr * 1024
52
-
53
- if GetFullPathNameA(path, buf.size, buf, nil) == 0
54
- raise SystemCallError.new('GetFullPathName', FFI.errno)
55
- end
56
-
57
- p buf.strip
58
- =end
59
-
60
- =begin
61
- ptr = FFI::MemoryPointer.from_string(path)
62
-
63
- p PathIsRelativeA(path)
64
-
65
- unless PathStripToRootA(ptr)
66
- raise SystemCallError.new('PathStripToRoot', FFI.errno)
67
- end
68
-
69
- p ptr.read_string
70
- =end
71
-
72
- =begin
73
- unless PathCanonicalizeA(buf, path)
74
- raise SystemCallError.new('PathCanonicalize', FFI.errno)
75
- end
76
-
77
- buf.strip
78
- =end
79
- end
80
- end
81
-
82
- if $0 == __FILE__
83
- Windows.new.xpath("/../../a", "foo")
84
- #p Windows.new.xpath("C:/foo/../bar")
85
- #p Windows.new.xpath("C:foo")
86
- #Windows.new.xpath("C:foo")
87
- #Windows.new.xpath("C:foo/bar")
88
- #Windows.new.xpath("C:/foo")
89
- #Windows.new.xpath("foo")
90
- end
1
+ require 'ffi'
2
+
3
+ class Windows
4
+ extend FFI::Library
5
+ typedef :ulong, :dword
6
+
7
+ ffi_lib :shlwapi
8
+
9
+ attach_function :PathCanonicalizeA, [:buffer_out, :string], :bool
10
+ attach_function :PathStripToRootA, [:pointer], :bool
11
+ attach_function :PathIsRelativeA, [:string], :bool
12
+ attach_function :PathAppendA, [:buffer_out, :string], :bool
13
+ attach_function :PathSkipRootA, [:string], :string
14
+ attach_function :PathIsNetworkPathA, [:string], :bool
15
+
16
+ ffi_lib :kernel32
17
+ attach_function :GetFullPathNameA, [:string, :dword, :buffer_out, :pointer], :dword
18
+
19
+ def xpath(path, dir=nil)
20
+ path = path.tr("/", "\\")
21
+
22
+ buf = 0.chr * 512
23
+
24
+ p path
25
+ p PathIsRelativeA(path)
26
+
27
+ if !PathCanonicalizeA(buf, path)
28
+ raise
29
+ end
30
+
31
+ p buf.strip
32
+
33
+ #p dir
34
+ #p path
35
+
36
+ #regex = /\A(\w):([^\\]+)(.*)/i
37
+ =begin
38
+ if m = regex.match(path)
39
+ drive = m.captures[0]
40
+ path = m.captures[1..-1].join
41
+ p drive
42
+ p path
43
+ end
44
+ =end
45
+
46
+ #p PathIsNetworkPathA(path)
47
+ #p PathSkipRootA(path)
48
+ #p PathIsRelativeA(path)
49
+
50
+ =begin
51
+ buf = 0.chr * 1024
52
+
53
+ if GetFullPathNameA(path, buf.size, buf, nil) == 0
54
+ raise SystemCallError.new('GetFullPathName', FFI.errno)
55
+ end
56
+
57
+ p buf.strip
58
+ =end
59
+
60
+ =begin
61
+ ptr = FFI::MemoryPointer.from_string(path)
62
+
63
+ p PathIsRelativeA(path)
64
+
65
+ unless PathStripToRootA(ptr)
66
+ raise SystemCallError.new('PathStripToRoot', FFI.errno)
67
+ end
68
+
69
+ p ptr.read_string
70
+ =end
71
+
72
+ =begin
73
+ unless PathCanonicalizeA(buf, path)
74
+ raise SystemCallError.new('PathCanonicalize', FFI.errno)
75
+ end
76
+
77
+ buf.strip
78
+ =end
79
+ end
80
+ end
81
+
82
+ if $0 == __FILE__
83
+ Windows.new.xpath("/../../a", "foo")
84
+ #p Windows.new.xpath("C:/foo/../bar")
85
+ #p Windows.new.xpath("C:foo")
86
+ #Windows.new.xpath("C:foo")
87
+ #Windows.new.xpath("C:foo/bar")
88
+ #Windows.new.xpath("C:/foo")
89
+ #Windows.new.xpath("foo")
90
+ end
@@ -1,185 +1,206 @@
1
- require 'test-unit'
2
- require 'tmpdir'
3
- require 'win32/xpath'
4
- require 'etc'
5
-
6
- class Test_XPath < Test::Unit::TestCase
7
- def self.startup
8
- ENV['HOME'] ||= ENV['USERPROFILE']
9
- @@login = Etc.getlogin
10
- end
11
-
12
- def setup
13
- @pwd = Dir.pwd
14
- @tmp = Dir.tmpdir
15
- @root = 'C:/'
16
- @drive = ENV['HOMEDRIVE']
17
- @home = ENV['HOME'].tr('\\', '/')
18
- @unc = "//foo/bar"
19
- end
20
-
21
- test "converts an empty pathname into absolute current pathname" do
22
- assert_equal(@pwd, File.expand_path(''))
23
- end
24
-
25
- test "converts '.' into absolute pathname using current directory" do
26
- assert_equal(@pwd, File.expand_path('.'))
27
- end
28
-
29
- test "converts 'foo' into absolute pathname using current directory" do
30
- assert_equal(File.join(@pwd, 'foo'), File.expand_path('foo'))
31
- end
32
-
33
- test "converts 'foo' into absolute pathname ignoring nil dir" do
34
- assert_equal(File.join(@pwd, 'foo'), File.expand_path('foo', nil))
35
- end
36
-
37
- test "converts 'foo' and 'bar' into absolute pathname" do
38
- assert_equal(File.join(@pwd, "bar", "foo"), File.expand_path('foo', 'bar'))
39
- end
40
-
41
- test "converts a pathname into absolute pathname" do
42
- assert_equal(File.join(@pwd, 'a'), File.expand_path('a.'))
43
- assert_equal(File.join(@pwd, '.a'), File.expand_path('.a'))
44
- assert_equal(File.join(@pwd, '..a'), File.expand_path('..a'))
45
- assert_equal(File.join(@pwd, 'a../b'), File.expand_path('a../b'))
46
- end
47
-
48
- test "converts a pathname and make it valid" do
49
- assert_equal(File.join(@pwd, 'a'), File.expand_path('a..'))
50
- end
51
-
52
- test "converts a pathname to an absolute pathname using a complete path" do
53
- assert_equal(@tmp, File.expand_path('', @tmp))
54
- assert_equal(File.join(@tmp, 'a'), File.expand_path('a', @tmp))
55
- assert_equal(File.join(@tmp, 'a'), File.expand_path('../a', "#{@tmp}/xxx"))
56
- assert_equal(@root, File.expand_path('.', @root))
57
- end
58
-
59
- test "ignores supplied dir if path contains a drive letter" do
60
- assert_equal(@root, File.expand_path(@root, "D:/"))
61
- end
62
-
63
- test "removes trailing slashes from absolute path" do
64
- assert_equal(File.join(@root, 'foo'), File.expand_path("#{@root}foo/"))
65
- assert_equal(File.join(@root, 'foo.rb'), File.expand_path("#{@root}foo.rb/"))
66
- end
67
-
68
- test "removes trailing spaces from absolute path" do
69
- assert_equal(File.join(@root, 'foo'), File.expand_path("#{@root}foo "))
70
- end
71
-
72
- test "removes trailing dots from absolute path" do
73
- assert_equal(File.join(@root, 'a'), File.expand_path("#{@root}a."))
74
- end
75
-
76
- test "converts a pathname with a drive letter but no slash" do
77
- assert_match(/\Ac:\//i, File.expand_path("c:"))
78
- end
79
-
80
- test "converts a pathname with a drive letter ignoring different drive dir" do
81
- assert_match(/\Ac:\//i, File.expand_path("c:foo", "d:/bar"))
82
- end
83
-
84
- test "converts a pathname which starts with a slash using current drive" do
85
- assert_match(/\A#{@drive}\/foo\z/i, File.expand_path('/foo'))
86
- end
87
-
88
- test "returns tainted strings or not" do
89
- assert_true(File.expand_path('foo').tainted?)
90
- assert_true(File.expand_path('foo'.taint).tainted?)
91
- assert_true(File.expand_path('/foo').tainted?)
92
- assert_true(File.expand_path('/foo'.taint).tainted?)
93
- assert_true(File.expand_path('C:/foo'.taint).tainted?)
94
- assert_false(File.expand_path('C:/foo').tainted?)
95
- assert_false(File.expand_path('//foo').tainted?)
96
- end
97
-
98
- test "converts a pathname to an absolute pathname using tilde as base" do
99
- assert_equal(@home, File.expand_path('~'))
100
- assert_equal("#{@home}/foo", File.expand_path('~/foo'))
101
- end
102
-
103
- test "converts a pathname to an absolute pathname using tilde for UNC path" do
104
- ENV['HOME'] = @unc
105
- assert_equal(@unc, File.expand_path('~'))
106
- end
107
-
108
- test "converts a tilde to path if used for dir argument" do
109
- assert_equal(@home, File.expand_path('', '~'))
110
- assert_equal("#{@home}/foo", File.expand_path('', '~/foo'))
111
- assert_equal("#{@home}/foo", File.expand_path('foo', '~'))
112
- end
113
-
114
- test "does not modify a HOME string argument" do
115
- str = "~/a"
116
- assert_equal("#{@home}/a", File.expand_path(str))
117
- assert_equal("~/a", str)
118
- end
119
-
120
- test "raises ArgumentError when HOME is nil" do
121
- ENV['HOME'] = nil
122
- ENV['USERPROFILE'] = nil
123
- ENV['HOMEDRIVE'] = nil
124
- assert_raise(ArgumentError){ File.expand_path('~') }
125
- end
126
-
127
- test "raises ArgumentError if HOME is relative" do
128
- ENV['HOME'] = '.'
129
- assert_raise(ArgumentError){ File.expand_path('~') }
130
- end
131
-
132
- test "raises ArgumentError if relative user is provided" do
133
- ENV['HOME'] = '.'
134
- assert_raise(ArgumentError){ File.expand_path('~anything') }
135
- end
136
-
137
- test "raises an ArgumentError if a bogus username is supplied" do
138
- assert_raise(ArgumentError){ File.expand_path('~anything') }
139
- end
140
-
141
- test "converts a tilde plus username as expected" do
142
- assert_equal("C:/Users/#{@@login}", File.expand_path("~#{@@login}"))
143
- assert_equal("C:/Users/#{@@login}/foo", File.expand_path("~#{@@login}/foo"))
144
- end
145
-
146
- test "raises a TypeError if not passed a string" do
147
- assert_raise(TypeError){ File.expand_path(1) }
148
- assert_raise(TypeError){ File.expand_path(nil) }
149
- assert_raise(TypeError){ File.expand_path(true) }
150
- end
151
-
152
- test "canonicalizes absolute path" do
153
- assert_equal("C:/dir", File.expand_path("C:/./dir"))
154
- end
155
-
156
- test "does not modify its argument" do
157
- str = "./a/b/../c"
158
- assert_equal("#{@home}/a/c", File.expand_path(str, @home))
159
- assert_equal("./a/b/../c", str)
160
- end
161
-
162
- test "accepts objects that have a to_path method" do
163
- klass = Class.new{ def to_path; "a/b/c"; end }
164
- obj = klass.new
165
- assert_equal("#{@pwd}/a/b/c", File.expand_path(obj))
166
- end
167
-
168
- test "works with unicode characters" do
169
- assert_equal("#{@pwd}/Ελλάσ", File.expand_path("Ελλάσ"))
170
- end
171
-
172
- def teardown
173
- @pwd = nil
174
- @unc = nil
175
- @dir = nil
176
- @root = nil
177
- @drive = nil
178
-
179
- ENV['HOME'] = @home
180
- end
181
-
182
- def self.shutdown
183
- @@login = nil
184
- end
185
- end
1
+ require 'test-unit'
2
+ require 'tmpdir'
3
+ require 'win32/xpath'
4
+ require 'etc'
5
+
6
+ class Test_XPath < Test::Unit::TestCase
7
+ def self.startup
8
+ ENV['HOME'] ||= ENV['USERPROFILE']
9
+ @@login = Etc.getlogin
10
+ end
11
+
12
+ def setup
13
+ @pwd = Dir.pwd
14
+ @tmp = Dir.tmpdir
15
+ @root = 'C:/'
16
+ @drive = ENV['HOMEDRIVE']
17
+ @home = ENV['HOME'].tr('\\', '/')
18
+ @unc = "//foo/bar"
19
+ end
20
+
21
+ test "converts an empty pathname into absolute current pathname" do
22
+ assert_equal(@pwd, File.expand_path(''))
23
+ end
24
+
25
+ test "converts '.' into absolute pathname using current directory" do
26
+ assert_equal(@pwd, File.expand_path('.'))
27
+ end
28
+
29
+ test "converts 'foo' into absolute pathname using current directory" do
30
+ assert_equal(File.join(@pwd, 'foo'), File.expand_path('foo'))
31
+ end
32
+
33
+ test "converts 'foo' into absolute pathname ignoring nil dir" do
34
+ assert_equal(File.join(@pwd, 'foo'), File.expand_path('foo', nil))
35
+ end
36
+
37
+ test "converts 'foo' and 'bar' into absolute pathname" do
38
+ assert_equal(File.join(@pwd, "bar", "foo"), File.expand_path('foo', 'bar'))
39
+ end
40
+
41
+ test "converts a pathname into absolute pathname" do
42
+ assert_equal(File.join(@pwd, 'a'), File.expand_path('a.'))
43
+ assert_equal(File.join(@pwd, '.a'), File.expand_path('.a'))
44
+ assert_equal(File.join(@pwd, '..a'), File.expand_path('..a'))
45
+ assert_equal(File.join(@pwd, 'a../b'), File.expand_path('a../b'))
46
+ end
47
+
48
+ test "converts a pathname and make it valid" do
49
+ assert_equal(File.join(@pwd, 'a'), File.expand_path('a..'))
50
+ end
51
+
52
+ test "converts a pathname to an absolute pathname using a complete path" do
53
+ assert_equal(@tmp, File.expand_path('', @tmp))
54
+ assert_equal(File.join(@tmp, 'a'), File.expand_path('a', @tmp))
55
+ assert_equal(File.join(@tmp, 'a'), File.expand_path('../a', "#{@tmp}/xxx"))
56
+ assert_equal(@root, File.expand_path('.', @root))
57
+ end
58
+
59
+ test "ignores supplied dir if path contains a drive letter" do
60
+ assert_equal(@root, File.expand_path(@root, "D:/"))
61
+ end
62
+
63
+ test "removes trailing slashes from absolute path" do
64
+ assert_equal(File.join(@root, 'foo'), File.expand_path("#{@root}foo/"))
65
+ assert_equal(File.join(@root, 'foo.rb'), File.expand_path("#{@root}foo.rb/"))
66
+ end
67
+
68
+ test "removes trailing spaces from absolute path" do
69
+ assert_equal(File.join(@root, 'foo'), File.expand_path("#{@root}foo "))
70
+ end
71
+
72
+ test "removes trailing dots from absolute path" do
73
+ assert_equal(File.join(@root, 'a'), File.expand_path("#{@root}a."))
74
+ end
75
+
76
+ test "converts a pathname with a drive letter but no slash" do
77
+ assert_match(/\Ac:\//i, File.expand_path("c:"))
78
+ end
79
+
80
+ test "converts a pathname with a drive letter ignoring different drive dir" do
81
+ assert_match(/\Ac:\//i, File.expand_path("c:foo", "d:/bar"))
82
+ end
83
+
84
+ test "converts a pathname which starts with a slash using current drive" do
85
+ assert_match(/\A#{@drive}\/foo\z/i, File.expand_path('/foo'))
86
+ end
87
+
88
+ test "returns tainted strings or not" do
89
+ assert_true(File.expand_path('foo').tainted?)
90
+ assert_true(File.expand_path('foo'.taint).tainted?)
91
+ assert_true(File.expand_path('/foo').tainted?)
92
+ assert_true(File.expand_path('/foo'.taint).tainted?)
93
+ assert_true(File.expand_path('C:/foo'.taint).tainted?)
94
+ assert_false(File.expand_path('C:/foo').tainted?)
95
+ assert_false(File.expand_path('//foo').tainted?)
96
+ end
97
+
98
+ test "converts a pathname to an absolute pathname using tilde as base" do
99
+ assert_equal(@home, File.expand_path('~'))
100
+ assert_equal("#{@home}/foo", File.expand_path('~/foo'))
101
+ assert_equal("#{@home}/.foo", File.expand_path('~/.foo'))
102
+ end
103
+
104
+ test "converts a pathname to an absolute pathname using tilde for UNC path" do
105
+ ENV['HOME'] = @unc
106
+ assert_equal(@unc, File.expand_path('~'))
107
+ end
108
+
109
+ test "converts a tilde to path if used for dir argument" do
110
+ assert_equal(@home, File.expand_path('', '~'))
111
+ assert_equal("#{@home}/foo", File.expand_path('', '~/foo'))
112
+ assert_equal("#{@home}/foo", File.expand_path('foo', '~'))
113
+ end
114
+
115
+ test "does not modify a HOME string argument" do
116
+ str = "~/a"
117
+ assert_equal("#{@home}/a", File.expand_path(str))
118
+ assert_equal("~/a", str)
119
+ end
120
+
121
+ test "defaults to HOMEDRIVE + HOMEPATH if HOME or USERPROFILE are nil" do
122
+ ENV['HOME'] = nil
123
+ ENV['USERPROFILE'] = nil
124
+ ENV['HOMEDRIVE'] = "C:"
125
+ ENV['HOMEPATH'] = "\\Users\\foo"
126
+ assert_equal("C:/Users/foo/bar", File.expand_path("~/bar"))
127
+ end
128
+
129
+ test "raises ArgumentError when HOME is nil" do
130
+ ENV['HOME'] = nil
131
+ ENV['USERPROFILE'] = nil
132
+ ENV['HOMEDRIVE'] = nil
133
+ assert_raise(ArgumentError){ File.expand_path('~') }
134
+ end
135
+
136
+ test "raises ArgumentError if HOME is relative" do
137
+ ENV['HOME'] = '.'
138
+ assert_raise(ArgumentError){ File.expand_path('~') }
139
+ end
140
+
141
+ test "raises ArgumentError if relative user is provided" do
142
+ ENV['HOME'] = '.'
143
+ assert_raise(ArgumentError){ File.expand_path('~anything') }
144
+ end
145
+
146
+ test "raises an ArgumentError if a bogus username is supplied" do
147
+ assert_raise(ArgumentError){ File.expand_path('~anything') }
148
+ end
149
+
150
+ test "converts a tilde plus username as expected" do
151
+ assert_equal("C:/Users/#{@@login}", File.expand_path("~#{@@login}"))
152
+ assert_equal("C:/Users/#{@@login}/foo", File.expand_path("~#{@@login}/foo"))
153
+ end
154
+
155
+ test "raises a TypeError if not passed a string" do
156
+ assert_raise(TypeError){ File.expand_path(1) }
157
+ assert_raise(TypeError){ File.expand_path(nil) }
158
+ assert_raise(TypeError){ File.expand_path(true) }
159
+ end
160
+
161
+ test "canonicalizes absolute path" do
162
+ assert_equal("C:/dir", File.expand_path("C:/./dir"))
163
+ end
164
+
165
+ test "does not modify its argument" do
166
+ str = "./a/b/../c"
167
+ assert_equal("#{@home}/a/c", File.expand_path(str, @home))
168
+ assert_equal("./a/b/../c", str)
169
+ end
170
+
171
+ test "accepts objects that have a to_path method" do
172
+ klass = Class.new{ def to_path; "a/b/c"; end }
173
+ obj = klass.new
174
+ assert_equal("#{@pwd}/a/b/c", File.expand_path(obj))
175
+ end
176
+
177
+ test "works with unicode characters" do
178
+ assert_equal("#{@pwd}/Ελλάσ", File.expand_path("Ελλάσ"))
179
+ end
180
+
181
+ # The +1 below is for the slash trailing @pwd that's appended.
182
+ test "handles paths longer than 260 (MAX_PATH) characters" do
183
+ assert_nothing_raised{ File.expand_path("a" * 261) }
184
+ assert_nothing_raised{ File.expand_path("a" * 1024) }
185
+ assert_equal(@pwd.size + 1024 + 1, File.expand_path("a" * 1024).size)
186
+ end
187
+
188
+ test "handles very long paths with tilde" do
189
+ path = "a * 1024"
190
+ assert_nothing_raised{ File.expand_path("~/#{path}") }
191
+ end
192
+
193
+ def teardown
194
+ @pwd = nil
195
+ @unc = nil
196
+ @dir = nil
197
+ @root = nil
198
+ @drive = nil
199
+
200
+ ENV['HOME'] = @home
201
+ end
202
+
203
+ def self.shutdown
204
+ @@login = nil
205
+ end
206
+ end