Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

splitpath: accept generic strings; more generic path tests #33012

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ julia> splitpath("/home/myuser/example.jl")
"example.jl"
```
"""
splitpath(p::AbstractString) = splitpath(String(p))

function splitpath(p::String)
drive, p = splitdrive(p)
out = String[]
Expand All @@ -244,8 +246,6 @@ function splitpath(p::String)
return out
end

joinpath(a::AbstractString) = a

"""
joinpath(parts...) -> AbstractString

Expand All @@ -260,6 +260,7 @@ julia> joinpath("/home/myuser", "example.jl")
```
"""
joinpath(a::AbstractString, b::AbstractString, c::AbstractString...) = joinpath(joinpath(a,b), c...)
joinpath(a::AbstractString) = a

function joinpath(a::String, b::String)
isabspath(b) && return b
Expand Down
252 changes: 127 additions & 125 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,42 +86,42 @@
@test relpath(S(joinpath("foo","bar")), S("foo")) == "bar"

@testset "splitpath" begin
@test splitpath(joinpath("a","b","c")) == ["a", "b", "c"]
@test splitpath(S(joinpath("a","b","c"))) == ["a", "b", "c"]
@test splitpath("") == [""]

@test splitpath(joinpath("cats are", "gr8t")) == ["cats are", "gr8t"]
@test splitpath(joinpath(" ", " ")) == [" ", " "]
@test splitpath(S(joinpath("cats are", "gr8t"))) == ["cats are", "gr8t"]
@test splitpath(S(joinpath(" ", " "))) == [" ", " "]

# Unix-style paths are understood by all systems.
@test splitpath("/a/b") == ["/", "a", "b"]
@test splitpath("/") == ["/"]
@test splitpath("a/") == ["a"]
@test splitpath("a/b/") == ["a", "b"]
@test splitpath("a.dir/b.txt") == ["a.dir", "b.txt"]
@test splitpath("///") == ["/"]
@test splitpath("///a///b///") == ["/", "a", "b"]
@test splitpath(S("/a/b")) == ["/", "a", "b"]
@test splitpath(S("/")) == ["/"]
@test splitpath(S("a/")) == ["a"]
@test splitpath(S("a/b/")) == ["a", "b"]
@test splitpath(S("a.dir/b.txt")) == ["a.dir", "b.txt"]
@test splitpath(S("///")) == ["/"]
@test splitpath(S("///a///b///")) == ["/", "a", "b"]

if Sys.iswindows()
@test splitpath("C:\\\\a\\b\\c") == ["C:\\", "a", "b", "c"]
@test splitpath("C:\\\\") == ["C:\\"]
@test splitpath("J:\\") == ["J:\\"]
@test splitpath("C:") == ["C:"]
@test splitpath("C:a") == ["C:a"]
@test splitpath("C:a\\b") == ["C:a", "b"]
@test splitpath(S("C:\\\\a\\b\\c")) == ["C:\\", "a", "b", "c"]
@test splitpath(S("C:\\\\")) == ["C:\\"]
@test splitpath(S("J:\\")) == ["J:\\"]
@test splitpath(S("C:")) == ["C:"]
@test splitpath(S("C:a")) == ["C:a"]
@test splitpath(S("C:a\\b")) == ["C:a", "b"]

@test splitpath("a\\") == ["a"]
@test splitpath("a\\\\b\\\\") == ["a","b"]
@test splitpath("a.dir\\b.txt") == ["a.dir", "b.txt"]
@test splitpath("\\a\\b\\") == ["\\", "a","b"]
@test splitpath("\\\\a\\b") == ["\\\\a\\b"] # This is actually a valid drive name in windows.
@test splitpath(S("a\\")) == ["a"]
@test splitpath(S("a\\\\b\\\\")) == ["a","b"]
@test splitpath(S("a.dir\\b.txt")) == ["a.dir", "b.txt"]
@test splitpath(S("\\a\\b\\")) == ["\\", "a","b"]
@test splitpath(S("\\\\a\\b")) == ["\\\\a\\b"] # This is actually a valid drive name in windows.

@test splitpath("/a/b\\c/d\\\\e") == ["/", "a", "b", "c", "d", "e"]
@test splitpath("/\\/\\") == ["/"]
@test splitpath("\\/\\a/\\//b") == ["\\","a","b"]
@test splitpath(S("/a/b\\c/d\\\\e")) == ["/", "a", "b", "c", "d", "e"]
@test splitpath(S("/\\/\\")) == ["/"]
@test splitpath(S("\\/\\a/\\//b")) == ["\\","a","b"]
end
end

@testset "splitting" begin
@testset "splitdir, splitdrive" begin
@test joinpath(splitdir(S(homedir()))...) == homedir()
@test string(splitdrive(S(homedir()))...) == homedir()
@test splitdrive("a\nb") == ("", "a\nb")
Expand Down Expand Up @@ -150,113 +150,115 @@
@test_broken splitext(S(".foo...")) == (".foo", "...")
@test splitext(S(".foo.bar")) == (".foo", ".bar")
end
end

@testset "isabspath" begin
@test isabspath("~") == false
@test isabspath("/") == true # on windows, this is relatively absolute
@test isabspath("A:/") == Sys.iswindows()
@test isabspath("B:\\") == Sys.iswindows()
@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("\\\\") == Sys.iswindows()
if Sys.isunix()
@test isabspath(expanduser("~")) == true
@test startswith(expanduser("~"), homedir())
else
@test expanduser("~") == "~"
@testset "isabspath" begin
@test isabspath(S("~")) == false
@test isabspath(S("/")) == true # on windows, this is relatively absolute
@test isabspath(S("A:/")) == Sys.iswindows()
@test isabspath(S("B:\\")) == Sys.iswindows()
@test isabspath(S("./")) == false
@test isabspath(S("C:")) == false
@test isabspath(S("C:.")) == false
@test isabspath(S("α:/")) == false
@test isabspath(S(".:/")) == false
#@test isabspath(S("_:/")) == false # FIXME?
#@test isabspath(S("AB:/")) == false # FIXME?
@test isabspath(S("\\\\")) == Sys.iswindows()
if Sys.isunix()
@test isabspath(S(expanduser("~"))) == true
@test startswith(expanduser(S("~")), homedir())
else
@test expanduser(S("~")) == "~"
end
end
end

@testset "relpath" begin
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]
@testset "relpath" begin
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(S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)"), "")
@test_throws ArgumentError relpath(S(""), S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)"))
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)")
test_relpath()
end

@testset "type stability" begin
@test isa(joinpath(S("a"), S("b")), String)
@test isa(joinpath(S(abspath("a")), S("b")), String)
end
test_relpath()
end
@testset "type stability" begin
@test isa(joinpath("a", "b"), String)
@test isa(joinpath(abspath("a"), "b"), String)
end

@testset "homedir" begin
var = Sys.iswindows() ? "USERPROFILE" : "HOME"
AVG_PATH = Base.Filesystem.AVG_PATH - 1 # null-termination character
Expand Down