Skip to content

Commit 8d6f3d5

Browse files
hadleyDavisVaughan
andauthored
Make interactive snapshot output a bit more obvious (#2000)
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). It also revealed a bug in the width computation for issue headings 😬 Fixes #1992 Co-authored-by: Davis Vaughan <[email protected]>
1 parent f686b2d commit 8d6f3d5

File tree

8 files changed

+21
-13
lines changed

8 files changed

+21
-13
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/reporter-progress.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,9 @@ issue_header <- function(x, pad = FALSE) {
534534
issue_summary <- function(x, rule = FALSE) {
535535
header <- cli::style_bold(issue_header(x))
536536
if (rule) {
537-
header <- cli::rule(header, width = max(cli::ansi_nchar(header) + 6, 80))
537+
# Don't truncate long test names
538+
width <- max(cli::ansi_nchar(header) + 6, getOption("width"))
539+
header <- cli::rule(header, width = width)
538540
}
539541

540542
paste0(header, "\n", format(x))

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(path)
105105
return(invisible())
106106
}
107107

R/snapshot.R

Lines changed: 6 additions & 3 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(save(val))
276276
return(invisible())
277277
}
278278

@@ -323,11 +323,14 @@ snapshot_accept_hint <- function(variant, file, reset_output = TRUE) {
323323
}
324324

325325
snapshot_not_available <- function(message) {
326+
local_reporter_output()
327+
328+
cat(cli::rule("Snapshot"), "\n", sep = "")
326329
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."
330+
i = "Can't save or compare to reference when testing interactively."
329331
))
330332
cat(message, "\n", sep = "")
333+
cat(cli::rule(), "\n", sep = "")
331334
}
332335

333336
local_snapshot_dir <- function(snap_names, .env = parent.frame()) {

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)

tests/testthat/test-snapshot-reporter.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ test_that("basic workflow", {
1414
snapper$start_file("snapshot-2")
1515
# output if not active (because test not set here)
1616
expect_snapshot_output("x") %>%
17-
expect_message("Can't compare") %>%
18-
expect_output("Current value:\n[1] \"x\"", fixed = TRUE)
17+
expect_message("Can't save") %>%
18+
expect_output("[1] \"x\"", fixed = TRUE)
1919

2020
# warns on first creation
2121
snapper$start_file("snapshot-2", "test")

0 commit comments

Comments
 (0)