Skip to content

Commit

Permalink
readdir: default to "." or pwd() depending on join keyword
Browse files Browse the repository at this point in the history
This is a pretty niche issue but it allows `readdir()` to work after the current
working direcotory has been deleted, whereas when `join=true` you need a path
which no longer exists, so `pwd()` fails and so `readdir(join=true)` also fails.
  • Loading branch information
StefanKarpinski committed Sep 6, 2019
1 parent 7cafa94 commit 8832fa8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ julia> readdir(abspath("base"), join=true)
"/home/JuliaUser/dev/julia/base/weakkeydict.jl"
```
"""
function readdir(dir::AbstractString=pwd(); join::Bool=false)
function readdir(dir::AbstractString; join::Bool=false)
# Allocate space for uv_fs_t struct
uv_readdir_req = zeros(UInt8, ccall(:jl_sizeof_uv_fs_t, Int32, ()))

Expand All @@ -759,6 +759,7 @@ function readdir(dir::AbstractString=pwd(); join::Bool=false)

return entries
end
readdir(; join::Bool=false) = readdir(join ? pwd() : ".", join=join)

"""
walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)
Expand Down
19 changes: 18 additions & 1 deletion test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ end
end
end

@testset "readir tests" begin
@testset "readdir tests" begin
(a, b) = sort(a) == sort(b)
mktempdir() do dir
d = cd(pwd, dir) # might resolve symlinks
Expand All @@ -1431,4 +1431,21 @@ end
@test readdir(b, join=true) [joinpath(b, x) for x in names]
end
end
if !Sys.iswindows()
mktempdir() do dir
cd(dir) do
d = pwd() # might resolve symlinks
@test isdir(d)
@test Base.Filesystem.samefile(d, ".")
@test isempty(readdir())
@test isempty(readdir(d))
@test isempty(readdir(join=true))
rm(d, recursive=true)
@test !ispath(d)
@test isempty(readdir())
@test_throws SystemError readdir(d)
@test_throws Base.IOError readdir(join=true)
end
end
end
end

0 comments on commit 8832fa8

Please sign in to comment.