Skip to content

Commit c0e88a4

Browse files
authored
Add skip_if_not_exported() for incremental path testing (#140)
* Add `skip_if_not_exported()` for incremental path testing Also moved `testthat` directory up a level to avoid tarball path length issues in R CMD check, and deleted generated `.Rbuildignore` files for the same reason. Fixes #133. * Review and revise AI-generated code. * force trigger actions
1 parent 7b27c22 commit c0e88a4

36 files changed

Lines changed: 170 additions & 14 deletions

‎.Rbuildignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
^AGENTS\.md$
2121
^conversation-export\.json$
2222
^vignettes/articles$
23+
^\.posit$

‎DESCRIPTION‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Suggests:
4141
desc,
4242
janitor,
4343
knitr,
44+
pkgload,
4445
rmarkdown,
4546
rvest,
4647
whisker,

‎NAMESPACE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export(read_rapid_filename)
1313
export(read_security_data)
1414
export(read_security_data_filename)
1515
export(read_security_schemes)
16+
export(skip_if_not_exported)
1617
export(use_beekeeper)
1718
if (getRversion() < "4.3.0") importFrom("S7", "@")
1819
importFrom(S7,class_any)

‎R/skip_if_not_exported.R‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#' Skip a test if a function is not exported
2+
#'
3+
#' Skip tests when functions aren't exported by a package (yet). Useful for
4+
#' auto-generated tests, so the tests are not active until the function has been
5+
#' reviewed and exported.
6+
#'
7+
#' @param fn_name (`character(1)`) The name of the function to check.
8+
#' @param pkg (`character(1)`) The name of the package to check. Defaults to
9+
#' the name of the package being tested via [testthat::testing_package()]. If
10+
#' the package is *not* the package currently being tested, it must be
11+
#' installed on the testing system.
12+
#'
13+
#' @returns (`NULL`, invisibly) Called for its side effect: skips the current
14+
#' test if `fn_name` is not exported by `pkg`.
15+
#' @export
16+
#'
17+
#' @examples
18+
#' # In a generated test file, place this at the top of the test_that() block:
19+
#' \dontrun{
20+
#' test_that("my_fn() returns expected result", {
21+
#' skip_if_not_exported("my_fn")
22+
#' # ... rest of test
23+
#' })
24+
#' }
25+
skip_if_not_exported <- function(fn_name, pkg = testthat::testing_package()) {
26+
rlang::check_installed(
27+
"pkgload",
28+
"to decide whether to skip a test for unexported functions."
29+
)
30+
fn_name <- stbl::to_chr_scalar(fn_name)
31+
pkg <- stbl::to_chr_scalar(pkg)
32+
if (!pkgload::is_dev_package(pkg)) {
33+
# Load non-dev package to check its exports.
34+
rlang::check_installed(
35+
pkg,
36+
paste0("to decide whether to skip a test for ", pkg, "::", fn_name, "().")
37+
)
38+
require(pkg, character.only = TRUE, quietly = TRUE)
39+
}
40+
if (!fn_name %in% getNamespaceExports(pkg)) {
41+
testthat::skip(paste0(fn_name, "() is not exported from ", pkg))
42+
}
43+
invisible(NULL)
44+
}

‎inst/templates/test-paths.R‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
with_mock_dir("api/path/{{tag}}/{{fn_prefix}}{{operation_id}}", {
77
test_that("{{fn_prefix}}{{operation_id}}() returns expected result", {
8+
skip_if_not_exported("{{fn_prefix}}{{operation_id}}")
89
expect_snapshot({
910
test_result <- {{fn_prefix}}{{operation_id}}({{{test_args}}})
1011
test_result

‎man/skip_if_not_exported.Rd‎

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/_fixtures/000-create_fixtures.R‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ apid_url |>
1010
rapid_filename = "_beekeeper_rapid.rds"
1111
)
1212
generate_pkg(pkg_dir = pkg_dir)
13+
# testthat paths are too long for tarballs, so move them up a level.
14+
fs::file_move(
15+
fs::path(pkg_dir, "tests", "testthat"),
16+
fs::path(pkg_dir, "testthat")
17+
)
18+
# R CMD check doesn't like the .Rbuildignore files, and we don't use them.
19+
fs::file_delete(fs::path(pkg_dir, ".Rbuildignore"))
1320

1421
apid_url <- "https://api.apis.guru/v2/specs/fec.gov/1.0/openapi.yaml"
1522
api_abbr <- "fec"
@@ -50,6 +57,13 @@ fec_rapid |>
5057
rapid_filename = "fec_subset_rapid.rds"
5158
)
5259
generate_pkg(config_filename = "fec_subset_beekeeper.yml", pkg_dir = pkg_dir)
60+
# testthat paths are too long for tarballs, so move them up a level.
61+
fs::file_move(
62+
fs::path(pkg_dir, "tests", "testthat"),
63+
fs::path(pkg_dir, "testthat")
64+
)
65+
# R CMD check doesn't like the .Rbuildignore files, and we don't use them.
66+
fs::file_delete(fs::path(pkg_dir, ".Rbuildignore"))
5367

5468
apid_url <- "https://api.apis.guru/v2/specs/trello.com/1.0/openapi.yaml"
5569
api_abbr <- "trello"
@@ -73,3 +87,10 @@ trello_rapid |>
7387
rapid_filename = "_beekeeper_rapid.rds"
7488
)
7589
generate_pkg(pkg_dir = pkg_dir)
90+
# testthat paths are too long for tarballs, so move them up a level.
91+
fs::file_move(
92+
fs::path(pkg_dir, "tests", "testthat"),
93+
fs::path(pkg_dir, "testthat")
94+
)
95+
# R CMD check doesn't like the .Rbuildignore files, and we don't use them.
96+
fs::file_delete(fs::path(pkg_dir, ".Rbuildignore"))

‎tests/testthat/_fixtures/fec/.Rbuildignore‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
File renamed without changes.

tests/testthat/_fixtures/fec/tests/testthat/test-010-prepare.R renamed to tests/testthat/_fixtures/fec/testthat/test-010-prepare.R

File renamed without changes.

0 commit comments

Comments
 (0)