Skip to content

Commit b4cc9a0

Browse files
committed
In R CMD check, only notify about snapshots once
Fixes #2207
1 parent 0b896ac commit b4cc9a0

File tree

14 files changed

+140
-122
lines changed

14 files changed

+140
-122
lines changed

R/expect-that.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#' informative traceack for failures. You should only need to set this if
1919
#' you're calling `fail()` from a helper function; see
2020
#' `vignette("custom-expectation")` for details.
21+
#' @param snapshot Is this a snapshot failure?
2122
#' @param trace An optional backtrace created by [rlang::trace_back()].
2223
#' When supplied, the expectation is displayed with the backtrace.
2324
#' Expert use only.
@@ -39,7 +40,8 @@ fail <- function(
3940
info = NULL,
4041
srcref = NULL,
4142
trace_env = caller_env(),
42-
trace = NULL
43+
trace = NULL,
44+
snapshot = FALSE
4345
) {
4446
if (is.null(trace)) {
4547
trace <- trace_back(top = getOption("testthat_topenv"), bottom = trace_env)
@@ -50,7 +52,13 @@ fail <- function(
5052
}
5153

5254
message <- paste(c(message, info), collapse = "\n")
53-
expectation("failure", message, srcref = srcref, trace = trace)
55+
expectation(
56+
"failure",
57+
message,
58+
srcref = srcref,
59+
trace = trace,
60+
snapshot = snapshot
61+
)
5462
}
5563

5664
#' @rdname fail

R/expectation.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,20 @@ expect <- function(
4949
#' @keywords internal
5050
#' @inheritParams expect
5151
#' @export
52-
expectation <- function(type, message, srcref = NULL, trace = NULL) {
53-
exp <- new_expectation(type, message, srcref = srcref, trace = trace)
52+
expectation <- function(
53+
type,
54+
message,
55+
snapshot = FALSE,
56+
srcref = NULL,
57+
trace = NULL
58+
) {
59+
exp <- new_expectation(
60+
type,
61+
message,
62+
snapshot = snapshot,
63+
srcref = srcref,
64+
trace = trace
65+
)
5466
exp_signal(exp)
5567
}
5668
#' @rdname expectation

R/reporter-check.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CheckReporter <- R6::R6Class(
1313
skips = NULL,
1414
warnings = NULL,
1515
n_ok = 0L,
16+
has_snapshot_failure = FALSE,
1617

1718
initialize = function(...) {
1819
self$capabilities$parallel_support <- TRUE
@@ -64,6 +65,11 @@ CheckReporter <- R6::R6Class(
6465
self$rule("Failed tests", line = 2)
6566
self$cat_line(map_chr(problems, issue_summary, rule = TRUE))
6667
self$cat_line()
68+
69+
if (some(problems, \(x) isTRUE(attr(x, "snapshot")))) {
70+
self$rule("To resolve snapshot failures", line = 1)
71+
self$cat_line(snapshot_check_hint())
72+
}
6773
} else {
6874
# clean up
6975
unlink("testthat-problems.rds")
@@ -105,3 +111,36 @@ summary_line <- function(n_fail, n_warn, n_skip, n_pass) {
105111
" ]"
106112
)
107113
}
114+
115+
snapshot_check_hint <- function() {
116+
if (on_gh()) {
117+
repository <- Sys.getenv("GITHUB_REPOSITORY")
118+
run_id <- Sys.getenv("GITHUB_RUN_ID")
119+
120+
call <- sprintf(
121+
"testthat::snapshot_download_gh(\"%s\", \"%s\")",
122+
repository,
123+
run_id
124+
)
125+
copy <- sprintf("* Run `%s` to download snapshots.", call)
126+
} else {
127+
copy <- c(
128+
if (on_ci()) {
129+
"* Download and unzip artifact."
130+
} else {
131+
"* Locate check directory."
132+
},
133+
"* Copy 'tests/testthat/_snaps' to local package."
134+
)
135+
}
136+
137+
action <- c(
138+
"* Run `testthat::snapshot_accept()` to accept all changes.",
139+
"* Run `testthat::snapshot_review()` to review all changes."
140+
)
141+
c(copy, action)
142+
}
143+
144+
run <- function(x) {
145+
cli::format_inline(paste0("{.run testthat::", x, "}"))
146+
}

R/snapshot-file.R

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ expect_snapshot_file <- function(
142142
return(equal)
143143
}
144144

145-
hint <- snapshot_review_hint(snapshotter$file, name)
145+
if (in_rcmd_check()) {
146+
hint <- ""
147+
} else {
148+
hint <- snapshot_review_hint(snapshotter$file, name)
149+
}
146150

147151
if (!equal) {
148152
msg <- sprintf(
@@ -151,7 +155,7 @@ expect_snapshot_file <- function(
151155
paste0(snapshotter$file, "/", name),
152156
hint
153157
)
154-
return(fail(msg))
158+
return(fail(msg, snapshot = TRUE))
155159
}
156160
pass(NULL)
157161
}
@@ -167,45 +171,16 @@ announce_snapshot_file <- function(path, name = basename(path)) {
167171
}
168172
}
169173

170-
snapshot_review_hint <- function(
171-
test,
172-
name,
173-
ci = on_ci(),
174-
check = in_rcmd_check(),
175-
reset_output = TRUE
176-
) {
174+
snapshot_review_hint <- function(test, name, reset_output = TRUE) {
177175
if (reset_output) {
178176
local_reporter_output()
179177
}
180178

181-
path <- paste0("tests/testthat/_snaps/", test, "/", new_name(name))
182-
183-
if (check) {
184-
if (on_gh()) {
185-
bullets <- snap_download_hint()
186-
} else {
187-
bullets <- c(
188-
if (ci) "* Download and unzip run artifact\n",
189-
if (!ci) "* Locate check directory\n",
190-
paste0("* Copy '", path, "' to local test directory\n")
191-
)
192-
}
193-
} else {
194-
bullets <- NULL
195-
}
196-
197-
paste0(
198-
c(
199-
bullets,
200-
cli::format_inline(
201-
"* Run {.run testthat::snapshot_review('{test}/')} to review changes"
202-
)
203-
),
204-
collapse = ""
179+
cli::format_inline(
180+
"* Run {.run testthat::snapshot_review('{test}/{name}')} to review the change."
205181
)
206182
}
207183

208-
209184
snapshot_file_equal <- function(
210185
snap_dir, # _snaps/
211186
snap_test, # test file name
@@ -254,7 +229,7 @@ snapshot_file_equal <- function(
254229
# We want to fail on CI since this suggests that the user has failed
255230
# to record the value locally
256231
if (fail_on_new) {
257-
return(fail(message, trace_env = trace_env))
232+
return(fail(message, snapshot = TRUE, trace_env = trace_env))
258233
}
259234
testthat_warn(message)
260235
TRUE

R/snapshot-github.R

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ snapshot_download_gh <- function(repository, run_id, dest_dir = ".") {
4040
dir_copy(src_snaps, dest_snaps)
4141
}
4242

43-
snap_download_hint <- function() {
44-
repository <- Sys.getenv("GITHUB_REPOSITORY")
45-
run_id <- Sys.getenv("GITHUB_RUN_ID")
46-
47-
sprintf(
48-
"* Call `snapshot_download_gh(\"%s\", \"%s\")` to download the snapshots from GitHub.\n",
49-
repository,
50-
run_id
51-
)
52-
}
53-
5443
gh_find_job <- function(repository, run_id) {
5544
jobs_json <- gh::gh(
5645
"/repos/{repository}/actions/runs/{run_id}/jobs",

R/snapshot.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ expect_snapshot_condition_ <- function(
289289
class
290290
)
291291
}
292-
return(fail(msg, trace_env = trace_env))
292+
return(fail(msg, snapshot = TRUE, trace_env = trace_env))
293293
}
294294

295295
expect_snapshot_helper(
@@ -340,7 +340,11 @@ expect_snapshot_helper <- function(
340340
} else {
341341
variant_lab <- ""
342342
}
343-
hint <- snapshot_accept_hint(variant, snapshotter$file)
343+
if (in_rcmd_check()) {
344+
hint <- ""
345+
} else {
346+
hint <- snapshot_accept_hint(variant, snapshotter$file)
347+
}
344348

345349
if (length(comp) != 0) {
346350
msg <- sprintf(
@@ -350,7 +354,7 @@ expect_snapshot_helper <- function(
350354
paste0(comp, collapse = "\n\n"),
351355
hint
352356
)
353-
return(fail(msg, trace_env = trace_env))
357+
return(fail(msg, snapshot = TRUE, trace_env = trace_env))
354358
}
355359

356360
pass(NULL)
@@ -368,13 +372,12 @@ snapshot_accept_hint <- function(variant, file, reset_output = TRUE) {
368372
}
369373

370374
paste0(
371-
if (on_gh()) snap_download_hint(),
372375
cli::format_inline(
373376
"* Run {.run testthat::snapshot_accept('{name}')} to accept the change."
374377
),
375378
"\n",
376379
cli::format_inline(
377-
"* Run {.run testthat::snapshot_review('{name}')} to interactively review the change."
380+
"* Run {.run testthat::snapshot_review('{name}')} to review the change."
378381
)
379382
)
380383
}

man/expectation.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fail.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/reporter-check.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,32 @@
9696

9797
[ FAIL 4 | WARN 1 | SKIP 3 | PASS 1 ]
9898

99+
# generates informative snapshot hints
100+
101+
Code
102+
base::writeLines(snapshot_check_hint())
103+
Output
104+
* Locate check directory.
105+
* Copy 'tests/testthat/_snaps' to local package.
106+
* Run `testthat::snapshot_accept()` to accept all changes.
107+
* Run `testthat::snapshot_review()` to review all changes.
108+
109+
---
110+
111+
Code
112+
base::writeLines(snapshot_check_hint())
113+
Output
114+
* Download and unzip artifact.
115+
* Copy 'tests/testthat/_snaps' to local package.
116+
* Run `testthat::snapshot_accept()` to accept all changes.
117+
* Run `testthat::snapshot_review()` to review all changes.
118+
119+
---
120+
121+
Code
122+
base::writeLines(snapshot_check_hint())
123+
Output
124+
* Run `testthat::snapshot_download_gh("r-lib/testthat", "123")` to download snapshots.
125+
* Run `testthat::snapshot_accept()` to accept all changes.
126+
* Run `testthat::snapshot_review()` to review all changes.
127+

tests/testthat/_snaps/snapshot-file.md

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,12 @@
2222
Error in `snapshot_file_equal_()`:
2323
! 'doesnt-exist.txt' not found.
2424

25-
# snapshot_hint output differs in R CMD check
25+
# generates informative hint
2626

2727
Code
28-
cat(snapshot_review_hint("lala", "foo.r", check = FALSE, ci = FALSE))
28+
cat(snapshot_review_hint("lala", "foo.r", reset_output = FALSE))
2929
Output
30-
* Run `testthat::snapshot_review('lala/')` to review changes
31-
32-
---
33-
34-
Code
35-
cat(snapshot_review_hint("lala", "foo.r", check = TRUE, ci = FALSE))
36-
Output
37-
* Locate check directory
38-
* Copy 'tests/testthat/_snaps/lala/foo.new.r' to local test directory
39-
* Run `testthat::snapshot_review('lala/')` to review changes
40-
41-
---
42-
43-
Code
44-
cat(snapshot_review_hint("lala", "foo.r", check = TRUE, ci = TRUE))
45-
Output
46-
* Download and unzip run artifact
47-
* Copy 'tests/testthat/_snaps/lala/foo.new.r' to local test directory
48-
* Run `testthat::snapshot_review('lala/')` to review changes
49-
50-
---
51-
52-
Code
53-
cat(snapshot_review_hint("lala", "foo.r", check = TRUE, ci = TRUE))
54-
Output
55-
* Call `snapshot_download_gh("r-lib/testthat", "123")` to download the snapshots from GitHub.
56-
* Run `testthat::snapshot_review('lala/')` to review changes
30+
* Run `testthat::snapshot_review('lala/foo.r')` to review the change.
5731

5832
# expect_snapshot_file validates its inputs
5933

0 commit comments

Comments
 (0)