From 9969c594f1be2d74be73bd340fdb69c25376391e Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 25 May 2020 10:48:47 -0500 Subject: [PATCH] Enable watching directories Fixes #470 --- src/Revise.jl | 6 ++++-- test/runtests.jl | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Revise.jl b/src/Revise.jl index ae0c63d3..d2efa8e7 100644 --- a/src/Revise.jl +++ b/src/Revise.jl @@ -143,9 +143,11 @@ You can use the return value `key` to remove the callback later to `Revise.add_callback` with `key=key`. """ function add_callback(f, files, modules=nothing; key=gensym()) + fix_trailing(path) = isdir(path) ? joinpath(path, "") : path # insert a trailing '/' if missing, see https://github.com/timholy/Revise.jl/issues/470#issuecomment-633298553 + remove_callback(key) - files = map(abspath, files) + files = map(fix_trailing, map(abspath, files)) init_watching(files) if modules !== nothing @@ -928,7 +930,7 @@ includet(file::AbstractString) = includet(Main, file) entr(f, files; postpone=false, pause=0.02) entr(f, files, modules; postpone=false, pause=0.02) -Execute `f()` whenever files listed in `files`, or code in `modules`, updates. +Execute `f()` whenever files or directories listed in `files`, or code in `modules`, updates. `entr` will process updates (and block your command line) until you press Ctrl-C. Unless `postpone` is `true`, `f()` will be executed also when calling `entr`, regardless of file changes. The `pause` is the period (in seconds) that `entr` diff --git a/test/runtests.jl b/test/runtests.jl index b805d9db..5164d34c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2949,6 +2949,35 @@ do_test("entr") && @testset "entr" begin @test isa(err, LoadError) @test err.error.msg == "stop" end + + # 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 + @async begin + entr([dn]) do + counter[] += 1 + stop[] && error("stopping") + end + end + sleep(mtimedelay) + touch(fn) + sleep(mtimedelay) + @test counter[] == 1 + stop[] = true + touch(fn) + sleep(mtimedelay) + end + catch + @test counter[] == 2 + end end const A354_result = Ref(0)