Skip to content

Commit

Permalink
Add a helper for loading the help database in tests (#2708)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Jan 16, 2025
1 parent 06f2b9c commit eb6abc8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
10 changes: 10 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ skip_if_not_utf8_locale <- function() {
testthat::skip_if_not(l10n_info()[["UTF-8"]], "Not a UTF-8 locale")
}

safe_load_help_db <- function() {
help_db <- tryCatch(tools::Rd_db("lintr"), error = function(e) NULL)
# e.g. in dev under pkgload::load_all()
if (length(help_db) == 0L) {
help_db <- tryCatch(tools::Rd_db(dir = testthat::test_path("..", "..")), error = function(e) NULL)
testthat::skip_if_not(length(help_db) > 0L, message = "Package help corrupted or not installed")
}
help_db
}

pipes <- function(exclude = NULL) {
if (getRversion() < "4.1.0") exclude <- unique(c(exclude, "|>"))
all_pipes <- c(
Expand Down
7 changes: 1 addition & 6 deletions tests/testthat/test-linter_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ test_that("rownames for available_linters data frame doesn't have missing entrie
# See the roxygen helpers in R/linter_tags.R for the code used to generate the docs.
# This test helps ensure the documentation is up to date with the available_linters() database
test_that("lintr help files are up to date", {
help_db <- tools::Rd_db("lintr")
# e.g. in dev under pkgload::load_all()
if (length(help_db) == 0L) {
help_db <- tools::Rd_db(dir = test_path("..", ".."))
skip_if_not(length(help_db) > 0L, message = "Package help not installed or corrupted")
}
help_db <- safe_load_help_db()

lintr_db <- available_linters(exclude_tags = NULL)
lintr_db$package <- NULL
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-lintr-package.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("All linter help files have examples", {
help_db <- tools::Rd_db("lintr")
help_db <- safe_load_help_db()
linter_db <- help_db[endsWith(names(help_db), "_linter.Rd")]
rd_has_examples <- function(rd) any(vapply(rd, attr, "Rd_tag", FUN.VALUE = character(1L)) == "\\examples")
linter_has_examples <- vapply(linter_db, rd_has_examples, logical(1L))
Expand Down

0 comments on commit eb6abc8

Please sign in to comment.