Skip to content

Commit d214082

Browse files
committed
Combine hint functions
1 parent ee43c0f commit d214082

File tree

7 files changed

+64
-69
lines changed

7 files changed

+64
-69
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ S3method(print,comparison)
2424
S3method(print,expectation)
2525
S3method(print,mismatch_character)
2626
S3method(print,mismatch_numeric)
27+
S3method(print,testthat_hint)
2728
S3method(print,testthat_results)
2829
S3method(snapshot_replay,character)
2930
S3method(snapshot_replay,condition)

R/snapshot-file.R

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ expect_snapshot_file <- function(
157157
}
158158

159159
file <- snapshotter$file
160-
if (in_check_reporter()) {
161-
hint <- ""
162-
} else {
163-
hint <- snapshot_review_hint(file, name, is_text = is_text)
164-
}
165160

166161
if (!equal) {
167162
if (is_text) {
@@ -181,6 +176,8 @@ expect_snapshot_file <- function(
181176
comp <- NULL
182177
}
183178

179+
hint <- snapshot_hint(file, variant, file, show_accept = is_text)
180+
184181
msg <- c(
185182
sprintf("Snapshot of %s has changed.", lab),
186183
comp,
@@ -207,28 +204,6 @@ announce_snapshot_file <- function(path, name = basename(path)) {
207204
}
208205
}
209206

210-
snapshot_review_hint <- function(
211-
test,
212-
name,
213-
is_text = FALSE,
214-
reset_output = TRUE
215-
) {
216-
if (reset_output) {
217-
local_reporter_output()
218-
}
219-
220-
c(
221-
if (is_text) {
222-
cli::format_inline(
223-
"* Run {.run testthat::snapshot_accept('{test}/{name}')} to accept the change."
224-
)
225-
},
226-
cli::format_inline(
227-
"* Run {.run testthat::snapshot_review('{test}/{name}')} to review the change."
228-
)
229-
)
230-
}
231-
232207
snapshot_file_equal <- function(
233208
snap_dir, # _snaps/
234209
snap_test, # test file name

R/snapshot.R

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,12 @@ expect_snapshot_helper <- function(
367367
} else {
368368
variant_lab <- ""
369369
}
370-
if (in_check_reporter()) {
371-
hint <- ""
372-
} else {
373-
hint <- snapshot_accept_hint(variant, snapshotter$file)
374-
}
375370

376371
if (length(comp) != 0) {
377-
msg <- sprintf(
378-
"Snapshot of %s has changed%s:\n%s\n\n%s",
379-
lab,
380-
variant_lab,
381-
paste0(comp, collapse = "\n\n"),
372+
hint <- snapshot_hint(NULL, variant, snapshotter$file)
373+
msg <- c(
374+
sprintf("Snapshot of %s has changed%s:", lab, variant_lab),
375+
comp,
382376
hint
383377
)
384378
return(snapshot_fail(msg, trace_env = trace_env))
@@ -387,18 +381,25 @@ expect_snapshot_helper <- function(
387381
pass(NULL)
388382
}
389383

390-
snapshot_accept_hint <- function(variant, file, reset_output = TRUE) {
384+
snapshot_hint <- function(
385+
file,
386+
variant,
387+
name,
388+
show_accept = TRUE,
389+
reset_output = TRUE
390+
) {
391+
if (in_check_reporter()) {
392+
return("")
393+
}
394+
391395
if (reset_output) {
392396
local_reporter_output()
393397
}
394398

395-
if (is.null(variant) || variant == "_default") {
396-
name <- file
397-
} else {
398-
name <- file.path(variant, file)
399-
}
399+
id <- c(file, if (!identical(variant, "_default")) variant, name)
400+
full_name <- paste0(id, collapse = "/")
400401

401-
args <- encodeString(name, quote = '"')
402+
args <- encodeString(full_name, quote = '"')
402403
# Include path argument if we're in a different working directory
403404
wd <- Sys.getenv("TESTTHAT_WD", unset = "")
404405
if (wd != "") {
@@ -408,17 +409,23 @@ snapshot_accept_hint <- function(variant, file, reset_output = TRUE) {
408409
}
409410
}
410411

411-
paste0(
412-
cli::format_inline(
413-
"* Run {.run testthat::snapshot_accept({args})} to accept the change."
414-
),
415-
"\n",
416-
cli::format_inline(
417-
"* Run {.run testthat::snapshot_review({args})} to review the change."
418-
)
412+
accept_link <- cli::format_inline("{.run testthat::snapshot_accept({args})}")
413+
review_link <- cli::format_inline("{.run testthat::snapshot_review({args})}")
414+
415+
out <- c(
416+
if (show_accept) sprintf("* Run %s to accept the change.", accept_link),
417+
sprintf("* Run %s to review the change.", review_link)
419418
)
419+
structure(out, class = "testthat_hint")
420420
}
421421

422+
#' @export
423+
print.testthat_hint <- function(x, ...) {
424+
cat(paste0(x, "\n", collapse = ""))
425+
invisible(x)
426+
}
427+
428+
422429
snapshot_not_available <- function(message) {
423430
local_reporter_output()
424431

tests/testthat/_snaps/snapshot-file.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@
2525
# generates informative hint
2626

2727
Code
28-
base::writeLines(snapshot_review_hint("lala", "foo.R", reset_output = FALSE))
28+
snapshot_hint("lala", NULL, "foo.R", show_accept = FALSE, reset_output = FALSE)
2929
Output
30-
* Run `testthat::snapshot_review('lala/foo.R')` to review the change.
30+
* Run `testthat::snapshot_review("lala/foo.R")` to review the change.
3131

3232
---
3333

3434
Code
35-
base::writeLines(snapshot_review_hint("lala", "foo.R", is_text = TRUE,
36-
reset_output = FALSE))
35+
snapshot_hint("lala", NULL, "foo.R", show_accept = TRUE, reset_output = FALSE)
3736
Output
38-
* Run `testthat::snapshot_accept('lala/foo.R')` to accept the change.
39-
* Run `testthat::snapshot_review('lala/foo.R')` to review the change.
37+
* Run `testthat::snapshot_accept("lala/foo.R")` to accept the change.
38+
* Run `testthat::snapshot_review("lala/foo.R")` to review the change.
4039

4140
# expect_snapshot_file validates its inputs
4241

tests/testthat/_snaps/snapshot.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@
264264
# hint is informative
265265

266266
Code
267-
cat(snapshot_accept_hint("_default", "bar.R", reset_output = FALSE))
267+
snapshot_hint(NULL, "_default", "bar.R", reset_output = FALSE)
268268
Output
269269
* Run `testthat::snapshot_accept("bar.R")` to accept the change.
270270
* Run `testthat::snapshot_review("bar.R")` to review the change.
271271
Code
272-
cat(snapshot_accept_hint("foo", "bar.R", reset_output = FALSE))
272+
snapshot_hint(NULL, "foo", "bar.R", reset_output = FALSE)
273273
Output
274274
* Run `testthat::snapshot_accept("foo/bar.R")` to accept the change.
275275
* Run `testthat::snapshot_review("foo/bar.R")` to review the change.

tests/testthat/test-snapshot-file.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,24 @@ test_that("split_path handles edge cases", {
163163
})
164164

165165
test_that("generates informative hint", {
166-
expect_snapshot(base::writeLines(snapshot_review_hint(
166+
local_mocked_bindings(in_check_reporter = function() FALSE)
167+
withr::local_envvar(GITHUB_ACTIONS = "false", TESTTHAT_WD = NA)
168+
169+
expect_snapshot(snapshot_hint(
167170
"lala",
171+
NULL,
168172
"foo.R",
173+
show_accept = FALSE,
169174
reset_output = FALSE
170-
)))
175+
))
171176

172-
expect_snapshot(base::writeLines(snapshot_review_hint(
177+
expect_snapshot(snapshot_hint(
173178
"lala",
179+
NULL,
174180
"foo.R",
175-
is_text = TRUE,
181+
show_accept = TRUE,
176182
reset_output = FALSE
177-
)))
183+
))
178184
})
179185

180186
test_that("expect_snapshot_file validates its inputs", {

tests/testthat/test-snapshot.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,28 @@ test_that("errors and warnings are folded", {
166166
# })
167167

168168
test_that("hint is informative", {
169+
local_mocked_bindings(in_check_reporter = function() FALSE)
169170
withr::local_envvar(GITHUB_ACTIONS = "false", TESTTHAT_WD = NA)
170171

171172
expect_snapshot({
172-
cat(snapshot_accept_hint("_default", "bar.R", reset_output = FALSE))
173-
cat(snapshot_accept_hint("foo", "bar.R", reset_output = FALSE))
173+
snapshot_hint(NULL, "_default", "bar.R", reset_output = FALSE)
174+
snapshot_hint(NULL, "foo", "bar.R", reset_output = FALSE)
174175
})
175176
})
176177

177178
test_that("hint includes path when WD is different", {
179+
local_mocked_bindings(in_check_reporter = function() FALSE)
178180
withr::local_envvar(TESTTHAT_WD = "..")
179181

180-
hint <- snapshot_accept_hint("_default", "bar.R", reset_output = FALSE)
182+
hint <- snapshot_hint(NULL, "_default", "bar.R", reset_output = FALSE)
181183
hint <- gsub(getwd(), "<WD>", hint)
182184
# Can't use snapshot here because its hint will get the wrong path
183-
expect_match(hint, 'snapshot_accept("bar.R", "<WD>")', fixed = TRUE)
185+
expect_match(
186+
hint,
187+
'snapshot_accept("bar.R", "<WD>")',
188+
fixed = TRUE,
189+
all = FALSE
190+
)
184191
})
185192

186193
test_that("expect_snapshot requires a non-empty test label", {

0 commit comments

Comments
 (0)