Skip to content

Commit eba3399

Browse files
Partial fix for upcoming testthat release (#2937)
* Partial fix for upcoming testthat release `expect_success()` and `expect_failure()` now test that you have exactly one success/failure and zero failures/successes. I can't quite figure out why the tests are still failing here; maybe it's something to do with recycling? I'm happy to help but I unfortunately I don't know enough about the lintr internals to figure out what's going wrong here. We're planning to submit testthat to CRAN on Nov 10. * simple delint * extract to helper for cyclo complexity * avoid testthat::expect() again * fail() needs return() --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 275ed7d commit eba3399

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

R/expect_lint.R

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en", igno
5959

6060
wrong_number_fmt <- "got %d lints instead of %d%s"
6161
if (is.null(checks)) {
62-
msg <- sprintf(wrong_number_fmt, n_lints, length(checks), lint_str)
63-
return(testthat::expect(n_lints %==% 0L, msg))
62+
if (n_lints != 0L) {
63+
return(testthat::fail(sprintf(wrong_number_fmt, n_lints, 0L, lint_str)))
64+
}
65+
return(testthat::succeed())
6466
}
6567

6668
if (!is.list(checks) || !is.null(names(checks))) { # vector or named list
@@ -69,8 +71,7 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en", igno
6971
checks[] <- lapply(checks, fix_names, "message")
7072

7173
if (n_lints != length(checks)) {
72-
msg <- sprintf(wrong_number_fmt, n_lints, length(checks), lint_str)
73-
return(testthat::expect(FALSE, msg))
74+
return(testthat::fail(sprintf(wrong_number_fmt, n_lints, length(checks), lint_str)))
7475
}
7576

7677
if (ignore_order) {
@@ -85,42 +86,47 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en", igno
8586
checks <- checks[check_order]
8687
}
8788

88-
local({
89-
itr_env <- new.env(parent = emptyenv())
90-
itr_env$itr <- 0L
91-
# valid fields are those from Lint(), plus 'linter'
92-
lint_fields <- c(names(formals(Lint)), "linter")
93-
Map(
94-
function(lint, check) {
95-
itr_env$itr <- itr_env$itr + 1L
96-
lapply(names(check), function(field) {
97-
if (!field %in% lint_fields) {
98-
cli_abort(c(
99-
x = "Check {.val {itr_env$itr}} has an invalid field: {.field {field}}.",
100-
i = "Valid fields are: {.field {lint_fields}}."
101-
))
102-
}
103-
check <- check[[field]]
104-
value <- lint[[field]]
105-
msg <- sprintf(
106-
"check #%d: %s %s did not match %s",
107-
itr_env$itr, field, deparse(value), deparse(check)
108-
)
109-
# deparse ensures that NULL, list(), etc are handled gracefully
110-
ok <- if (field == "message") {
111-
re_matches_logical(value, check)
112-
} else {
113-
isTRUE(all.equal(value, check))
114-
}
115-
testthat::expect(ok, msg)
116-
})
117-
},
118-
lints,
119-
checks
120-
)
121-
})
89+
expect_lint_impl_(lints, checks)
90+
91+
testthat::succeed()
92+
}
12293

123-
invisible(NULL)
94+
#' NB: must _not_ succeed(), should only fail() or abort()
95+
#' @noRd
96+
expect_lint_impl_ <- function(lints, checks) {
97+
itr <- 0L
98+
# valid fields are those from Lint(), plus 'linter'
99+
lint_fields <- c(names(formals(Lint)), "linter")
100+
101+
for (i in seq_along(lints)) {
102+
lint <- lints[[i]]
103+
check <- checks[[i]]
104+
105+
itr <- itr + 1L
106+
107+
for (field in names(check)) {
108+
if (!field %in% lint_fields) {
109+
cli_abort(c(
110+
x = "Check {.val {itr}} has an invalid field: {.field {field}}.",
111+
i = "Valid fields are: {.field {lint_fields}}."
112+
))
113+
}
114+
check_field <- check[[field]]
115+
value <- lint[[field]]
116+
ok <- if (field == "message") {
117+
re_matches_logical(value, check_field)
118+
} else {
119+
isTRUE(all.equal(value, check_field))
120+
}
121+
if (!ok) {
122+
return(testthat::fail(sprintf(
123+
"check #%d: %s %s did not match %s",
124+
# deparse ensures that NULL, list(), etc are handled gracefully
125+
itr, field, deparse(value), deparse(check)
126+
)))
127+
}
128+
}
129+
}
124130
}
125131

126132
#' @rdname expect_lint
@@ -162,12 +168,11 @@ expect_lint_free <- function(...) {
162168
if (has_lints) {
163169
lint_output <- format(lints)
164170
}
165-
result <- testthat::expect(
166-
!has_lints,
167-
paste0("Not lint free\n", lint_output)
168-
)
169171

170-
invisible(result)
172+
if (has_lints) {
173+
return(testthat::fail(paste0("Not lint free\n", lint_output)))
174+
}
175+
testthat::succeed()
171176
}
172177

173178
# Helper function to check if testthat is installed.

0 commit comments

Comments
 (0)