From 4bdc1a85c282f2a580571cdaab1ebb11dd5351ad Mon Sep 17 00:00:00 2001 From: peter1000 Date: Tue, 21 Apr 2015 17:46:12 -0300 Subject: [PATCH] relpath is only needed for julia < 0.4- `relpath` got merged: https://github.com/JuliaLang/julia/pull/10893 replaced the Lexicon version with the final merged one. --- src/render.jl | 60 ++++++++-------- test/facts/rendering.jl | 156 ++++++++++++++++++++-------------------- 2 files changed, 104 insertions(+), 112 deletions(-) diff --git a/src/render.jl b/src/render.jl index b44c86a..bb52b40 100644 --- a/src/render.jl +++ b/src/render.jl @@ -108,41 +108,37 @@ 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), path_separator_re) + start_arr = split(abspath(startpath), 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 + pathpart = join(path_arr[i+1:findlast(x -> !isempty(x), path_arr)], path_separator) + prefix_num = findlast(x -> !isempty(x), start_arr) - i - 1 + if prefix_num >= 0 + prefix = pardir * path_separator + relpath_ = isempty(pathpart) ? + (prefix^prefix_num) * pardir : + (prefix^prefix_num) * pardir * path_separator * pathpart + else + relpath_ = pathpart + end + return isempty(relpath_) ? curdir : relpath_ 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 - end - return isempty(relpath_) ? curdir : relpath_ end ## Format-specific rendering ------------------------------------------------------------ diff --git a/test/facts/rendering.jl b/test/facts/rendering.jl index 2abc079..cfde98a 100644 --- a/test/facts/rendering.jl +++ b/test/facts/rendering.jl @@ -46,88 +46,84 @@ 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 = relpath(filep, startp) + idx += 1 + @fact res => relpath_expected_results[idx] "Excpected: $(relpath_expected_results[idx])" + end end + # Additional cases + @fact_throws ArgumentError relpath("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)", "") + @fact_throws ArgumentError relpath("", "$(sep)home$(sep)user$(sep)dir_withendsep$(sep)") 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)") - end end