Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,23 @@ contractuser(path::AbstractString)
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`.

On Windows, case sensitivity is applied to every part of the path except drive letters. If
`path` and `startpath` refer to different drives, the absolute path of `path` is returned.
"""
function relpath(path::String, startpath::String = ".")
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)
path_drive, path_without_drive = splitdrive(path)
startpath_drive, startpath_without_drive = splitdrive(startpath)
path_arr = split(abspath(path_without_drive), path_separator_re)
start_arr = split(abspath(startpath_without_drive), path_separator_re)
if Sys.iswindows()
lowercase(path_drive) != lowercase(startpath_drive) && return abspath(path)
end
i = 0
while i < min(length(path_arr), length(start_arr))
i += 1
Expand Down
20 changes: 20 additions & 0 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@
res = relpath(filep, startp)
idx += 1
@test res == relpath_expected_results[idx]
if Sys.iswindows()
@test relpath("e:$filep", "e:$startp") == relpath_expected_results[idx]
@test relpath("e:$filep", "E:$startp") == relpath_expected_results[idx]
@test relpath("E:$filep", "e:$startp") == relpath_expected_results[idx]
@test relpath("E:$filep", "E:$startp") == relpath_expected_results[idx]
end
end
end
# Additional cases
Expand All @@ -271,6 +277,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