From 64d33bc1d4373fa09dc60232da7f161ae628e10e Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 20 Sep 2022 16:46:18 -0500 Subject: [PATCH] Set package in test_active_file() And update tests so they actually fail if not set correctly Fixes #2470 --- R/test.R | 7 ++++++- tests/testthat/test-test.R | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/R/test.R b/R/test.R index 1b7a92ff7..a8898bfa1 100644 --- a/R/test.R +++ b/R/test.R @@ -63,7 +63,12 @@ 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") + testthat::test_file( + test_files, + package = pkg$package, + ..., + load_package = "source" + ) } #' @param show_report Show the test coverage report. diff --git a/tests/testthat/test-test.R b/tests/testthat/test-test.R index ab40cb38f..8ebf00036 100644 --- a/tests/testthat/test-test.R +++ b/tests/testthat/test-test.R @@ -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) })