Skip to content

Commit

Permalink
Fix test case for entr on directories
Browse files Browse the repository at this point in the history
This fixes uses of `entr` to watch directories, both when `Revise` watches
directories (`watching_files[] = false`) and when it watches
directories (`watching_files[] = true`).

Tests mirror those defined for `entr` on regular files, and check in particular that:
- file creations, modifications and deletions in the directory are correctly
  detected;
- clustered notifications trigger the callback only once;
- empty directories are correctly handled (i.e. they remain in the watch list
  even when all files in them are deleted).

Closes: timholy#470
  • Loading branch information
ffevotte committed Jun 2, 2020
1 parent 8cd9013 commit 90bed27
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Revise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ function revise_file_queued(pkgdata::PkgData, file)
dirfull, basename = splitdir(file)
stillwatching = true
while stillwatching
if !file_exists(file)
if !file_exists(file) && !isdir(file)
with_logger(SimpleLogger(stderr)) do
@warn "$file is not an existing file, Revise is not watching"
end
Expand Down
76 changes: 57 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3003,32 +3003,70 @@ do_test("entr") && @testset "entr" begin


# Watch directories (#470)
dn = joinpath(tempdir(), randtmp())
mkdir(dn)
fn = joinpath(dn, "trigger.txt")
open(fn, "w") do io
println(io, "blank")
end
counter = Ref(0)
stop = Ref(false)
try
@sync begin
@sync let
srcdir = joinpath(tempdir(), randtmp())
mkdir(srcdir)

trigger = joinpath(srcdir, "trigger.txt")

counter = Ref(0)
stop = Ref(false)

@async begin
entr([dn]) do
entr([srcdir]; pause=0.5) do
counter[] += 1
stop[] && error("stopping")
stop[] && error("stop watching directory")
end
end
sleep(mtimedelay)
touch(fn)
sleep(mtimedelay)
@test counter[] == 1
sleep(1)
@test length(readdir(srcdir)) == 0 # directory should still be empty
@test counter[] == 1 # postpone=false

# File creation
touch(trigger)
sleep(1)
@test counter[] == 2

# File modification
touch(trigger)
sleep(1)
@test counter[] == 3

# File deletion -> the directory should be empty again
rm(trigger)
sleep(1)
@test length(readdir(srcdir)) == 0
@test counter[] == 4

# Two events in quick succession (w.r.t. the `pause` argument)
touch(trigger) # creation
sleep(0.1)
touch(trigger) # modification
sleep(1)
@test counter[] == 5 # Callback should have been called only once

# Stop
stop[] = true
touch(fn)
sleep(mtimedelay)
touch(trigger)
end

# `entr` should have errored by now
@test false
catch err
while err isa CompositeException
err = err.exceptions[1]
@static if VERSION >= v"1.3.0-alpha.110"
if err isa TaskFailedException
err = err.task.exception
end
end
if err isa CapturedException
err = err.ex
end
end
catch
@test counter[] == 2
@test isa(err, ErrorException)
@test err.msg == "stop watching directory"
end
end

Expand Down

0 comments on commit 90bed27

Please sign in to comment.