Skip to content

Commit

Permalink
Fix JuliaLang#23646: relpath recognizes Windows drive letters
Browse files Browse the repository at this point in the history
  • Loading branch information
belamenso committed Nov 1, 2020
1 parent 238874c commit 2cd989e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@ function relpath(path::String, startpath::String = ".")
path == startpath && return curdir
path_arr = split(abspath(path), path_separator_re)
start_arr = split(abspath(startpath), path_separator_re)
if Sys.iswindows()
if lowercase(path_arr[1]) != lowercase(start_arr[1]) # no relative paths between drives
return abspath(path)
elseif path_arr[1] != start_arr[1] # paths are case-sensitive but cannot go above drive's root directory
path_arr[1] = start_arr[1]
end
end
i = 0
while i < min(length(path_arr), length(start_arr))
i += 1
Expand Down
14 changes: 14 additions & 0 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@
test_relpath()
end

if Sys.iswindows()
@testset "issue #23646" begin
@test lowercase(relpath("E:\\a\\b", "C:\\c")) == "e:\\a\\b"
@test lowercase(relpath("E:\\a\\b", "c:\\c")) == "e:\\a\\b"
@test lowercase(relpath("e:\\a\\b", "C:\\c")) == "e:\\a\\b"
@test lowercase(relpath("e:\\a\\b", "c:\\c")) == "e:\\a\\b"

@test relpath("C:\\a\\b", "c:\\a\\b") == "."
@test relpath("c:\\a\\b", "C:\\a\\b") == "."
@test lowercase(relpath("C:\\a\\b", "c:\\c\\d")) == "..\\..\\a\\b"
@test lowercase(relpath("c:\\a\\b", "C:\\c\\d")) == "..\\..\\a\\b"
end
end

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

0 comments on commit 2cd989e

Please sign in to comment.