Skip to content

Commit 9e2793a

Browse files
authored
Set package in test_active_file() (#2471)
* 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.
1 parent c9696a6 commit 9e2793a

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Imports:
3535
rversions (>= 2.1.1),
3636
sessioninfo (>= 1.2.2),
3737
stats,
38-
testthat (>= 3.1.4),
38+
testthat (>= 3.1.4.9000),
3939
tools,
4040
urlchecker (>= 1.0.1),
4141
utils,
@@ -68,3 +68,5 @@ Language: en-US
6868
Roxygen: list(markdown = TRUE)
6969
RoxygenNote: 7.2.1
7070
Config/testthat/edition: 3
71+
Remotes:
72+
r-lib/testthat

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# devtools (development version)
22

3+
* `test_active_file()` passes the package onto to testthat so it can correctly
4+
set the `TESTTHAT_PKG` envvar (#2470).
5+
6+
* You can once again use `devtools::test()` and `devtools::test_active_file()`
7+
with the testthat package.
8+
39
* `check(cran = TRUE)` sets the env var
410
`_R_CHECK_PACKAGES_USED_IGNORE_UNUSED_IMPORTS_` to `FALSE`, in order to
511
surface the `"Namespace in Imports field not imported from"` NOTE. This only

R/test.R

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ test <- function(pkg = ".", filter = NULL, stop_on_failure = FALSE, export_all =
3737

3838
cli::cli_inform(c(i = "Testing {.pkg {pkg$package}}"))
3939
withr::local_envvar(r_env_vars())
40+
41+
load_package <- load_package_if_needed(pkg)
4042
testthat::test_local(
4143
pkg$path,
4244
filter = filter,
4345
stop_on_failure = stop_on_failure,
46+
load_package = load_package,
4447
...
4548
)
4649
}
@@ -63,7 +66,25 @@ test_active_file <- function(file = find_active_file(), ...) {
6366
if (is_rstudio_running()) {
6467
rstudioapi::executeCommand("activateConsole", quiet = TRUE)
6568
}
66-
testthat::test_file(test_files, ..., load_package = "source")
69+
70+
load_package <- load_package_if_needed(pkg)
71+
testthat::test_file(
72+
test_files,
73+
package = pkg$package,
74+
load_package = load_package,
75+
...
76+
)
77+
}
78+
79+
load_package_if_needed <- function(pkg) {
80+
if (pkg$package == "testthat") {
81+
# Must load testthat outside of testthat so tests are run with
82+
# dev testthat
83+
load_all(pkg$path, quiet = TRUE)
84+
"none"
85+
} else {
86+
"source"
87+
}
6788
}
6889

6990
#' @param show_report Show the test coverage report.

tests/testthat/test-test.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@ test_that("devtools::test_active_file works", {
3131
expect_equal(length(out), 1)
3232
})
3333

34-
test_that("TESTTHAT_PKG environment varaible is set", {
35-
test_test(test_path("testTest"), filter = "envvar")
34+
test_that("TESTTHAT_PKG environment variable is set", {
35+
withr::local_envvar("TESTTHAT_PKG" = "incorrect")
36+
37+
test_test(
38+
test_path("testTest"),
39+
filter = "envvar",
40+
stop_on_failure = TRUE
41+
)
42+
test_active_file(
43+
test_path("testTest/tests/testthat/test-envvar.R"),
44+
stop_on_failure = TRUE
45+
)
46+
3647
expect_true(TRUE)
3748
})
3849

0 commit comments

Comments
 (0)