Skip to content

Commit d42608b

Browse files
committed
Polish parallel errors
2 parents ed47e44 + 00b3cc1 commit d42608b

17 files changed

+153
-80
lines changed

β€ŽNAMESPACEβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ export(skip_on_os)
192192
export(skip_on_travis)
193193
export(skip_unless_r)
194194
export(snapshot_accept)
195+
export(snapshot_reject)
195196
export(snapshot_review)
196197
export(source_dir)
197198
export(source_file)

β€ŽNEWS.mdβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* New `snapshot_reject()` rejects all modified snapshots by deleting the `.new` variants (#1923).
34
* New `SlowReporter` makes it easier to find the slowest tests in your package. The easiest way to run it is with `devtools::test(reporter = "slow")` (#1466).
45
* Power `expect_mapequal()` with `waldo::compare(list_as_map = TRUE)` (#1521).
56
* On CRAN, `test_that()` now automatically skips if a package is not installed (#1585). Practically, this means that you no longer need to check that suggested packages are installed. (We don't do this in the tidyverse because we think it has limited payoff, but other styles advise differently.)
@@ -19,7 +20,7 @@
1920
* `vignette("custom-expectations)` has been overhauled to make it much clearer how to create high-quality expectations (#2113, #2132, #2072).
2021
* `expect_snapshot()` and friends will now fail when creating a new snapshot on CI. This is usually a signal that you've forgotten to run it locally before committing (#1461).
2122
* `expect_snapshot_value()` can now handle expressions that generate `-` (#1678) or zero length atomic vectors (#2042).
22-
* `expect_matches()` failures should be a little easier to read (#2135).
23+
* `expect_matches()` failures should be a little easier to read (#2135, #2181).
2324
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).
2425
* `expect_no_*()` now executes the entire code block, rather than stopping at the first message or warning (#1991).
2526
* `expect_no_failures()` and `expect_no_successes()` are now deprecated as `expect_success()` now test for no failures and `expect_failure()` tests for no successes (#)

β€ŽR/expect-match.Rβ€Ž

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ expect_match_ <- function(
106106
info = NULL,
107107
label = NULL,
108108
negate = FALSE,
109+
title = "Text",
109110
trace_env = caller_env()
110111
) {
111112
matches <- grepl(regexp, act$val, perl = perl, fixed = fixed, ...)
@@ -118,14 +119,24 @@ expect_match_ <- function(
118119

119120
text <- encodeString(act$val)
120121
if (length(act$val) == 1) {
121-
values <- paste0('Text: "', text, '"')
122+
values <- paste0(title, ': "', text, '"')
123+
which <- ""
122124
} else {
123-
values <- paste0("Text:\n", paste0("* ", text, collapse = "\n"))
125+
bullet <- ifelse(
126+
condition,
127+
cli::col_green(cli::symbol$tick),
128+
cli::col_red(cli::symbol$cross)
129+
)
130+
values <- paste0(title, ":\n", paste0(bullet, " ", text, collapse = "\n"))
131+
which <- if (all) "Every element of " else "Some element of "
124132
}
133+
match <- if (negate) "matches" else "does not match"
125134

126135
msg <- sprintf(
127-
if (negate) "%s matches %s %s.\n%s" else "%s does not match %s %s.\n%s",
136+
"%s%s %s %s %s.\n%s",
137+
which,
128138
act$lab,
139+
match,
129140
if (fixed) "string" else "regexp",
130141
encodeString(regexp, quote = '"'),
131142
values

β€ŽR/expect-output.Rβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ expect_output <- function(
4848
pass(act$val)
4949
} else {
5050
act <- labelled_value(act$cap, act$lab)
51-
expect_match_(act, enc2native(regexp), ...)
51+
expect_match_(act, enc2native(regexp), ..., title = "Output")
5252
}
5353
}

β€ŽR/snapshot-file.Rβ€Ž

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,13 @@ snapshot_review_hint <- function(
188188
)
189189
}
190190

191+
191192
snapshot_file_equal <- function(
192-
snap_test_dir,
193-
snap_name,
194-
snap_variant,
195-
path,
193+
snap_dir, # _snaps/
194+
snap_test, # test file name
195+
snap_name, # snapshot file name
196+
snap_variant, # variant (optional)
197+
path, # path to new file
196198
file_equal = compare_file_binary,
197199
fail_on_new = FALSE,
198200
trace_env = caller_env()
@@ -201,8 +203,14 @@ snapshot_file_equal <- function(
201203
cli::cli_abort("{.path {path}} not found.")
202204
}
203205

206+
if (is.null(snap_variant)) {
207+
snap_test_dir <- file.path(snap_dir, snap_test)
208+
} else {
209+
snap_test_dir <- file.path(snap_dir, snap_variant, snap_test)
210+
}
211+
204212
cur_path <- file.path(snap_test_dir, snap_name)
205-
new_path <- new_name(cur_path)
213+
new_path <- file.path(snap_test_dir, new_name(snap_name))
206214

207215
if (file.exists(cur_path)) {
208216
eq <- file_equal(cur_path, path)
@@ -217,10 +225,10 @@ snapshot_file_equal <- function(
217225
dir.create(snap_test_dir, showWarnings = FALSE, recursive = TRUE)
218226
file.copy(path, cur_path)
219227

220-
message <- paste0(
228+
message <- paste_c(
221229
"Adding new file snapshot: 'tests/testthat/_snaps/",
222-
snap_variant,
223-
if (!is.null(snap_variant)) "/",
230+
c(snap_variant, if (!is.null(snap_variant)) "/"),
231+
c(snap_test, "/"),
224232
snap_name,
225233
"'"
226234
)

β€ŽR/snapshot-manage.Rβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#' Accept or reject modified snapshots
22
#'
33
#' * `snapshot_accept()` accepts all modified snapshots.
4+
#' * `snapshot_reject()` rejects all modified snapshots by deleting the `.new` variants.
45
#' * `snapshot_review()` opens a Shiny app that shows a visual diff of each
56
#' modified snapshot. This is particularly useful for whole file snapshots
67
#' created by `expect_snapshot_file()`.
@@ -26,6 +27,22 @@ snapshot_accept <- function(files = NULL, path = "tests/testthat") {
2627
invisible()
2728
}
2829

30+
#' @rdname snapshot_accept
31+
#' @export
32+
snapshot_reject <- function(files = NULL, path = "tests/testthat") {
33+
changed <- snapshot_meta(files, path)
34+
if (nrow(changed) == 0) {
35+
inform("No snapshots to reject")
36+
return(invisible())
37+
}
38+
39+
inform(c("Rejecting snapshots:", changed$name))
40+
unlink(changed$new)
41+
42+
rstudio_tickle()
43+
invisible()
44+
}
45+
2946
#' @rdname snapshot_accept
3047
#' @export
3148
snapshot_review <- function(files = NULL, path = "tests/testthat") {

β€ŽR/snapshot-reporter.Rβ€Ž

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,9 @@ SnapshotReporter <- R6::R6Class(
108108
) {
109109
self$announce_file_snapshot(name)
110110

111-
if (is.null(variant)) {
112-
snap_dir <- file.path(self$snap_dir, self$file)
113-
} else {
114-
snap_dir <- file.path(self$snap_dir, variant, self$file)
115-
}
116111
snapshot_file_equal(
117-
snap_test_dir = snap_dir,
112+
snap_dir = self$snap_dir,
113+
snap_test = self$file,
118114
snap_name = name,
119115
snap_variant = variant,
120116
path = path,

β€ŽR/utils.Rβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ no_wrap <- function(x) {
6363
x <- gsub("\n", "\f", x, fixed = TRUE)
6464
x
6565
}
66+
67+
paste_c <- function(...) {
68+
paste0(c(...), collapse = "")
69+
}

β€Žman/snapshot_accept.Rdβ€Ž

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

β€Žtests/testthat/_snaps/expect-match.mdβ€Ž

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@
99

1010
---
1111

12-
`many` does not match regexp "asdf".
12+
Every element of `many` does not match regexp "a".
1313
Text:
14-
* a
15-
* b
16-
* c
17-
* d
18-
* e
14+
βœ” a
15+
βœ” a
16+
βœ– b
17+
18+
---
19+
20+
Some element of `many` does not match regexp "c".
21+
Text:
22+
βœ– a
23+
βœ– a
24+
βœ– b
1925

2026
# expect_match validates its inputs
2127

0 commit comments

Comments
Β (0)