From 71fa17d447a7d426956ccce8e7fd43eb930dc00a Mon Sep 17 00:00:00 2001 From: Alexander Rossell Hayes Date: Mon, 12 Feb 2024 21:34:28 -0800 Subject: [PATCH] feat(check_always): add `verbose` argument which can be used to suppress messages --- R/check_always.R | 18 ++++++++++++------ man/check_always.Rd | 9 +++++++-- tests/testthat/test-check_always.R | 12 ++++++++++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/R/check_always.R b/R/check_always.R index 2539c51..ecfb3a3 100644 --- a/R/check_always.R +++ b/R/check_always.R @@ -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 @@ -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") } diff --git a/man/check_always.Rd b/man/check_always.Rd index e08a010..beb4a63 100644 --- a/man/check_always.Rd +++ b/man/check_always.Rd @@ -5,9 +5,14 @@ \alias{uncheck_always} \title{Always check whether R output is really nice} \usage{ -check_always() +check_always(verbose = TRUE) -uncheck_always() +uncheck_always(verbose = TRUE) +} +\arguments{ +\item{verbose}{If \code{TRUE}, prints a message when \code{check_always()} and +\code{uncheck_always()} are run. +Defaults to \code{TRUE}.} } \description{ \code{check_always()} creates a \link[=addTaskCallback]{task callback function} diff --git a/tests/testthat/test-check_always.R b/tests/testthat/test-check_always.R index 3c4ae35..0200c27 100644 --- a/tests/testthat/test-check_always.R +++ b/tests/testthat/test-check_always.R @@ -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() @@ -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()