Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbosity resetters #1417

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ export(quos_auto_name)
export(raw_deparse_str)
export(rep_along)
export(rep_named)
export(reset_message_verbosity)
export(reset_warning_verbosity)
export(return_from)
export(run_on_load)
export(scoped_bindings)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# rlang (development version)

* New `reset_warning_verbosity()` and `reset_message_verbosity()`
functions. These reset the verbosity of messages signalled with
`warn()` and `inform()` with the `.frequency` argument. This is
useful for testing verbosity in your package (#1414).

* `check_dots_empty()` now allows trailing missing arguments (#1390).

* Calls to local functions that are not accessible through `::` or
Expand Down
26 changes: 26 additions & 0 deletions R/cnd-signal.R
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,32 @@ peek_verbosity <- function(opt, call = caller_env()) {
)
}

#' @rdname abort
#' @param id The identifying string of the condition that was supplied
#' as `.frequency_id` to `warn()` or `inform()`.
#' @export
reset_warning_verbosity <- function(id) {
reset_verbosity(id, "warning")
}
#' @rdname abort
#' @export
reset_message_verbosity <- function(id) {
reset_verbosity(id, "message")
}
reset_verbosity <- function(id, type = c("message", "warning")) {
check_string(id)
type <- arg_match(type)

env <- switch(
type,
message = message_freq_env,
warning = warning_freq_env
)
env[[id]] <- NULL

invisible(NULL)
}

message_freq <- function(message, frequency, type) {
if (is_string(frequency, "always")) {
return(chr())
Expand Down
9 changes: 9 additions & 0 deletions man/abort.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test-cnd-signal.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,23 @@ test_that("cnd_signal() sets call", {
expect_equal(cnd$call, quote(f()))
})

test_that("can reset verbosity", {
on.exit(reset_warning_verbosity("test_reset_verbosity"))

expect_warning(
warn("foo", .frequency = "once", .frequency_id = "test_reset_verbosity")
)
expect_no_warning(
warn("foo", .frequency = "once", .frequency_id = "test_reset_verbosity")
)

reset_warning_verbosity("test_reset_verbosity")

expect_warning(
warn("foo", .frequency = "once", .frequency_id = "test_reset_verbosity")
)
})


# Lifecycle ----------------------------------------------------------

Expand Down