Skip to content

Commit

Permalink
Fix #542
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Jul 5, 2021
1 parent ec8fe15 commit e46f178
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 16 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Add a new "progress" display to the `tar_watch()` app to show verbose progress info and metadata.
* Add a new `workspace_on_error` argument of `tar_option_set()` to supersede `error = "workspace"`. Helps control workspace behavior independently of the `error` argument of `tar_target()` (#405, #533, #534, @mattwarkentin, @xinstein).
* Implement `error = "abridge"` in `tar_target()` and related functions. If a target errors out with this option, the target itself stops, any currently running targets keeps, and no new targets launch after that (#533, #534, @xinstein).
* Add a menu prompt to `tar_destroy()` which can be suppressed with `TAR_ASK = "false"` (#542, @gofford).

## Deprecations

Expand Down
24 changes: 16 additions & 8 deletions R/tar_destroy.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#' automatically get deleted except if R crashed.
#' * `"workspaces"`: compressed files in `workspaces/` in the data store with
#' the saved workspaces of targets. See [tar_workspace()] for details.
#' @param ask Logical of length 1, whether to pause with a menu prompt
#' before deleting files. To disable this menu, set the `TAR_ASK`
#' environment variable to `"false"`. `usethis::edit_r_environ()`
#' can help set environment variables.
#' @examples
#' if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) {
#' tar_dir({ # tar_dir() runs code from a temporary directory.
Expand All @@ -42,17 +46,21 @@ tar_destroy <- function(
"scratch",
"workspaces"
),
ask = NULL,
store = targets::tar_config_get("store")
) {
switch(
path <- switch(
match.arg(destroy),
all = unlink(store, recursive = TRUE),
meta = unlink(path_meta(store)),
process = unlink(path_process(store)),
progress = unlink(path_progress(store)),
objects = unlink(path_objects_dir(store), recursive = TRUE),
scratch = unlink(path_scratch_dir(store), recursive = TRUE),
workspaces = unlink(path_workspaces_dir(store), recursive = TRUE)
all = store,
meta = path_meta(store),
process = path_process(store),
progress = path_progress(store),
objects = path_objects_dir(store),
scratch = path_scratch_dir(store),
workspaces = path_workspaces_dir(store)
)
if (tar_should_delete(path = path, ask = ask)) {
unlink(path, recursive = TRUE)
}
invisible()
}
2 changes: 1 addition & 1 deletion R/tar_github_actions.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tar_github_actions <- function(
) {
tar_assert_chr(path, "path must be a character")
tar_assert_scalar(path, "path must have length 1")
if (!tar_should_overwrite(ask, path)) {
if (!tar_should_overwrite(path = path, ask = ask)) {
# covered in tests/interactive/test-tar_github_actions.R # nolint
return(invisible()) # nocov
}
Expand Down
2 changes: 1 addition & 1 deletion R/tar_script.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ tar_script <- function(
ask = NULL,
script = targets::tar_config_get("script")
) {
if (!tar_should_overwrite(ask, script)) {
if (!tar_should_overwrite(path = script, ask = ask)) {
# covered in tests/interactive/test-tar_script.R # nolint
return(invisible()) # nocov
}
Expand Down
25 changes: 22 additions & 3 deletions R/utils_ask.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
tar_should_overwrite <- function(ask = NULL, file = character(0)) {
tar_should_delete <- function(path, ask) {
prompt <- paste0("Delete ", path, "?")
tar_should_modify_path(path = path, ask = ask, prompt = prompt)
}

tar_should_overwrite <- function(path, ask) {
prompt <- paste0("Overwrite ", path, "?")
tar_should_modify_path(path = path, ask = ask, prompt = prompt)
}

tar_should_modify_path <- function(
path = character(0),
ask = NULL,
prompt = character(0)
) {
ask <- ask %|||% tar_ask_env() %|||% interactive()
tar_assert_lgl(ask)
tar_assert_scalar(ask)
if (!file.exists(file) || !ask) {
prompt <- paste(
prompt,
"(Set the TAR_ASK environment variable to \"false\"",
"to disable this menu, e.g. usethis::edit_r_environ().)"
)
if (!file.exists(path) || !ask) {
return(TRUE)
}
# This part is intrinsically interactive and cannot be covered
# in fully automated tests.
# tests/interactive/test-tar_script.R is an example of a semi-automated test
# that coveres this.
# nocov start
out <- utils::menu(c("yes", "no"), title = paste0("Overwrite ", file, "?"))
out <- utils::menu(c("yes", "no"), title = prompt)
identical(as.integer(out), 1L)
# nocov end
}
Expand Down
3 changes: 3 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ reproducibly
rerender
RL
rmarkdown
rmedicine
Rmd
RMSPE
roadmap
Expand All @@ -267,6 +268,8 @@ skippable
SLURM
SSP
Stallman
Stan
stan
Stawitz
storr
Storr
Expand Down
6 changes: 6 additions & 0 deletions man/tar_destroy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/interactive/test-tar_destroy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tar_test("Run tar_destroy() interactively.", {
tar_script()
tar_make()
old_tar_ask <- Sys.getenv("TAR_ASK")
Sys.setenv(TAR_ASK = "true")
expect_true(file.exists("_targets"))
tar_destroy() # See a menu and choose 2.
expect_true(file.exists("_targets"))
tar_destroy() # See a menu and choose 0.
expect_true(file.exists("_targets"))
tar_destroy() # See a menu and choose 1.
expect_false(file.exists("_targets"))
Sys.setenv(TAR_ASK = old_tar_ask)
})
6 changes: 3 additions & 3 deletions tests/testthat/test-utils_ask.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ tar_test("tar_ask_env()", {

tar_test("tar_should_overwrite", {
tmp <- tempfile()
expect_true(tar_should_overwrite(ask = TRUE, file = tmp))
expect_true(tar_should_overwrite(ask = FALSE, file = tmp))
expect_true(tar_should_overwrite(path = tmp, ask = TRUE))
expect_true(tar_should_overwrite(path = tmp, ask = FALSE))
file.create(tmp)
expect_true(tar_should_overwrite(ask = FALSE, file = tmp))
expect_true(tar_should_overwrite(path = tmp, ask = FALSE))
# More testing is in tests/interactive/test-tar_script.R # nolint.
})

0 comments on commit e46f178

Please sign in to comment.