Skip to content

Commit

Permalink
Snapshot hint override (#1608)
Browse files Browse the repository at this point in the history
Fixes #1606
  • Loading branch information
hadley authored Apr 25, 2022
1 parent 0e03c40 commit 0f24eae
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Depends:
Imports:
brio,
callr (>= 3.5.1),
cli (>= 3.1.1),
cli (>= 3.3.0),
crayon (>= 1.3.4),
desc,
digest,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# testthat (development version)

* Minor tweaks to output for latest cli (#1606).

# testthat 3.1.3

* Package that explicitly depend on rlang in their description file
Expand Down
15 changes: 9 additions & 6 deletions R/local.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,18 @@ local_reproducible_output <- function(width = 80,
withr::local_collate("C", .local_envir = .env)
}

waldo_compare <- function(x, y, ..., x_arg = "x", y_arg = "y") {

local_reporter_output <- function(.env = parent.frame()) {
reporter <- get_reporter()
if (!is.null(reporter)) {
# Need to very carefully isolate this change to this function - can not set
# in expectation functions because part of expectation handling bubbles
# up through calling handlers, which are run before on.exit()
reporter$local_user_output()
reporter$local_user_output(.env)
}
}

waldo_compare <- function(x, y, ..., x_arg = "x", y_arg = "y") {
# Need to very carefully isolate this change to this function - can not set
# in expectation functions because part of expectation handling bubbles
# up through calling handlers, which are run before on.exit()
local_reporter_output()

waldo::compare(x, y,..., x_arg = x_arg, y_arg = y_arg)
}
Expand Down
2 changes: 2 additions & 0 deletions R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ expect_snapshot_helper <- function(lab, val,
}

snapshot_accept_hint <- function(variant, file) {
local_reporter_output()

if (is.null(variant) || variant == "_default") {
name <- file
} else {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@
Code
cat(snapshot_accept_hint("_default", "bar.R"))
Output
* Run `snapshot_accept('bar.R')` to accept the change
* Run `snapshot_review('bar.R')` to interactively review the change
* Run `]8;;rstudio:run:testthat::snapshot_accept('bar.R')snapshot_accept('bar.R')]8;;` to accept the change
* Run `]8;;rstudio:run:testthat::snapshot_review('bar.R')snapshot_review('bar.R')]8;;` to interactively review the change
Code
cat(snapshot_accept_hint("foo", "bar.R"))
Output
* Run `snapshot_accept('foo/bar.R')` to accept the change
* Run `snapshot_review('foo/bar.R')` to interactively review the change
* Run `]8;;rstudio:run:testthat::snapshot_accept('foo/bar.R')snapshot_accept('foo/bar.R')]8;;` to accept the change
* Run `]8;;rstudio:run:testthat::snapshot_review('foo/bar.R')snapshot_review('foo/bar.R')]8;;` to interactively review the change

2 changes: 2 additions & 0 deletions tests/testthat/test-snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ test_that("errors and warnings are folded", {
})

test_that("hint is informative", {
withr::local_options(cli.hyperlink = TRUE)

expect_snapshot({
cat(snapshot_accept_hint("_default", "bar.R"))
cat(snapshot_accept_hint("foo", "bar.R"))
Expand Down

0 comments on commit 0f24eae

Please sign in to comment.