Skip to content

Commit

Permalink
Return current directory without the trailing slash (JuliaLang#31417)
Browse files Browse the repository at this point in the history
  • Loading branch information
musm authored and StefanKarpinski committed Mar 25, 2019
1 parent bea863e commit c46f4bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1428,5 +1428,6 @@ Return the current working directory if run from a REPL or if evaluated by `juli
"""
macro __DIR__()
__source__.file === nothing && return nothing
return abspath(dirname(String(__source__.file)))
_dirname = dirname(String(__source__.file))
return isempty(_dirname) ? pwd() : abspath(_dirname)
end
6 changes: 5 additions & 1 deletion base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,16 @@ end
"""
dirname(path::AbstractString) -> AbstractString
Get the directory part of a path.
Get the directory part of a path. Trailing characters ('/' or '\\') in the path are
counted as part of the path.
# Examples
```jldoctest
julia> dirname("/home/myuser")
"/home"
julia> dirname("/home/myuser/")
"/home/myuser"
```
See also: [`basename`](@ref)
Expand Down
7 changes: 5 additions & 2 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ include_string_test_func = include_string(@__MODULE__, "include_string_test() =

@test isdir(@__DIR__)
@test @__DIR__() == dirname(@__FILE__)
@test !endswith(@__DIR__, Base.Filesystem.path_separator)
let exename = `$(Base.julia_cmd()) --compiled-modules=yes --startup-file=no`,
wd = sprint(show, abspath(pwd(), "")),
s_dir = sprint(show, joinpath(realpath(tempdir()), ""))
wd = sprint(show, pwd())
s_dir = sprint(show, realpath(tempdir()))
@test wd != s_dir
@test readchomp(`$exename -E "@__DIR__" -i`) == wd
@test readchomp(`$exename -E "cd(()->eval(:(@__DIR__)), $s_dir)" -i`) == s_dir
@test readchomp(`$exename -E "@__DIR__"`) == wd # non-interactive
@test !endswith(wd, Base.Filesystem.path_separator)
@test !endswith(s_dir, Base.Filesystem.path_separator)
end

# Issue #5789 and PR #13542:
Expand Down

0 comments on commit c46f4bb

Please sign in to comment.