Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make require_testthat helper function #2587

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* `make_linter_from_xpath()` errors up front when `lint_message` is missing (instead of delaying this error until the linter is used, #2541, @MichaelChirico).
* `paste_linter()` is extended to recommend using `paste()` instead of `paste0()` for simply aggregating a character vector with `collapse=`, i.e., when `sep=` is irrelevant (#1108, @MichaelChirico).
* `expect_no_lint()` was added as new function to cover the typical use case of expecting no lint message, akin to the recent {testthat} functions like `expect_no_warning()` (#2580, @F-Noelle).
* `expect_lint_free()` and other functions that rely on the {testthat} framework now have a consistent error message. (#2585, @F-Noelle).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this under a ## Notes section (see other releases)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shifted the item to notes.


### New linters

Expand Down
27 changes: 17 additions & 10 deletions R/expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@
#' )
#' @export
expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
if (!requireNamespace("testthat", quietly = TRUE)) {
stop( # nocov start
"'expect_lint' and 'expect_no_lint' are designed to work within the 'testthat' testing framework, ",
"but 'testthat' is not installed.",
call. = FALSE
) # nocov end
}
require_testthat("expect_lint")

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

Expand Down Expand Up @@ -129,18 +124,20 @@ 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_no_lint")
expect_lint(content, NULL, ..., file = file, language = language)
}

#' Test that the package is lint free
#'
#' This function is a thin wrapper around lint_package that simply tests there are no
#' lints in the package. It can be used to ensure that your tests fail if the package
#' contains lints.
#' This function is a thin wrapper around lint_package that simply tests there are no lints in the package.
#' It can be used to ensure that your tests fail if the package contains lints.
#'
#' @param ... arguments passed to [lint_package()]
#' @export
expect_lint_free <- function(...) {
require_testthat("expect_lint_free")

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

Expand All @@ -158,3 +155,13 @@ expect_lint_free <- function(...) {

invisible(result)
}

# Helper function to check if testthat is installed.
require_testthat <- function(name) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not require the argument, e.g. name = sys.call(sys.parent()), or possibly re-using/unifying with linter_auto_name(). Then just require_testthat().

Copy link
Contributor Author

@F-Noelle F-Noelle May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this a bit. linter_auto_name(-5L) seems to do the trick.

if (!requireNamespace("testthat", quietly = TRUE)) {
stop( # nocov start
name, "is designed to work within the 'testthat' testing framework, but 'testthat' is not installed.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't stop() use sep = "", butchering the error message like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Added a space.

call. = FALSE
) # nocov end
}
}
5 changes: 2 additions & 3 deletions man/expect_lint_free.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading