win32-xpath 1.0.2 → 1.0.3
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.
- checksums.yaml +4 -4
- data/CHANGES +11 -8
- data/MANIFEST +9 -9
- data/README +65 -54
- data/Rakefile +60 -60
- data/bench/bench_win32_xpath.rb +79 -79
- data/ext/extconf.rb +13 -4
- data/ext/win32/xpath.c +399 -369
- data/futzing/futz.rb +71 -71
- data/futzing/test.rb +90 -90
- data/test/test_win32_xpath.rb +206 -185
- data/win32-xpath.gemspec +25 -25
- metadata +3 -3
data/futzing/futz.rb
CHANGED
@@ -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
|
data/futzing/test.rb
CHANGED
@@ -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
|
data/test/test_win32_xpath.rb
CHANGED
@@ -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
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
assert_equal(
|
111
|
-
assert_equal("#{@home}/foo", File.expand_path('
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
assert_equal("
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
ENV['
|
123
|
-
ENV['
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
assert_raise(ArgumentError){ File.expand_path('~
|
139
|
-
end
|
140
|
-
|
141
|
-
test "
|
142
|
-
|
143
|
-
|
144
|
-
end
|
145
|
-
|
146
|
-
test "raises
|
147
|
-
assert_raise(
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
@
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
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
|