Skip to content

Commit 2b36f1b

Browse files
committed
Make interactive snapshot output a bit more obvious
Testing this change revealed a bug in `test_code()`: specifying the default reporter happened _after_ `local_test_context()` meaning that it failed to capture the actual user settings. I fixed this by now requiring a reporter (rather than a backup reporter). Fixes #1992
1 parent 75c70e0 commit 2b36f1b

File tree

6 files changed

+17
-12
lines changed

6 files changed

+17
-12
lines changed

R/describe.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ describe <- function(description, code) {
7676
}
7777

7878
describe_it <- function(description, code, env = parent.frame()) {
79+
reporter <- get_reporter() %||% local_interactive_reporter()
7980
local_test_context()
8081

8182
test_code(
8283
description,
8384
code,
8485
env = env,
85-
default_reporter = local_interactive_reporter(),
86+
reporter = reporter,
8687
skip_on_empty = FALSE
8788
)
8889
}

R/snapshot-file.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ expect_snapshot_file <- function(path,
101101

102102
snapshotter <- get_snapshotter()
103103
if (is.null(snapshotter)) {
104-
snapshot_not_available(paste0("New path: ", path))
104+
snapshot_not_available("New path", path)
105105
return(invisible())
106106
}
107107

R/snapshot.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ expect_snapshot_helper <- function(lab, val,
272272

273273
snapshotter <- get_snapshotter()
274274
if (is.null(snapshotter)) {
275-
snapshot_not_available(paste0("Current value:\n", save(val)))
275+
snapshot_not_available("Current snapshot", save(val))
276276
return(invisible())
277277
}
278278

@@ -322,12 +322,15 @@ snapshot_accept_hint <- function(variant, file, reset_output = TRUE) {
322322
)
323323
}
324324

325-
snapshot_not_available <- function(message) {
325+
snapshot_not_available <- function(header, message) {
326+
local_reporter_output()
327+
326328
cli::cli_inform(c(
327-
"{.strong Can't compare snapshot to reference when testing interactively.}",
328-
i = "Run {.run devtools::test()} or {.code testthat::test_file()} to see changes."
329+
i = "Can't create snapshot or compare to reference when testing interactively."
329330
))
331+
cat(cli::rule(header), "\n", sep = "")
330332
cat(message, "\n", sep = "")
333+
cat(cli::rule(), "\n", sep = "")
331334
}
332335

333336
local_snapshot_dir <- function(snap_names, .env = parent.frame()) {
@@ -362,4 +365,3 @@ with_is_snapshotting <- function(code) {
362365
withr::local_envvar(TESTTHAT_IS_SNAPSHOT = "true")
363366
code
364367
}
365-

R/source.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ source_file <- function(path,
4444
test = NULL,
4545
code = exprs,
4646
env = env,
47-
default_reporter = StopReporter$new()
47+
reporter = get_reporter() %||% StopReporter$new()
4848
))
4949
} else {
5050
withCallingHandlers(

R/test-example.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test_example <- function(path, title = path) {
6565
test = title,
6666
code = parse(ex_path, encoding = "UTF-8"),
6767
env = env,
68-
default_reporter = StopReporter$new(),
68+
reporter = get_reporter() %||% StopReporter$new(),
6969
skip_on_empty = FALSE
7070
)
7171
if (ok) succeed(path)

R/test-that.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,23 @@ test_that <- function(desc, code) {
4646
}
4747
}
4848

49+
# Must initialise interactive reporter before local_test_context()
50+
reporter <- get_reporter() %||% local_interactive_reporter()
4951
local_test_context()
52+
5053
test_code(
5154
desc,
5255
code,
5356
env = parent.frame(),
54-
default_reporter = local_interactive_reporter()
57+
reporter = reporter
5558
)
5659
}
5760

5861
# Access error fields with `[[` rather than `$` because the
5962
# `$.Throwable` from the rJava package throws with unknown fields
60-
test_code <- function(test, code, env, default_reporter, skip_on_empty = TRUE) {
63+
test_code <- function(test, code, env, reporter, skip_on_empty = TRUE) {
6164

6265
frame <- caller_env()
63-
reporter <- get_reporter() %||% default_reporter
6466

6567
if (!is.null(test)) {
6668
reporter$start_test(context = reporter$.context, test = test)

0 commit comments

Comments
 (0)