Skip to content

Commit

Permalink
relpath is only needed for julia < 0.4-
Browse files Browse the repository at this point in the history
`relpath` got merged: JuliaLang/julia#10893
  • Loading branch information
peter1000 committed Apr 21, 2015
1 parent 5ce3ae7 commit 596c024
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 112 deletions.
61 changes: 30 additions & 31 deletions src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,41 +108,40 @@ if VERSION < v"0.4-"
0
end
findlast(testf::Function, A) = findprev(testf, A, length(A))
end

# Return a relative filepath to path either from the current directory or from an optional start directory.
# This is a path computation: the filesystem is not accessed to confirm the existence or nature of path or startpath.
# Inspired by python's relpath
function relpath(path::ByteString, startpath::ByteString = ".")
isempty(path) && throw(ArgumentError("`path` must be specified"))
isempty(startpath) && throw(ArgumentError("`startpath` must be specified"))
curdir = "."
pardir = ".."
path == startpath && return curdir

path_arr = split(abspath(path), Base.path_separator_re)
start_arr = split(abspath(startpath), Base.path_separator_re)

i = 0
while i < min(length(path_arr), length(start_arr))
i += 1
if path_arr[i] != start_arr[i]
i -= 1
break
# Return a relative filepath to path either from the current directory or from an
# optional start directory.
function relpath(path::AbstractString, startpath::AbstractString = ".")
isempty(path) && throw(ArgumentError("`path` must be specified"))
isempty(startpath) && throw(ArgumentError("`startpath` must be specified"))
curdir = "."
pardir = ".."
path == startpath && return curdir

path_arr = split(abspath(path), Base.path_separator_re)
start_arr = split(abspath(startpath), Base.path_separator_re)

i = 0
while i < min(length(path_arr), length(start_arr))
i += 1
if path_arr[i] != start_arr[i]
i -= 1
break
end
end
end

pathpart = join(path_arr[i+1:findlast(x -> !isempty(x), path_arr)], Base.path_separator)
prefix_num = findlast(x -> !isempty(x), start_arr) - i - 1
if prefix_num >= 0
prefix = pardir * Base.path_separator
relpath_ = isempty(pathpart) ?
(prefix^prefix_num) * pardir :
(prefix^prefix_num) * pardir * Base.path_separator * pathpart
else
relpath_ = pathpart
pathpart = join(path_arr[i+1:findlast(x -> !isempty(x), path_arr)], Base.path_separator)
prefix_num = findlast(x -> !isempty(x), start_arr) - i - 1
if prefix_num >= 0
prefix = pardir * Base.path_separator
relpath_ = isempty(pathpart) ?
(prefix^prefix_num) * pardir :
(prefix^prefix_num) * pardir * Base.path_separator * pathpart
else
relpath_ = pathpart
end
return isempty(relpath_) ? curdir : relpath_
end
return isempty(relpath_) ? curdir : relpath_
end

## Format-specific rendering ------------------------------------------------------------
Expand Down
158 changes: 77 additions & 81 deletions test/facts/rendering.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
facts("Rendering.") do

sep = Base.path_separator
output = IOBuffer()

context("Query output.") do
Expand Down Expand Up @@ -46,88 +45,85 @@ facts("Rendering.") do
end
end

context("Testin relpath.") do
filepaths = [
"$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.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)v0.4$(sep)Lexicon$(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)"
]

# generated with python's relpath
relpath_expected_results = [
"..$(sep)Lexicon.md",
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"..$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)Lexicon.md",
"..$(sep)lib$(sep)file1.md",
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"home$(sep)user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"user$(sep).julia$(sep)v0.4$(sep)Lexicon$(sep)docs$(sep)api$(sep)lib$(sep)file1.md",
"..$(sep)..$(sep)..$(sep)..$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"..$(sep)..$(sep)home$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"..$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"..$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"home$(sep)user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"user$(sep).julia$(sep)v0.4$(sep)Docile$(sep)docs$(sep)api$(sep)Docile.md",
"..$(sep)..$(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)..$(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)..$(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)..$(sep)home", "..$(sep)..", "..", "..", ".", "home", ".",
"..$(sep)..$(sep)..$(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 = Lexicon.relpath(filep, startp)
idx += 1
@fact res => relpath_expected_results[idx] "Excpected: $(relpath_expected_results[idx])"
if VERSION < v"0.4-"
context("Testing relpath.") do
sep = Base.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 = Lexicon.relpath(filep, startp)
idx += 1
@fact res => relpath_expected_results[idx] "Excpected: $(relpath_expected_results[idx])"
end
end
end

# Additional cases
@fact_throws ArgumentError Lexicon.relpath("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)", "")
@fact_throws ArgumentError Lexicon.relpath("", "$(sep)home$(sep)user$(sep)dir_withendsep$(sep)")

# Additional cases
@fact_throws ArgumentError Lexicon.relpath("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)", "")
@fact_throws ArgumentError Lexicon.relpath("", "$(sep)home$(sep)user$(sep)dir_withendsep$(sep)")
end
end

end

0 comments on commit 596c024

Please sign in to comment.