Skip to content

Commit

Permalink
Only display lifecycle footer in interactive sessions
Browse files Browse the repository at this point in the history
Follow-up to #132 for snapshot compatibility.
  • Loading branch information
lionel- committed Aug 22, 2022
1 parent 414dee1 commit 0917c68
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

S3method(cnd_footer,lifecycle_warning_deprecated)
export(badge)
export(deprecate_soft)
export(deprecate_stop)
Expand Down
12 changes: 8 additions & 4 deletions R/deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,14 @@ deprecate_soft0 <- function(msg) {
}

deprecate_warn0 <- function(msg, trace = NULL, always = FALSE) {
footer <- paste_line(
if (!always) silver("This warning is displayed once every 8 hours."),
silver("Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.")
)
footer <- function(...) {
if (is_interactive()) {
c(
if (!always) silver("This warning is displayed once every 8 hours."),
silver("Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.")
)
}
}
wrn <- new_deprecated_warning(msg, trace, footer = footer)

# Record muffled warnings if testthat is running because it
Expand Down
9 changes: 2 additions & 7 deletions R/warning.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,16 @@ last_lifecycle_warnings <- function() {
warnings_env$warnings
}

new_deprecated_warning <- function(msg, trace, ...) {
new_deprecated_warning <- function(msg, trace, ..., footer = NULL) {
warning_cnd(
"lifecycle_warning_deprecated",
message = msg,
trace = trace,
footer = footer,
internal = list(...)
)
}

#' @export
cnd_footer.lifecycle_warning_deprecated <- function(cnd, ...) {
cnd$internal$footer
}


warnings_env <- env(empty_env())

init_warnings <- function() {
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/_snaps/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
Condition
Warning:
`foo()` was deprecated in lifecycle 1.0.0.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
Code
deprecate()

Expand All @@ -17,13 +15,11 @@
Condition
Warning:
`foo()` was deprecated in lifecycle 1.0.0.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
Code
deprecate(always = TRUE)
Condition
Warning:
`foo()` was deprecated in lifecycle 1.0.0.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.

# what deprecation messages are readable

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/_snaps/warning.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<warning/lifecycle_warning_deprecated>
Warning:
`trace()` was deprecated in lifecycle 1.0.0.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
---
Backtrace:
1. lifecycle::expect_deprecated(f())
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-lifecycle.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ test_that("soft-deprecation warnings are issued when called from child of global

test_that("deprecation warnings are not displayed again", {
local_options(lifecycle_verbosity = NULL)
local_interactive()

wrn <- catch_cnd(
deprecate_warn("1.0.0", "foo()", id = "once-every-8-hours-note"),
classes = "warning"
)
footer <- wrn$internal$footer
expect_true(is_string(footer) && grepl("once every 8 hours", footer))
footer <- cnd_footer(wrn)
expect_true(is_character(footer) && any(grepl("once every 8 hours", footer)))

local_options(lifecycle_verbosity = "warning")

Expand Down

0 comments on commit 0917c68

Please sign in to comment.