Skip to content

Commit 596c024

Browse files
author
peter1000
committed
relpath is only needed for julia < 0.4-
`relpath` got merged: JuliaLang/julia#10893
1 parent 5ce3ae7 commit 596c024

File tree

2 files changed

+107
-112
lines changed

2 files changed

+107
-112
lines changed

src/render.jl

+30-31
Original file line numberDiff line numberDiff line change
@@ -108,41 +108,40 @@ if VERSION < v"0.4-"
108108
0
109109
end
110110
findlast(testf::Function, A) = findprev(testf, A, length(A))
111-
end
112111

113-
# Return a relative filepath to path either from the current directory or from an optional start directory.
114-
# This is a path computation: the filesystem is not accessed to confirm the existence or nature of path or startpath.
115-
# Inspired by python's relpath
116-
function relpath(path::ByteString, startpath::ByteString = ".")
117-
isempty(path) && throw(ArgumentError("`path` must be specified"))
118-
isempty(startpath) && throw(ArgumentError("`startpath` must be specified"))
119-
curdir = "."
120-
pardir = ".."
121-
path == startpath && return curdir
122-
123-
path_arr = split(abspath(path), Base.path_separator_re)
124-
start_arr = split(abspath(startpath), Base.path_separator_re)
125-
126-
i = 0
127-
while i < min(length(path_arr), length(start_arr))
128-
i += 1
129-
if path_arr[i] != start_arr[i]
130-
i -= 1
131-
break
112+
# Return a relative filepath to path either from the current directory or from an
113+
# optional start directory.
114+
function relpath(path::AbstractString, startpath::AbstractString = ".")
115+
isempty(path) && throw(ArgumentError("`path` must be specified"))
116+
isempty(startpath) && throw(ArgumentError("`startpath` must be specified"))
117+
curdir = "."
118+
pardir = ".."
119+
path == startpath && return curdir
120+
121+
path_arr = split(abspath(path), Base.path_separator_re)
122+
start_arr = split(abspath(startpath), Base.path_separator_re)
123+
124+
i = 0
125+
while i < min(length(path_arr), length(start_arr))
126+
i += 1
127+
if path_arr[i] != start_arr[i]
128+
i -= 1
129+
break
130+
end
132131
end
133-
end
134132

135-
pathpart = join(path_arr[i+1:findlast(x -> !isempty(x), path_arr)], Base.path_separator)
136-
prefix_num = findlast(x -> !isempty(x), start_arr) - i - 1
137-
if prefix_num >= 0
138-
prefix = pardir * Base.path_separator
139-
relpath_ = isempty(pathpart) ?
140-
(prefix^prefix_num) * pardir :
141-
(prefix^prefix_num) * pardir * Base.path_separator * pathpart
142-
else
143-
relpath_ = pathpart
133+
pathpart = join(path_arr[i+1:findlast(x -> !isempty(x), path_arr)], Base.path_separator)
134+
prefix_num = findlast(x -> !isempty(x), start_arr) - i - 1
135+
if prefix_num >= 0
136+
prefix = pardir * Base.path_separator
137+
relpath_ = isempty(pathpart) ?
138+
(prefix^prefix_num) * pardir :
139+
(prefix^prefix_num) * pardir * Base.path_separator * pathpart
140+
else
141+
relpath_ = pathpart
142+
end
143+
return isempty(relpath_) ? curdir : relpath_
144144
end
145-
return isempty(relpath_) ? curdir : relpath_
146145
end
147146

148147
## Format-specific rendering ------------------------------------------------------------

test/facts/rendering.jl

+77-81
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
facts("Rendering.") do
22

3-
sep = Base.path_separator
43
output = IOBuffer()
54

65
context("Query output.") do
@@ -46,88 +45,85 @@ facts("Rendering.") do
4645
end
4746
end
4847

49-
context("Testin relpath.") do
50-
filepaths = [
51-
"$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
52-
"$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
53-
"$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
54-
"$(sep)home$(sep)user$(sep)dir_withendsep$(sep)",
55-
"$(sep)home$(sep)dir2_withendsep$(sep)",
56-
"$(sep)home$(sep)test.md",
57-
"$(sep)home",
58-
# Special cases
59-
"$(sep)",
60-
"$(sep)home$(sep)$(sep)$(sep)"
61-
]
62-
63-
startpaths = [
64-
"$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)genindex.md",
65-
"$(sep)multi_docs$(sep)genindex.md",
66-
"$(sep)home$(sep)user$(sep)dir_withendsep$(sep)",
67-
"$(sep)home$(sep)dir2_withendsep$(sep)",
68-
"$(sep)home$(sep)test.md",
69-
"$(sep)home",
70-
# Special cases
71-
"$(sep)",
72-
"$(sep)home$(sep)$(sep)$(sep)"
73-
]
74-
75-
# generated with python's relpath
76-
relpath_expected_results = [
77-
"..$(sep)Lexicon.md",
78-
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
79-
"..$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
80-
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
81-
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
82-
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
83-
"home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
84-
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
85-
"..$(sep)lib$(sep)file1.md",
86-
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
87-
"..$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
88-
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
89-
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
90-
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
91-
"home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
92-
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
93-
"..$(sep)..$(sep)..$(sep)..$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
94-
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
95-
"..$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
96-
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
97-
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
98-
"user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
99-
"home$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
100-
"user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
101-
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)dir_withendsep",
102-
"..$(sep)..$(sep)home$(sep)user$(sep)dir_withendsep", ".", "..$(sep)user$(sep)dir_withendsep",
103-
"..$(sep)user$(sep)dir_withendsep", "user$(sep)dir_withendsep",
104-
"home$(sep)user$(sep)dir_withendsep", "user$(sep)dir_withendsep",
105-
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)dir2_withendsep",
106-
"..$(sep)..$(sep)home$(sep)dir2_withendsep", "..$(sep)..$(sep)dir2_withendsep", ".",
107-
"..$(sep)dir2_withendsep", "dir2_withendsep", "home$(sep)dir2_withendsep", "dir2_withendsep",
108-
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)test.md",
109-
"..$(sep)..$(sep)home$(sep)test.md", "..$(sep)..$(sep)test.md", "..$(sep)test.md", ".",
110-
"test.md", "home$(sep)test.md", "test.md", "..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..",
111-
"..$(sep)..$(sep)home", "..$(sep)..", "..", "..", ".", "home", ".",
112-
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..", "..$(sep)..",
113-
"..$(sep)..$(sep)..", "..$(sep)..", "..$(sep)..", "..", ".", "..",
114-
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..", "..$(sep)..$(sep)home",
115-
"..$(sep)..", "..", "..", ".", "home", "."
116-
]
117-
118-
idx = 0
119-
for filep in filepaths
120-
for startp in startpaths
121-
res = Lexicon.relpath(filep, startp)
122-
idx += 1
123-
@fact res => relpath_expected_results[idx] "Excpected: $(relpath_expected_results[idx])"
48+
if VERSION < v"0.4-"
49+
context("Testing relpath.") do
50+
sep = Base.path_separator
51+
filepaths = [
52+
"$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
53+
"$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
54+
"$(sep)home$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
55+
"$(sep)home$(sep)user$(sep)dir_withendsep$(sep)",
56+
"$(sep)home$(sep)dir2_withendsep$(sep)",
57+
"$(sep)home$(sep)test.md",
58+
"$(sep)home",
59+
# Special cases
60+
"$(sep)",
61+
"$(sep)home$(sep)$(sep)$(sep)"
62+
]
63+
startpaths = [
64+
"$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)genindex.md",
65+
"$(sep)multi_docs$(sep)genindex.md",
66+
"$(sep)home$(sep)user$(sep)dir_withendsep$(sep)",
67+
"$(sep)home$(sep)dir2_withendsep$(sep)",
68+
"$(sep)home$(sep)test.md",
69+
"$(sep)home",
70+
# Special cases
71+
"$(sep)",
72+
"$(sep)home$(sep)$(sep)$(sep)"
73+
]
74+
relpath_expected_results = [
75+
"..$(sep)Test1.md",
76+
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
77+
"..$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
78+
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
79+
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
80+
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
81+
"home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
82+
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)Test1.md",
83+
"..$(sep)lib$(sep)file1.md",
84+
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
85+
"..$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
86+
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
87+
"..$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
88+
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
89+
"home$(sep)user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
90+
"user$(sep).julia$(sep)Test1$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
91+
"..$(sep)..$(sep)..$(sep)..$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
92+
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
93+
"..$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
94+
"..$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
95+
"..$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
96+
"user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
97+
"home$(sep)user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
98+
"user$(sep).julia$(sep)测试2$(sep)docs$(sep)api$(sep)测试2.md",
99+
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)dir_withendsep",
100+
"..$(sep)..$(sep)home$(sep)user$(sep)dir_withendsep",".","..$(sep)user$(sep)dir_withendsep",
101+
"..$(sep)user$(sep)dir_withendsep","user$(sep)dir_withendsep",
102+
"home$(sep)user$(sep)dir_withendsep","user$(sep)dir_withendsep",
103+
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)dir2_withendsep",
104+
"..$(sep)..$(sep)home$(sep)dir2_withendsep","..$(sep)..$(sep)dir2_withendsep",".",
105+
"..$(sep)dir2_withendsep","dir2_withendsep","home$(sep)dir2_withendsep","dir2_withendsep",
106+
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)test.md","..$(sep)..$(sep)home$(sep)test.md",
107+
"..$(sep)..$(sep)test.md","..$(sep)test.md",".","test.md","home$(sep)test.md","test.md",
108+
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..","..$(sep)..$(sep)home","..$(sep)..",
109+
"..","..",".","home",".","..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..","..$(sep)..",
110+
"..$(sep)..$(sep)..","..$(sep)..","..$(sep)..","..",".","..",
111+
"..$(sep)..$(sep)..$(sep)..$(sep)..$(sep)..","..$(sep)..$(sep)home","..$(sep)..",
112+
"..","..",".","home","."
113+
]
114+
idx = 0
115+
for filep in filepaths
116+
for startp in startpaths
117+
res = Lexicon.relpath(filep, startp)
118+
idx += 1
119+
@fact res => relpath_expected_results[idx] "Excpected: $(relpath_expected_results[idx])"
120+
end
124121
end
125-
end
126-
127-
# Additional cases
128-
@fact_throws ArgumentError Lexicon.relpath("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)", "")
129-
@fact_throws ArgumentError Lexicon.relpath("", "$(sep)home$(sep)user$(sep)dir_withendsep$(sep)")
130122

123+
# Additional cases
124+
@fact_throws ArgumentError Lexicon.relpath("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)", "")
125+
@fact_throws ArgumentError Lexicon.relpath("", "$(sep)home$(sep)user$(sep)dir_withendsep$(sep)")
126+
end
131127
end
132128

133129
end

0 commit comments

Comments
 (0)