Skip to content

Commit

Permalink
Make require_testthat helper function (#2587)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Feb 13, 2025
1 parent dac9118 commit 5982a8b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
+ Linters `closed_curly_linter()`, `open_curly_linter()`, `paren_brace_linter()`, and `semicolon_terminator_linter()`.
+ `linter=` argument of `Lint()`.

## Notes

* `expect_lint_free()` and other functions that rely on the {testthat} framework now have a consistent error message. (#2585, @F-Noelle).

# lintr 3.2.0

## Deprecations & breaking changes
Expand Down
26 changes: 18 additions & 8 deletions R/expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@
#' )
#' @export
expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
if (!requireNamespace("testthat", quietly = TRUE)) {
# nocov start
cli_abort(
# nolint next: line_length_linter.
"{.fun expect_lint} and {.fun expect_no_lint} are designed to work within the {.pkg testthat} testing framework, which is not installed."
)
# nocov end
}
require_testthat()

old_lang <- set_lang(language)
on.exit(reset_lang(old_lang))

Expand Down Expand Up @@ -123,6 +117,7 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
#' @rdname expect_lint
#' @export
expect_no_lint <- function(content, ..., file = NULL, language = "en") {
require_testthat()
expect_lint(content, NULL, ..., file = file, language = language)
}

Expand All @@ -135,6 +130,8 @@ expect_no_lint <- function(content, ..., file = NULL, language = "en") {
#' @param ... arguments passed to [lint_package()]
#' @export
expect_lint_free <- function(...) {
require_testthat()

testthat::skip_on_cran()
testthat::skip_on_covr()

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

invisible(result)
}

# Helper function to check if testthat is installed.
require_testthat <- function() {
parent_call <- sys.call(-1L)[[1L]]
# supported: foo() or lintr::foo(). Undefined behavior otherwise.
# nolint next: object_usage_linter. TODO(#2252): Remove this.
name <- as.character(if (is.name(parent_call)) parent_call else parent_call[[3L]])
if (!requireNamespace("testthat", quietly = TRUE)) {
cli_abort(
"{.fun {name}} is designed to work within the {.pkg testthat} testing framework, which could not be loaded."
)
}
}
3 changes: 3 additions & 0 deletions R/lintr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ NULL

# make binding available for mock testing
# ref: https://testthat.r-lib.org/dev/reference/local_mocked_bindings.html#base-functions
# nolint start: object_name_linter. These will be copied from base style.
requireNamespace <- NULL
unlink <- NULL
quit <- NULL
# nolint end: object_name_linter.
10 changes: 10 additions & 0 deletions tests/testthat/test-expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ test_that("expect_lint doesn't change language", {
expect_identical(Sys.getenv("LANGUAGE"), "mr")
})
})

test_that("execution without testthat gives the right errors", {
local_mocked_bindings(requireNamespace = function(...) FALSE)
lint_msg <- function(nm) rex::rex("`", nm, "()` is designed to work", anything, "testthat")

expect_error(expect_lint(), lint_msg("expect_lint"))
expect_error(lintr::expect_lint(), lint_msg("expect_lint"))
expect_error(expect_no_lint(), lint_msg("expect_no_lint"))
expect_error(expect_lint_free(), lint_msg("expect_lint_free"))
})

0 comments on commit 5982a8b

Please sign in to comment.