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

Fix #38491: fix an abspath() edge case on Windows #38981

Merged
merged 4 commits into from
Mar 25, 2021
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
14 changes: 13 additions & 1 deletion base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,19 @@ normpath(a::AbstractString, b::AbstractString...) = normpath(joinpath(a,b...))
Convert a path to an absolute path by adding the current directory if necessary.
Also normalizes the path as in [`normpath`](@ref).
"""
abspath(a::String) = normpath(isabspath(a) ? a : joinpath(pwd(),a))
function abspath(a::String)::String
if !Sys.iswindows()
return normpath(isabspath(a) ? a : joinpath(pwd(),a))
else
pwd_drive, _ = splitdrive(pwd())
a_drive, a_nodrive = splitdrive(a)
if a_drive != "" && lowercase(pwd_drive) != lowercase(a_drive) && !isabspath(a)
return a_drive * normpath(joinpath(path_separator, a_nodrive))
else
return normpath(isabspath(a) ? a : joinpath(pwd(),a))
end
end
musm marked this conversation as resolved.
Show resolved Hide resolved
end

"""
abspath(path::AbstractString, paths::AbstractString...) -> String
Expand Down
17 changes: 17 additions & 0 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
@test isabspath(S(homedir()))
@test !isabspath(S("foo"))
end
if Sys.iswindows()
@testset "issue #38491" begin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@testset "issue #38491" begin
@testset "issue #38491" begin withenv("=X:" => nothing, "=Y:" => nothing) do

pwd_drive = uppercase(splitdrive(pwd())[1])
drive = if (pwd_drive != "X:") "X:" else "Y:" end
musm marked this conversation as resolved.
Show resolved Hide resolved
@test abspath("$(lowercase(drive))a\\b\\c") == "$(lowercase(drive))\\a\\b\\c"
@test abspath("$(uppercase(drive))a\\b\\c") == "$(uppercase(drive))\\a\\b\\c"
@test abspath("$(lowercase(drive))a") == "$(lowercase(drive))\\a"
@test abspath("$(uppercase(drive))a") == "$(uppercase(drive))\\a"
@test abspath(lowercase(drive)) == "$(lowercase(drive))\\"
@test abspath(uppercase(drive)) == "$(uppercase(drive))\\"

@test lowercase(abspath("$(pwd_drive)a\\b\\c")) == lowercase(joinpath(pwd(), "a\\b\\c"))
@test lowercase(abspath("$(pwd_drive)a")) == lowercase(joinpath(pwd(), "a"))
@test lowercase(abspath(lowercase(pwd_drive))) == lowercase("$(pwd())\\")
@test lowercase(abspath(uppercase(pwd_drive))) == lowercase("$(pwd())\\")
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end
end; end

end
@test basename(S("foo$(sep)bar")) == "bar"
@test dirname(S("foo$(sep)bar")) == "foo"

Expand Down