Skip to content

Commit 6154341

Browse files
committed
watcher() now accepts a vector of paths
1 parent b66ca2f commit 6154341

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

R/watch.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#' It is possible to set a watch on a path that does not currently exist, and it
1717
#' will be monitored once created.
1818
#'
19-
#' @param path Character path to a file, or directory to watch recursively.
20-
#' Defaults to the current working directory.
19+
#' @param path Character path to a file, or directory to watch recursively, or a
20+
#' vector of paths. Defaults to the current working directory.
2121
#' @param callback A function or formula (see [rlang::as_function]), which takes
2222
#' at least one argument. It will be called back with a character vector
2323
#' comprising the paths of all files that have changed. The default, `NULL`,

man/watcher.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/watcher.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ SEXP watcher_create(SEXP path, SEXP callback, SEXP latency) {
112112
if (fsw_add_path(handle, watch_path) != FSW_OK)
113113
watcher_error(handle, "Watcher path invalid.");
114114

115+
if (XLENGTH(path) > 1) {
116+
for (R_xlen_t i = 1; i < XLENGTH(path); i++) {
117+
if (fsw_add_path(handle, Rf_translateChar(STRING_ELT(path, i))) != FSW_OK)
118+
watcher_error(handle, "Watcher path invalid.");
119+
}
120+
}
121+
115122
if (fsw_set_latency(handle, lat) != FSW_OK)
116123
watcher_error(handle, "Watcher latency cannot be negative.");
117124

tests/testthat/test-watch.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
dir <- file.path(tempdir(), "watcher-test")
2+
dir2 <- file.path(tempdir(), "watcher-test2")
23
subdir <- file.path(dir, "文件subdir")
34
dir.create(dir)
5+
dir.create(dir2)
46
dir.create(subdir)
57

68
test_that("watcher() logs", {
@@ -24,13 +26,14 @@ test_that("watcher() logs", {
2426

2527
test_that("watcher() callbacks", {
2628
x <- 0L
27-
w <- watcher(dir, callback = ~{is.character(.x) || stop(); x <<- x + 1L}, latency = 0.2)
29+
w <- watcher(c(dir, dir2), callback = ~{is.character(.x) || stop(); x <<- x + 1L}, latency = 0.2)
2830
expect_output(print(w))
2931
expect_s3_class(w, "Watcher")
3032
expect_false(w$is_running())
3133
expect_true(w$start())
3234
expect_true(w$is_running())
3335
expect_type(w$get_path(), "character")
36+
expect_length(w$get_path(), 2L)
3437
Sys.sleep(1)
3538
file.create(file.path(subdir, "testfile"))
3639
later::run_now(1)
@@ -48,15 +51,25 @@ test_that("watcher() callbacks", {
4851
later::run_now(1)
4952
expect_gte(x, 1L)
5053
x <- 0L
54+
file.create(file.path(dir2, "français"))
55+
later::run_now(1)
56+
expect_gte(x, 1L)
57+
x <- 0L
5158
file.remove(file.path(dir, "みらいヘ"))
5259
later::run_now(1)
5360
expect_gte(x, 1L)
61+
x <- 0L
62+
file.remove(file.path(dir2, "français"))
63+
later::run_now(1)
64+
expect_gte(x, 1L)
65+
x <- 0L
5466
expect_true(w$stop())
5567
expect_false(w$is_running())
5668
rm(w)
5769
})
5870

5971
unlink(dir, recursive = TRUE, force = TRUE)
72+
unlink(dir2, recursive = TRUE, force = TRUE)
6073

6174
test_that("watcher() error handling", {
6275
expect_error(watcher(latency = -1), "Watcher latency cannot be negative.")

0 commit comments

Comments
 (0)