From 95d1c101596fe76be181234db4eee8c5d2ef04a9 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Sat, 9 Aug 2025 10:35:14 -0400 Subject: [PATCH 1/2] Don't override user-specified NOT_CRAN env var Continutation of #2112 --- R/auto-test.R | 2 +- R/local.R | 3 +-- R/skip.R | 8 ++++++++ R/test-package.R | 2 +- tests/testthat/test-skip.R | 14 ++++++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/R/auto-test.R b/R/auto-test.R index 4a714ba82..97f558c94 100644 --- a/R/auto-test.R +++ b/R/auto-test.R @@ -83,7 +83,7 @@ auto_test_package <- function( test_path <- normalizePath(file.path(path, "tests", "testthat")) # Start by loading all code and running all tests - withr::local_envvar("NOT_CRAN" = "true") + local_assume_not_on_cran() pkgload::load_all(path) test_dir( test_path, diff --git a/R/local.R b/R/local.R index 0d1613c95..621fa27d3 100644 --- a/R/local.R +++ b/R/local.R @@ -190,8 +190,7 @@ local_test_directory <- function(path, package = NULL, .env = parent.frame()) { } local_interactive_reporter <- function(.env = parent.frame()) { - # Definitely not on CRAN - withr::local_envvar(NOT_CRAN = "true", .local_envir = .env) + local_assume_not_on_cran() withr::local_options(testthat_interactive = TRUE, .local_envir = .env) # Use edition from working directory diff --git a/R/skip.R b/R/skip.R index b085db721..2e73c445f 100644 --- a/R/skip.R +++ b/R/skip.R @@ -190,6 +190,14 @@ local_on_cran <- function(on_cran, frame = caller_env()) { withr::local_envvar(NOT_CRAN = tolower(!on_cran), .local_envir = frame) } +# Assert that we're not on CRAN, but don't override the user's setting +local_assume_not_on_cran <- function(frame = caller_env()) { + if (Sys.getenv("NOT_CRAN") != "") { + return() + } + withr::local_envvar("NOT_CRAN" = "true", .local_envir = frame) +} + #' @export #' @param os Character vector of one or more operating systems to skip on. #' Supported values are `"windows"`, `"mac"`, `"linux"`, `"solaris"`, diff --git a/R/test-package.R b/R/test-package.R index d0f7f435a..f3897b7ef 100644 --- a/R/test-package.R +++ b/R/test-package.R @@ -68,7 +68,7 @@ test_local <- function( package <- pkgload::pkg_name(path) test_path <- file.path(pkgload::pkg_path(path), "tests", "testthat") - withr::local_envvar(NOT_CRAN = "true") + local_assume_not_on_cran() test_dir( test_path, package = package, diff --git a/tests/testthat/test-skip.R b/tests/testthat/test-skip.R index 98e23de99..564d851d2 100644 --- a/tests/testthat/test-skip.R +++ b/tests/testthat/test-skip.R @@ -88,6 +88,20 @@ test_that("local_on_cran sets NOT_CRAN", { }) }) +test_that("local_assume_not_on_cran() sets NOT_CRAN if not already set", { + withr::local_envvar(NOT_CRAN = NA) + local({ + local_assume_not_on_cran() + expect_equal(Sys.getenv("NOT_CRAN"), "true") + }) + + withr::local_envvar(NOT_CRAN = "false") + local({ + local_assume_not_on_cran() + expect_equal(Sys.getenv("NOT_CRAN"), "false") + }) +}) + test_that("skip_on_ci() works as expected", { withr::local_envvar(CI = "false") expect_no_skip(skip_on_ci()) From 879ed31eceeee7dcf5e32533b58a111133f04f5e Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 13 Aug 2025 07:24:46 -0500 Subject: [PATCH 2/2] Fix missing frame propagation --- R/local.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/local.R b/R/local.R index 621fa27d3..1f8b88377 100644 --- a/R/local.R +++ b/R/local.R @@ -190,7 +190,7 @@ local_test_directory <- function(path, package = NULL, .env = parent.frame()) { } local_interactive_reporter <- function(.env = parent.frame()) { - local_assume_not_on_cran() + local_assume_not_on_cran(.env) withr::local_options(testthat_interactive = TRUE, .local_envir = .env) # Use edition from working directory