Skip to content

Commit 3ac8a78

Browse files
authored
Set option when snapshotting (#1798)
Fixes #1796
1 parent e1bda60 commit 3ac8a78

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export(is_less_than)
145145
export(is_more_than)
146146
export(is_null)
147147
export(is_parallel)
148+
export(is_snapshotting)
148149
export(is_testing)
149150
export(is_true)
150151
export(it)

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# testthat (development version)
22

3+
* Experimental `is_snapshotting()` to determine if code is running inside a
4+
snapshot test (#1796).
5+
6+
* All packages, regardless of whether or not they use rlang, now
7+
use the new snapshot display for errors, warnings, and messages.
8+
39
* `skip_on_cran()` no longer skips (errors) when run interactively.
410

511
* `testthat::teardown_env()` works in more cases.

R/snapshot.R

+27-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ expect_snapshot <- function(x,
8484
cnd_class = cnd_class
8585
)
8686
}
87-
out <- verify_exec(quo_get_expr(x), quo_get_env(x), replay)
87+
with_is_snapshotting(
88+
out <- verify_exec(quo_get_expr(x), quo_get_env(x), replay)
89+
)
8890

8991
# Use expect_error() machinery to confirm that error is as expected
9092
msg <- compare_condition_3e("error", state$error, quo_label(x), error)
@@ -219,7 +221,9 @@ expect_snapshot_output <- function(x, cran = FALSE, variant = NULL) {
219221
variant <- check_variant(variant)
220222

221223
lab <- quo_label(enquo(x))
222-
val <- capture_output_lines(x, print = TRUE, width = NULL)
224+
with_is_snapshotting(
225+
val <- capture_output_lines(x, print = TRUE, width = NULL)
226+
)
223227

224228
expect_snapshot_helper(lab, val,
225229
cran = cran,
@@ -261,7 +265,9 @@ expect_snapshot_condition <- function(base_class, x, class, cran = FALSE, varian
261265
variant <- check_variant(variant)
262266

263267
lab <- quo_label(enquo(x))
264-
val <- capture_matching_condition(x, cnd_matcher(class))
268+
with_is_snapshotting(
269+
val <- capture_matching_condition(x, cnd_matcher(class))
270+
)
265271
if (is.null(val)) {
266272
if (base_class == class) {
267273
fail(sprintf("%s did not generate %s", lab, base_class))
@@ -326,6 +332,7 @@ expect_snapshot_value <- function(x,
326332
serialize = function(x) unserialize(jsonlite::base64_dec(x))
327333
)
328334

335+
with_is_snapshotting(force(x))
329336
expect_snapshot_helper(lab, x,
330337
save = save,
331338
load = load,
@@ -458,3 +465,20 @@ check_variant <- function(x) {
458465
abort("If supplied, `variant` must be a string")
459466
}
460467
}
468+
469+
with_is_snapshotting <- function(code) {
470+
withr::local_options(testthat_is_snapshotting = TRUE)
471+
code
472+
}
473+
474+
#' Is the code currently run within a snapshot test?
475+
#'
476+
#' This is often useful to compute a default argument for `quiet`. You
477+
#' often want to deliberatey suppress output for tests but still track
478+
#' it when snapshotting: `quiet = is_testing() && !is_snapshotting() `
479+
#'
480+
#' @export
481+
#' @keywords internal
482+
is_snapshotting <- function() {
483+
getOption("testthat_is_snapshotting", FALSE)
484+
}

man/is_snapshotting.Rd

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/snapshot.md

+23
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,26 @@
267267
* Run ]8;;ide:run:testthat::snapshot_accept('foo/bar.R')testthat::snapshot_accept('foo/bar.R')]8;; to accept the change.
268268
* Run ]8;;ide:run:testthat::snapshot_review('foo/bar.R')testthat::snapshot_review('foo/bar.R')]8;; to interactively review the change.
269269

270+
# is_snapshotting is true in snapshots
271+
272+
Code
273+
is_snapshotting()
274+
Output
275+
[1] TRUE
276+
277+
---
278+
279+
true
280+
281+
---
282+
283+
[1] TRUE
284+
285+
---
286+
287+
Is snapshotting!
288+
289+
---
290+
291+
Is snapshotting!
292+

tests/testthat/test-snapshot.R

+10
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,13 @@ test_that("hint is informative", {
201201
cat(snapshot_accept_hint("foo", "bar.R", reset_output = FALSE))
202202
})
203203
})
204+
205+
test_that("is_snapshotting is true in snapshots", {
206+
expect_false(is_snapshotting())
207+
208+
expect_snapshot(is_snapshotting())
209+
expect_snapshot_value(is_snapshotting())
210+
expect_snapshot_output(is_snapshotting())
211+
expect_snapshot_warning(if (is_snapshotting()) warning("Is snapshotting!"))
212+
expect_snapshot_error(if (is_snapshotting()) stop("Is snapshotting!"))
213+
})

0 commit comments

Comments
 (0)