Skip to content

Commit

Permalink
feat(check_always): add verbose argument which can be used to suppr…
Browse files Browse the repository at this point in the history
…ess messages
  • Loading branch information
rossellhayes committed Feb 13, 2024
1 parent 5bec800 commit 71fa17d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
18 changes: 12 additions & 6 deletions R/check_always.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ nice_callback <- function(expr, value, ok, visible) {
#' `uncheck_always()` disables the task callback function,
#' returning your R session to normal behavior.
#'
#' @param verbose If `TRUE`, prints a message when `check_always()` and
#' `uncheck_always()` are run.
#' Defaults to `TRUE`.
#'
#' @keywords nice
#'
#' @export
Expand All @@ -23,22 +27,24 @@ nice_callback <- function(expr, value, ok, visible) {
#'
#' uncheck_always()
#' 23 * 3
check_always <- function() {
check_always <- function(verbose = TRUE) {
if ("nice_callback" %in% getTaskCallbackNames()) {
return(message("Already checking whether all output is nice"))
if (isTRUE(verbose)) message("Already checking whether all output is nice")
return(invisible())
}

addTaskCallback(nice_callback, name = "nice_callback")
message("Now checking whether all output is nice")
if (isTRUE(verbose)) message("Now checking whether all output is nice")
}

#' @rdname check_always
#' @export
uncheck_always <- function() {
uncheck_always <- function(verbose = TRUE) {
if (!"nice_callback" %in% getTaskCallbackNames()) {
return(message("Not checking whether all output is nice"))
if (isTRUE(verbose)) message("Not checking whether all output is nice")
return(invisible())
}

removeTaskCallback("nice_callback")
message("No longer checking whether all output is nice")
if (isTRUE(verbose)) message("No longer checking whether all output is nice")
}
9 changes: 7 additions & 2 deletions man/check_always.Rd

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

12 changes: 10 additions & 2 deletions tests/testthat/test-check_always.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ test_that("uncheck_always() removes callback", {
expect_false("nice_callback" %in% getTaskCallbackNames())
})

test_that("verbose argument controls messages", {
expect_silent(check_always(verbose = FALSE))
expect_true("nice_callback" %in% getTaskCallbackNames())

expect_silent(uncheck_always(verbose = FALSE))
expect_false("nice_callback" %in% getTaskCallbackNames())
})

test_that("check_always() doesn't add duplicate callback tasks", {
expect_message(check_always())
check_always(verbose = FALSE)

callback_list <- getTaskCallbackNames()

Expand All @@ -32,7 +40,7 @@ test_that("check_always() doesn't add duplicate callback tasks", {
})

test_that("uncheck_always() does nothing if there is no task callback", {
expect_message(uncheck_always())
uncheck_always(verbose = FALSE)

callback_list <- getTaskCallbackNames()

Expand Down

0 comments on commit 71fa17d

Please sign in to comment.