Skip to content

Commit 3424d2c

Browse files
readdir: default to "." or pwd() depending on join keyword (#33175)
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.
1 parent a3ccac0 commit 3424d2c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

base/file.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ julia> readdir(abspath("base"), join=true)
737737
"/home/JuliaUser/dev/julia/base/weakkeydict.jl"
738738
```
739739
"""
740-
function readdir(dir::AbstractString=pwd(); join::Bool=false)
740+
function readdir(dir::AbstractString; join::Bool=false)
741741
# Allocate space for uv_fs_t struct
742742
uv_readdir_req = zeros(UInt8, ccall(:jl_sizeof_uv_fs_t, Int32, ()))
743743

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

760760
return entries
761761
end
762+
readdir(; join::Bool=false) = readdir(join ? pwd() : ".", join=join)
762763

763764
"""
764765
walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)

test/file.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ end
14051405
end
14061406
end
14071407

1408-
@testset "readir tests" begin
1408+
@testset "readdir tests" begin
14091409
(a, b) = sort(a) == sort(b)
14101410
mktempdir() do dir
14111411
d = cd(pwd, dir) # might resolve symlinks
@@ -1431,4 +1431,21 @@ end
14311431
@test readdir(b, join=true) [joinpath(b, x) for x in names]
14321432
end
14331433
end
1434+
if !Sys.iswindows()
1435+
mktempdir() do dir
1436+
cd(dir) do
1437+
d = pwd() # might resolve symlinks
1438+
@test isdir(d)
1439+
@test Base.Filesystem.samefile(d, ".")
1440+
@test isempty(readdir())
1441+
@test isempty(readdir(d))
1442+
@test isempty(readdir(join=true))
1443+
rm(d, recursive=true)
1444+
@test !ispath(d)
1445+
@test isempty(readdir())
1446+
@test_throws SystemError readdir(d)
1447+
@test_throws Base.IOError readdir(join=true)
1448+
end
1449+
end
1450+
end
14341451
end

0 commit comments

Comments
 (0)