Skip to content

Commit

Permalink
Change walkdir implementation to match Julia 0.6 and don't export it
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Feb 1, 2017
1 parent 40bc120 commit 8053b6f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Currently, the `@compat` macro supports the following syntaxes:

* `foreach`, similar to `map` but when the return value is not needed ([#13744])

* `walkdir`, returns an iterator that walks the directory tree of a directory ([#13707])
* `walkdir`/`Compat.walkdir`, returns an iterator that walks the directory tree of a directory. For compatibility with Julia 0.6- `Compat.walkdir` should be used and `walkdir` should be used for compatibility with the Julia 0.5. ([#13707])

* `allunique`, checks whether all elements in an iterable appear only once ([#15914])

Expand Down
27 changes: 15 additions & 12 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,21 @@ elseif VERSION < v"0.4.0-dev+6987"
export pipeline
end

if VERSION < v"0.5.0-dev+961"
export walkdir

if VERSION < v"0.6.0-dev.2043"
function walkdir(root; topdown=true, follow_symlinks=false, onerror=throw)
content = nothing
try
content = readdir(root)
catch err
isa(err, SystemError) || throw(err)
onerror(err)
#Need to return an empty task to skip the current root folder
return Task(()->())
# Need to return an empty closed channel to skip the current root folder
chnl = Channel(0)
close(chnl)
return chnl
end
dirs = Array(eltype(content), 0)
files = Array(eltype(content), 0)
dirs = Vector{eltype(content)}(0)
files = Vector{eltype(content)}(0)
for name in content
if isdir(joinpath(root, name))
push!(dirs, name)
Expand All @@ -175,24 +175,27 @@ if VERSION < v"0.5.0-dev+961"
end
end

function _it()
function _it(chnl)
if topdown
produce(root, dirs, files)
put!(chnl, (root, dirs, files))
end
for dir in dirs
path = joinpath(root,dir)
if follow_symlinks || !islink(path)
for (root_l, dirs_l, files_l) in walkdir(path, topdown=topdown, follow_symlinks=follow_symlinks, onerror=onerror)
produce(root_l, dirs_l, files_l)
put!(chnl, (root_l, dirs_l, files_l))
end
end
end
if !topdown
produce(root, dirs, files)
put!(chnl, (root, dirs, files))
end
end
Task(_it)

return Channel(_it)
end
else
import Base.walkdir
end

function rewrite_show(ex)
Expand Down
38 changes: 19 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -983,29 +983,29 @@ cd(dirwalk) do
follow_symlink_vec = has_symlinks ? [true, false] : [false]
has_symlinks && symlink(abspath("sub_dir2"), joinpath("sub_dir1", "link"))
for follow_symlinks in follow_symlink_vec
task = walkdir(".", follow_symlinks=follow_symlinks)
root, dirs, files = consume(task)
chnl = Compat.walkdir(".", follow_symlinks=follow_symlinks)
root, dirs, files = take!(chnl)
@test root == "."
@test dirs == ["sub_dir1", "sub_dir2"]
@test files == ["file1", "file2"]

root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
@test root == joinpath(".", "sub_dir1")
@test dirs == (has_symlinks ? ["link", "subsub_dir1", "subsub_dir2"] : ["subsub_dir1", "subsub_dir2"])
@test files == ["file1", "file2"]

root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
if follow_symlinks
@test root == joinpath(".", "sub_dir1", "link")
@test dirs == []
@test files == ["file_dir2"]
root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
end
for i=1:2
@test root == joinpath(".", "sub_dir1", "subsub_dir$i")
@test dirs == []
@test files == []
root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
end

@test root == joinpath(".", "sub_dir2")
Expand All @@ -1014,51 +1014,51 @@ cd(dirwalk) do
end

for follow_symlinks in follow_symlink_vec
task = walkdir(".", follow_symlinks=follow_symlinks, topdown=false)
root, dirs, files = consume(task)
chnl = Compat.walkdir(".", follow_symlinks=follow_symlinks, topdown=false)
root, dirs, files = take!(chnl)
if follow_symlinks
@test root == joinpath(".", "sub_dir1", "link")
@test dirs == []
@test files == ["file_dir2"]
root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
end
for i=1:2
@test root == joinpath(".", "sub_dir1", "subsub_dir$i")
@test dirs == []
@test files == []
root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
end
@test root == joinpath(".", "sub_dir1")
@test dirs == (has_symlinks ? ["link", "subsub_dir1", "subsub_dir2"] : ["subsub_dir1", "subsub_dir2"])
@test files == ["file1", "file2"]

root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
@test root == joinpath(".", "sub_dir2")
@test dirs == []
@test files == ["file_dir2"]

root, dirs, files = consume(task)
root, dirs, files = take!(chnl)
@test root == "."
@test dirs == ["sub_dir1", "sub_dir2"]
@test files == ["file1", "file2"]
end
#test of error handling
task_error = walkdir(".")
task_noerror = walkdir(".", onerror=x->x)
root, dirs, files = consume(task_error)
chnl_error = Compat.walkdir(".")
chnl_noerror = Compat.walkdir(".", onerror=x->x)
root, dirs, files = take!(chnl_error)
@test root == "."
@test dirs == ["sub_dir1", "sub_dir2"]
@test files == ["file1", "file2"]

rm(joinpath("sub_dir1"), recursive=true)
@test_throws SystemError consume(task_error) # throws an error because sub_dir1 do not exist
@test_throws SystemError take!(chnl_error) # throws an error because sub_dir1 do not exist

root, dirs, files = consume(task_noerror)
root, dirs, files = take!(chnl_noerror)
@test root == "."
@test dirs == ["sub_dir1", "sub_dir2"]
@test files == ["file1", "file2"]

root, dirs, files = consume(task_noerror) # skips sub_dir1 as it no longer exist
root, dirs, files = take!(chnl_noerror) # skips sub_dir1 as it no longer exist
@test root == joinpath(".", "sub_dir2")
@test dirs == []
@test files == ["file_dir2"]
Expand Down Expand Up @@ -1192,7 +1192,7 @@ let x = rand(3), y = rand(3)
@test @compat(sin.(cos.(x))) == map(x -> sin(cos(x)), x)
@test @compat(atan2.(sin.(y),x)) == broadcast(atan2,map(sin,y),x)
end
let x0 = Array(Float64), v, v0
let x0 = Array{Float64}(), v, v0
x0[1] = rand()
v0 = @compat sin.(x0)
@test isa(v0, Array{Float64,0})
Expand Down

0 comments on commit 8053b6f

Please sign in to comment.