Skip to content

Commit

Permalink
Set package in test_active_file() (#2471)
Browse files Browse the repository at this point in the history
* Set package in `test_active_file()`, and update tests so they actually fail if not set correctly. Fixes #2470

* Re-load testthat outside of testthat so that devtools works with testthat once more.
  • Loading branch information
hadley authored Sep 21, 2022
1 parent c9696a6 commit 9e2793a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Imports:
rversions (>= 2.1.1),
sessioninfo (>= 1.2.2),
stats,
testthat (>= 3.1.4),
testthat (>= 3.1.4.9000),
tools,
urlchecker (>= 1.0.1),
utils,
Expand Down Expand Up @@ -68,3 +68,5 @@ Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
Config/testthat/edition: 3
Remotes:
r-lib/testthat
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# devtools (development version)

* `test_active_file()` passes the package onto to testthat so it can correctly
set the `TESTTHAT_PKG` envvar (#2470).

* You can once again use `devtools::test()` and `devtools::test_active_file()`
with the testthat package.

* `check(cran = TRUE)` sets the env var
`_R_CHECK_PACKAGES_USED_IGNORE_UNUSED_IMPORTS_` to `FALSE`, in order to
surface the `"Namespace in Imports field not imported from"` NOTE. This only
Expand Down
23 changes: 22 additions & 1 deletion R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ test <- function(pkg = ".", filter = NULL, stop_on_failure = FALSE, export_all =

cli::cli_inform(c(i = "Testing {.pkg {pkg$package}}"))
withr::local_envvar(r_env_vars())

load_package <- load_package_if_needed(pkg)
testthat::test_local(
pkg$path,
filter = filter,
stop_on_failure = stop_on_failure,
load_package = load_package,
...
)
}
Expand All @@ -63,7 +66,25 @@ test_active_file <- function(file = find_active_file(), ...) {
if (is_rstudio_running()) {
rstudioapi::executeCommand("activateConsole", quiet = TRUE)
}
testthat::test_file(test_files, ..., load_package = "source")

load_package <- load_package_if_needed(pkg)
testthat::test_file(
test_files,
package = pkg$package,
load_package = load_package,
...
)
}

load_package_if_needed <- function(pkg) {
if (pkg$package == "testthat") {
# Must load testthat outside of testthat so tests are run with
# dev testthat
load_all(pkg$path, quiet = TRUE)
"none"
} else {
"source"
}
}

#' @param show_report Show the test coverage report.
Expand Down
15 changes: 13 additions & 2 deletions tests/testthat/test-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ test_that("devtools::test_active_file works", {
expect_equal(length(out), 1)
})

test_that("TESTTHAT_PKG environment varaible is set", {
test_test(test_path("testTest"), filter = "envvar")
test_that("TESTTHAT_PKG environment variable is set", {
withr::local_envvar("TESTTHAT_PKG" = "incorrect")

test_test(
test_path("testTest"),
filter = "envvar",
stop_on_failure = TRUE
)
test_active_file(
test_path("testTest/tests/testthat/test-envvar.R"),
stop_on_failure = TRUE
)

expect_true(TRUE)
})

Expand Down

0 comments on commit 9e2793a

Please sign in to comment.