From 8832fa825a8fb02447c63006f158ceca30bd6514 Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Thu, 5 Sep 2019 12:13:32 -0400 Subject: [PATCH] readdir: default to "." or pwd() depending on join keyword 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. --- base/file.jl | 3 ++- test/file.jl | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/base/file.jl b/base/file.jl index ccfb5783bc8bc..702a9c8cf87cc 100644 --- a/base/file.jl +++ b/base/file.jl @@ -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, ())) @@ -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) diff --git a/test/file.jl b/test/file.jl index b07b37ad1571d..9e0289fda7dab 100644 --- a/test/file.jl +++ b/test/file.jl @@ -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 @@ -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