forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.jl
199 lines (181 loc) · 9.37 KB
/
path.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# This file is a part of Julia. License is MIT: http://julialang.org/license
for S in (String, GenericString)
dir = pwd()
sep = Base.Filesystem.path_separator
@test abspath(S("foo")) == "$dir$(sep)foo"
@test abspath(S("foo"), S("bar")) == "$dir$(sep)foo$(sep)bar"
@test basename(S("foo$(sep)bar")) == "bar"
@test dirname(S("foo$(sep)bar")) == "foo"
@test expanduser(S("x")) == "x"
@test expanduser(S("~")) == (is_windows() ? "~" : homedir())
@test isabspath(S(homedir()))
@test !isabspath(S("foo"))
@test !isdirpath(S("foo"))
@test isdirpath(S("foo$sep"))
@test isdirpath(S(""))
@test isdirpath(S("."))
@test isdirpath(S(".."))
@test joinpath(S("foo")) == "foo"
@test joinpath(S("foo"), S("bar")) == "foo$(sep)bar"
@test joinpath(S("foo"), S(homedir())) == homedir()
@test joinpath(S(abspath("foo")), S(homedir())) == homedir()
@test normpath(S(joinpath("."))) == "."
@test normpath(S(joinpath(".."))) == ".."
@test normpath(S(joinpath("..","."))) == ".."
@test normpath(S(joinpath(".",".."))) == ".."
@test normpath(S(joinpath("..",".."))) == "..$(sep).."
@test normpath(S(joinpath(".","..",".."))) == "..$(sep).."
@test normpath(S(joinpath("..",".",".."))) == "..$(sep).."
@test normpath(S(joinpath("..","..","."))) == "..$(sep).."
@test normpath(S(joinpath("foo","."))) == "foo$sep"
@test normpath(S(joinpath("foo",".."))) == "."
@test normpath(S(joinpath("foo","..","."))) == "."
@test normpath(S(joinpath("foo",".",".."))) == "."
@test normpath(S(joinpath("foo","..",".."))) == ".."
@test normpath(S(joinpath("foo",".","..",".."))) == ".."
@test normpath(S(joinpath("foo","..",".",".."))) == ".."
@test normpath(S(joinpath("foo","..","..","."))) == ".."
@test normpath(S(joinpath(".","bar"))) == "bar"
@test normpath(S(joinpath("..","bar"))) == "..$(sep)bar"
@test normpath(S(joinpath("..",".","bar"))) == "..$(sep)bar"
@test normpath(S(joinpath(".","..","bar"))) == "..$(sep)bar"
@test normpath(S(joinpath("..","..","bar"))) == "..$(sep)..$(sep)bar"
@test normpath(S(joinpath(".","..","..","bar"))) == "..$(sep)..$(sep)bar"
@test normpath(S(joinpath("..",".","..","bar"))) == "..$(sep)..$(sep)bar"
@test normpath(S(joinpath("..","..",".","bar"))) == "..$(sep)..$(sep)bar"
@test normpath(S(joinpath("foo",".","bar"))) == "foo$(sep)bar"
@test normpath(S(joinpath("foo","..","bar"))) == "bar"
@test normpath(S(joinpath("foo","..",".","bar"))) == "bar"
@test normpath(S(joinpath("foo",".","..","bar"))) == "bar"
@test normpath(S(joinpath("foo","..","..","bar"))) == "..$(sep)bar"
@test normpath(S(joinpath("foo",".","..","..","bar"))) == "..$(sep)bar"
@test normpath(S(joinpath("foo","..",".","..","bar"))) == "..$(sep)bar"
@test normpath(S(joinpath("foo","..","..",".","bar"))) == "..$(sep)bar"
@test relpath(S(joinpath("foo","bar")), S("foo")) == "bar"
@test joinpath(splitdir(S(homedir()))...) == homedir()
@test string(splitdrive(S(homedir()))...) == homedir()
if is_windows()
@test splitdrive(S("\\\\servername\\hello.world\\filename.ext")) ==
("\\\\servername\\hello.world","\\filename.ext")
@test splitdrive(S("\\\\servername.com\\hello.world\\filename.ext")) ==
("\\\\servername.com\\hello.world","\\filename.ext")
@test splitdrive(S("C:\\foo\\bar")) ==
("C:","\\foo\\bar")
end
@test splitext(S("")) == ("", "")
@test splitext(S(".")) == (".", "")
@test_broken splitext(S("..")) == ("..", "")
@test_broken splitext(S("...")) == ("...", "")
@test splitext(S("foo")) == ("foo", "")
@test splitext(S("foo.")) == ("foo", ".")
@test_broken splitext(S("foo..")) == ("foo", "..")
@test_broken splitext(S("foo...")) == ("foo", "...")
@test splitext(S("foo.bar")) == ("foo", ".bar")
@test splitext(S(".foo")) == (".foo", "")
@test splitext(S(".foo.")) == (".foo", ".")
@test_broken splitext(S(".foo..")) == (".foo", "..")
@test_broken splitext(S(".foo...")) == (".foo", "...")
@test splitext(S(".foo.bar")) == (".foo", ".bar")
end
@test isabspath("~") == false
@test isabspath("/") == true # on windows, this is relatively absolute
@test isabspath("A:/") == is_windows()
@test isabspath("B:\\") == is_windows()
@test isabspath("./") == false
@test isabspath("C:") == false
@test isabspath("C:.") == false
@test isabspath("α:/") == false
@test isabspath(".:/") == false
#@test isabspath("_:/") == false # FIXME?
#@test isabspath("AB:/") == false # FIXME?
@test isabspath("\\\\") == is_windows()
if is_unix()
@test isabspath(expanduser("~")) == true
@test startswith(expanduser("~"), homedir())
else
@test expanduser("~") == "~"
end
############################################
# This section tests relpath computation. #
###########################################
function test_relpath()
sep = Base.Filesystem.path_separator
filepaths = [
"$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"$(sep)home$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"$(sep)home$(sep)user$(sep)dir_withendsep$(sep)",
"$(sep)home$(sep)dir2_withendsep$(sep)",
"$(sep)home$(sep)test.md",
"$(sep)home",
# Special cases
"$(sep)",
"$(sep)home$(sep)$(sep)$(sep)"
]
startpaths = [
"$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)genindex.md",
"$(sep)multi_docs$(sep)genindex.md",
"$(sep)home$(sep)user$(sep)dir_withendsep$(sep)",
"$(sep)home$(sep)dir2_withendsep$(sep)",
"$(sep)home$(sep)test.md",
"$(sep)home",
# Special cases
"$(sep)",
"$(sep)home$(sep)$(sep)$(sep)"
]
relpath_expected_results = [
"..$(sep)Test1.md",
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"..$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
"..$(sep)lib$(sep)file1.md",
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep)..$(sep)..$(sep)..$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"..$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"..$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"..$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"home$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)dir_withendsep",
"..$(sep)..$(sep)home$(sep)user$(sep)dir_withendsep",".","..$(sep)user$(sep)dir_withendsep",
"..$(sep)user$(sep)dir_withendsep","user$(sep)dir_withendsep",
"home$(sep)user$(sep)dir_withendsep","user$(sep)dir_withendsep",
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)dir2_withendsep",
"..$(sep)..$(sep)home$(sep)dir2_withendsep","..$(sep)..$(sep)dir2_withendsep",".",
"..$(sep)dir2_withendsep","dir2_withendsep","home$(sep)dir2_withendsep","dir2_withendsep",
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)test.md","..$(sep)..$(sep)home$(sep)test.md",
"..$(sep)..$(sep)test.md","..$(sep)test.md",".","test.md","home$(sep)test.md","test.md",
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..","..$(sep)..$(sep)home","..$(sep)..",
"..","..",".","home",".","..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..","..$(sep)..",
"..$(sep)..$(sep)..","..$(sep)..","..$(sep)..","..",".","..",
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..","..$(sep)..$(sep)home","..$(sep)..",
"..","..",".","home","."
]
idx = 0
for filep in filepaths
for startp in startpaths
res = relpath(filep, startp)
idx += 1
@test res == relpath_expected_results[idx]
end
end
# Additional cases
@test_throws ArgumentError relpath("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)", "")
@test_throws ArgumentError relpath("", "$(sep)home$(sep)user$(sep)dir_withendsep$(sep)")
end
test_relpath()
# Test type stability
@test isa(joinpath("a", "b"), String)
@test isa(joinpath(abspath("a"), "b"), String)