Skip to content

Commit

Permalink
Allow configuration of load_all() call (#1637)
Browse files Browse the repository at this point in the history
And load helpers to the search path by default to match previous behaviour.

Closes #1636
  • Loading branch information
lionel- authored Jun 2, 2022
1 parent 2f48ca6 commit 426eb52
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 5 deletions.
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# testthat (development version)

* You can now configure the behaviour of the implicit
`devtools::load_all()` call performed by `devtools::test()` in your
package DESCRIPTION file (#1636). To disable exports of internal
functions and of testthat helpers, use:

```
Config/testthat/load-all: list(export_all = FALSE, helpers = FALSE)
```

Helpers are now attached on the search path by default after calling
`devtools::test()`.

# testthat 3.1.4

* Minor tweaks to output for latest cli (#1606).
Expand Down
48 changes: 43 additions & 5 deletions R/test-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
#' * "none", the default, doesn't load the package.
#' * "installed", uses [library()] to load an installed package.
#' * "source", uses [pkgload::load_all()] to a source package.
#' To configure the arguments passed to `load_all()`, add this
#' field in your DESCRIPTION file:
#'
#' ```
#' Config/testthat/load-all: list(export_all = FALSE, helpers = FALSE)
#' ```
#' @param wrap DEPRECATED
#' @keywords internal
#' @return A list (invisibly) containing data about the test results.
Expand Down Expand Up @@ -217,15 +223,47 @@ test_files_setup_env <- function(test_package,
library(testthat)

load_package <- arg_match(load_package)
switch(load_package,
none = {},
installed = library(test_package, character.only = TRUE),
source = pkgload::load_all(test_dir, helpers = FALSE, quiet = TRUE)
)
if (load_package == "installed") {
library(test_package, character.only = TRUE)
} else if (load_package == "source") {
# Allow configuring what we export to the search path (#1636)
args <- find_load_all_args(test_dir)
pkgload::load_all(
test_dir,
export_all = args[["export_all"]],
helpers = args[["helpers"]],
quiet = TRUE
)
}

env %||% test_env(test_package)
}

find_load_all_args <- function(path) {
default <- list(export_all = TRUE, helpers = TRUE)

desc <- find_description(path)
if (is.null(desc)) {
return(default)
}

args <- desc$get_field("Config/testthat/load-all", default = NULL)
if (is.null(args)) {
return(default)
}

args <- parse_expr(args)
if (!is_call(args, "list")) {
abort("`Config/testthat/load-all` must be a list.", call = NULL)
}

args <- as.list(args[-1])
list(
export_all = args[["export_all"]] %||% default[["export_all"]],
helpers = args[["helpers"]] %||% default[["helpers"]]
)
}

test_files_setup_state <- function(test_dir, test_package, load_helpers, env, .env = parent.frame()) {

# Define testing environment
Expand Down
5 changes: 5 additions & 0 deletions man/test_dir.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-test-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,25 @@ test_that("can filter test scripts", {
expect_equal(filter_test_scripts(x, ".R"), character())
})

# ----------------------------------------------------------------------

test_that("can configure `load_all()` (#1636)", {
path <- test_path("testConfigLoadAll")

args <- find_load_all_args(path)
expect_equal(args, list(export_all = FALSE, helpers = FALSE))

results <- test_local(path, reporter = "silent")
for (res in results) {
expect_equal(sum(res[["failed"]]), 0)
}
})

test_that("helpers are attached to the search path after running `devtools::test()`", {
# This is FALSE under R CMD check
skip_if_not(pkgload::is_dev_package("testthat"))

expect_true(
"rlang_version" %in% names(pkg_env("testthat"))
)
})
18 changes: 18 additions & 0 deletions tests/testthat/testConfigLoadAll/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Package: testConfigLoadAll
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0
Config/testthat/load-all: list(export_all = FALSE, helpers = FALSE)
Imports:
rlang
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
3 changes: 3 additions & 0 deletions tests/testthat/testConfigLoadAll/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(exported)
4 changes: 4 additions & 0 deletions tests/testthat/testConfigLoadAll/R/config.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
internal <- function() NULL

#' @export
exported <- function() NULL
12 changes: 12 additions & 0 deletions tests/testthat/testConfigLoadAll/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/tests.html
# * https://testthat.r-lib.org/reference/test_package.html#special-files

library(testthat)
library(testConfigLoadAll)

test_check("testConfigLoadAll")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my_helper <- function() NULL
11 changes: 11 additions & 0 deletions tests/testthat/testConfigLoadAll/tests/testthat/test-config.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("helpers are not on the path", {
expect_false(
"my_helper" %in% names(rlang::pkg_env("testConfigLoadAll"))
)
})

test_that("internal functions are not on the path", {
fns <- names(rlang::pkg_env("testConfigLoadAll"))
expect_false("internal" %in% fns)
expect_true("exported" %in% fns)
})

0 comments on commit 426eb52

Please sign in to comment.