Skip to content

Commit 5982a8b

Browse files
Make require_testthat helper function (#2587)
* Make require_testthat helper function * Update NEWS.md * Add back testthat scopeing * Roxygenise * Add space in stop message * Add a default to require_testthat * avoid using linter_auto_name, add tests * move NEWS entry * I am in the wrong project. Notes is right indeed :) * delint * nolint false positive --------- Co-authored-by: Michael Chirico <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
1 parent dac9118 commit 5982a8b

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
+ Linters `closed_curly_linter()`, `open_curly_linter()`, `paren_brace_linter()`, and `semicolon_terminator_linter()`.
1111
+ `linter=` argument of `Lint()`.
1212

13+
## Notes
14+
15+
* `expect_lint_free()` and other functions that rely on the {testthat} framework now have a consistent error message. (#2585, @F-Noelle).
16+
1317
# lintr 3.2.0
1418

1519
## Deprecations & breaking changes

R/expect_lint.R

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,8 @@
4242
#' )
4343
#' @export
4444
expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
45-
if (!requireNamespace("testthat", quietly = TRUE)) {
46-
# nocov start
47-
cli_abort(
48-
# nolint next: line_length_linter.
49-
"{.fun expect_lint} and {.fun expect_no_lint} are designed to work within the {.pkg testthat} testing framework, which is not installed."
50-
)
51-
# nocov end
52-
}
45+
require_testthat()
46+
5347
old_lang <- set_lang(language)
5448
on.exit(reset_lang(old_lang))
5549

@@ -123,6 +117,7 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
123117
#' @rdname expect_lint
124118
#' @export
125119
expect_no_lint <- function(content, ..., file = NULL, language = "en") {
120+
require_testthat()
126121
expect_lint(content, NULL, ..., file = file, language = language)
127122
}
128123

@@ -135,6 +130,8 @@ expect_no_lint <- function(content, ..., file = NULL, language = "en") {
135130
#' @param ... arguments passed to [lint_package()]
136131
#' @export
137132
expect_lint_free <- function(...) {
133+
require_testthat()
134+
138135
testthat::skip_on_cran()
139136
testthat::skip_on_covr()
140137

@@ -152,3 +149,16 @@ expect_lint_free <- function(...) {
152149

153150
invisible(result)
154151
}
152+
153+
# Helper function to check if testthat is installed.
154+
require_testthat <- function() {
155+
parent_call <- sys.call(-1L)[[1L]]
156+
# supported: foo() or lintr::foo(). Undefined behavior otherwise.
157+
# nolint next: object_usage_linter. TODO(#2252): Remove this.
158+
name <- as.character(if (is.name(parent_call)) parent_call else parent_call[[3L]])
159+
if (!requireNamespace("testthat", quietly = TRUE)) {
160+
cli_abort(
161+
"{.fun {name}} is designed to work within the {.pkg testthat} testing framework, which could not be loaded."
162+
)
163+
}
164+
}

R/lintr-package.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ NULL
2121

2222
# make binding available for mock testing
2323
# ref: https://testthat.r-lib.org/dev/reference/local_mocked_bindings.html#base-functions
24+
# nolint start: object_name_linter. These will be copied from base style.
25+
requireNamespace <- NULL
2426
unlink <- NULL
2527
quit <- NULL
28+
# nolint end: object_name_linter.

tests/testthat/test-expect_lint.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,13 @@ test_that("expect_lint doesn't change language", {
7070
expect_identical(Sys.getenv("LANGUAGE"), "mr")
7171
})
7272
})
73+
74+
test_that("execution without testthat gives the right errors", {
75+
local_mocked_bindings(requireNamespace = function(...) FALSE)
76+
lint_msg <- function(nm) rex::rex("`", nm, "()` is designed to work", anything, "testthat")
77+
78+
expect_error(expect_lint(), lint_msg("expect_lint"))
79+
expect_error(lintr::expect_lint(), lint_msg("expect_lint"))
80+
expect_error(expect_no_lint(), lint_msg("expect_no_lint"))
81+
expect_error(expect_lint_free(), lint_msg("expect_lint_free"))
82+
})

0 commit comments

Comments
 (0)