Skip to content

Commit

Permalink
Make optional test deps really optional (#248)
Browse files Browse the repository at this point in the history
* Make optional test deps really optional

Cf. #247.

* Skip test if withr is not installed

* Trying to fix GHA

Need an older magick package on Windows && R < 4.0.0

* Give up on R 3.6.x on Windows :(

Can't build an older version of magick, either.
  • Loading branch information
gaborcsardi authored Mar 22, 2024
1 parent 9a61008 commit 371467a
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ jobs:
- {os: macos-latest, r: 'release'}

- {os: windows-latest, r: 'release'}
# Use 3.6 to trigger usage of RTools35
- {os: windows-latest, r: '3.6'}
# use 4.1 to check with rtools40's older compiler
- {os: windows-latest, r: '4.1'}

Expand All @@ -47,6 +45,7 @@ jobs:
- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
id: setup-r
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
Expand Down
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Imports:
Suggests:
asciicast (>= 2.3.1),
cli (>= 1.1.0),
covr,
mockery,
ps,
rprojroot,
Expand Down
25 changes: 25 additions & 0 deletions R/test-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

is_true_check_env_var <- function(x, default = "") {
# like utils:::str2logical
val <- Sys.getenv(x, default)
if (isTRUE(as.logical(val))) return(TRUE)
tolower(val) %in% c("1", "yes")
}

isFALSE <- function(x) {
is.logical(x) && length(x) == 1L && !is.na(x) && !x
}

is_false_check_env_var <- function(x, default = "") {
# like utils:::str2logical
val <- Sys.getenv(x, default)
if (isFALSE(as.logical(val))) return(TRUE)
tolower(val) %in% c("0", "no")
}

# Only skip if _R_CHECK_FORCE_SUGGESTS_ is false

skip_if_not_installed <- function(pkg) {
if (!is_false_check_env_var("_R_CHECK_FORCE_SUGGESTS_")) return()
testthat::skip_if_not_installed(pkg)
}
12 changes: 10 additions & 2 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
library(testthat)

library(callr)

test_check("callr")
if (callr:::is_false_check_env_var("_R_CHECK_FORCE_SUGGESTS_")) {
if (requireNamespace("testthat", quietly = TRUE)) {
library(testthat)
test_check("callr")
}
} else {
library(testthat)
test_check("callr")
}
6 changes: 6 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ read_next <- function(x, timeout = 3000) {
}

has_locale <- function(l) {
skip_if_not_installed("withr")
has <- TRUE
tryCatch(
withr::with_locale(c(LC_CTYPE = l), "foobar"),
Expand Down Expand Up @@ -99,6 +100,7 @@ test_paths <- function(callr_drop, callr_keep,

test_temp_file <- function(fileext = "", pattern = "test-file-",
envir = parent.frame(), create = TRUE) {
skip_if_not_installed("withr")
tmp <- tempfile(pattern = pattern, fileext = fileext)
if (identical(envir, .GlobalEnv)) {
message("Temporary files will _not_ be cleaned up")
Expand Down Expand Up @@ -126,6 +128,7 @@ expect_error <- function(..., class = "error") {
}

test_package_root <- function() {
skip_if_not_installed("rprojroot")
x <- tryCatch(
rprojroot::find_package_root_file(),
error = function(e) NULL)
Expand Down Expand Up @@ -162,6 +165,9 @@ without_env <- function(f) {

expect_r_process_snapshot <- function(..., interactive = TRUE, echo = TRUE,
transform = NULL, variant = NULL) {
skip_if_not_installed("asciicast")
skip_if_not_installed("withr")

# Skip these tests on platforms where V8 is not available
if (! R.Version()$arch %in% c("i386", "x86_64", "aarch64") &&
! requireNamespace("asciicast", quietly = TRUE)) {
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-bugs.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

test_that("repos is a list, #82", {
skip_if_not_installed("withr")
expect_true(withr::with_options(
list(repos = list(CRAN = "https://cloud.r-project.org")),
callr::r(function() inherits(getOption("repos"), "list"))
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-clean-subprocess.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

test_that("r() does not load anything", {
skip_in_covr()
skip_if_not_installed("withr")
pkgs <- withr::with_envvar(
clean_envvars(),
r(without_env(function() loadedNamespaces())))
Expand All @@ -11,6 +12,7 @@ test_that("r() does not load anything", {

test_that("r_bg() does not load anything", {
skip_in_covr()
skip_if_not_installed("withr")
p <- withr::with_envvar(
clean_envvars(),
r_bg(without_env(function() loadedNamespaces())))
Expand All @@ -24,6 +26,7 @@ test_that("r_bg() does not load anything", {

test_that("r_session does not load anything", {
skip_in_covr()
skip_if_not_installed("withr")
rs <- withr::with_envvar(clean_envvars(), r_session$new())
on.exit(rs$close(), add = TRUE)
pkgs <- rs$run(without_env(function() loadedNamespaces()))
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-error.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test_that("error stack is passed, .Last.error is set", {
})

test_that("error behavior can be set using option", {
skip_if_not_installed("withr")
withr::local_options(callr.error = "error")
expect_snapshot(
error = TRUE,
Expand All @@ -51,6 +52,7 @@ test_that("error behavior can be set using option", {
})

test_that("parent errors", {
skip_if_not_installed("withr")
withr::local_options(list("callr.error" = "error"))
expect_snapshot({
err <- tryCatch(
Expand All @@ -62,6 +64,7 @@ test_that("parent errors", {
})

test_that("parent errors, another level", {
skip_if_not_installed("withr")
withr::local_options(list("callr.error" = "error"))
expect_snapshot({
err <- tryCatch(
Expand All @@ -85,6 +88,7 @@ test_that("error traces are printed recursively", {
})

test_that("errors in r_bg() are merged", {
skip_if_not_installed("withr")
withr::local_options(list("callr.error" = "error"))

p <- r_bg(function() 1 + "A")
Expand All @@ -98,6 +102,7 @@ test_that("errors in r_bg() are merged", {
})

test_that("errors in r_process are merged", {
skip_if_not_installed("withr")
withr::local_options(list("callr.error" = "error"))

opts <- r_process_options(func = function() 1 + "A")
Expand Down Expand Up @@ -195,6 +200,7 @@ test_that("format.call_status_error", {
})

test_that("format.call_status_error 2", {
skip_if_not_installed("withr")
expect_r_process_snapshot(
withr::local_options(rlib_error_always_trace = TRUE),
err <- tryCatch(
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ test_that("stdout and stderr in the same file", {
})

test_that("profiles are used as requested", {
skip_if_not_installed("withr")
do <- function(system, user) {
tmp1 <- tempfile()
tmp2 <- tempfile()
Expand Down Expand Up @@ -114,6 +115,7 @@ test_that("profiles are used as requested", {
})

test_that(".Renviron is used, but lib path is set over it", {
skip_if_not_installed("withr")
dir.create(tmp <- tempfile())
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
withr::with_dir(tmp, {
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-libpath.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test_that(".Library.site", {
})

test_that(".libPaths()", {
skip_if_not_installed("withr")
dir.create(tmp <- tempfile())
on.exit(unlink(tmp, recursive = TRUE))

Expand All @@ -35,6 +36,7 @@ test_that("if .Renviron overrides R_PROFILE", {
## But we still need to use the proper lib path, as set in the fake
## profile
skip_in_covr()
skip_if_not_installed("withr")

cat("Sys.setenv(FOO='nope')\n", file = tmp_prof <- tempfile())
cat("R_PROFILE=\"", tmp_prof, "\"\n", file = tmp_env <- tempfile(), sep = "")
Expand Down Expand Up @@ -64,6 +66,7 @@ test_that("libpath in system(), empty .Renviron", {
# We remove the library with covr from the lib path, so this
# cannot work in a subprocess.
skip_in_covr()
skip_if_not_installed("withr")

dir.create(tmpdrop <- tempfile("drop"))
dir.create(tmpkeep <- tempfile("keep"))
Expand All @@ -86,6 +89,7 @@ test_that("libpath in system, R_LIBS in .Renviron", {
# We remove the library with covr from the lib path, so this
# cannot work in a subprocess.
skip_in_covr()
skip_if_not_installed("withr")

dir.create(tmpdrop <- tempfile("drop"))
dir.create(tmpkeep <- tempfile("keep"))
Expand All @@ -109,6 +113,7 @@ test_that("libpath in system, R_LIBS", {
# We remove the library with covr from the lib path, so this
# cannot work in a subprocess.
skip_in_covr()
skip_if_not_installed("withr")

dir.create(tmpdrop <- tempfile("drop"))
dir.create(tmpkeep <- tempfile("keep"))
Expand All @@ -132,6 +137,7 @@ test_that("libpath in system, R_LIBS and .Renviron", {
# We remove the library with covr from the lib path, so this
# cannot work in a subprocess.
skip_in_covr()
skip_if_not_installed("withr")

dir.create(tmpdrop <- tempfile("drop"))
dir.create(tmpkeep <- tempfile("keep"))
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-load-client.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test_that("load_client_lib", {
})

test_that("errors", {
skip_if_not_installed("mockery")
mockery::stub(load_client_lib, "system.file", "")
expect_error(
load_client_lib(),
Expand All @@ -22,6 +23,7 @@ test_that("errors", {
})

test_that("errors 2", {
skip_if_not_installed("mockery")
sofile <- system.file(
"libs", .Platform$r_arch, paste0("client", .Platform$dynlib.ext),
package = "processx"
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-messages.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

test_that("messages in callr::r do not crash session", {
skip_if_not_installed("cli")
ret <- r(function() { cli::cli_text("fooobar"); 1 + 1 })
expect_identical(ret, 2)
gc()
Expand All @@ -8,6 +9,7 @@ test_that("messages in callr::r do not crash session", {
test_that("messages in callr::r_bg do not crash session", {
skip_in_covr() # TODO: what wrong with this on Windows?
skip_on_cran()
skip_if_not_installed("cli")

rx <- r_bg(function() { cli::cli_text("fooobar"); 1 + 1 })
rx$wait(5000)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-presets.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

test_that("r", {
skip_if_not_installed("withr")

withr::with_options(
list(repos = "foobar"),
Expand Down Expand Up @@ -34,6 +35,7 @@ test_that("r_safe", {

## https://github.com/r-lib/callr/issues/66
test_that("names of getOption('repos') are preserved", {
skip_if_not_installed("withr")
repos <- withr::with_options(
list(repos = c(foo = "bar")),
callr::r(function() getOption("repos"))
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-r-session-messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test_that("callr_message, then error", {
})

test_that("message handlers", {
skip_if_not_installed("withr")
rs <- r_session$new()
on.exit(rs$kill(), add = TRUE)

Expand All @@ -58,6 +59,7 @@ test_that("message handlers", {
})

test_that("large messages", {
skip_if_not_installed("withr")
rs <- r_session$new()
on.exit(rs$close(), add = TRUE)

Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-r-session.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ test_that("custom load hook", {
})

test_that("traceback", {
skip_if_not_installed("withr")
withr::local_options(callr.traceback = TRUE)
rs <- r_session$new()
on.exit(rs$kill(), add = TRUE)
Expand Down Expand Up @@ -301,6 +302,7 @@ test_that("error in the load hook", {
})

test_that("fds are not leaked", {
skip_if_not_installed("ps")
rs <- r_session$new()
on.exit(rs$kill(), add = TRUE)

Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-rcmd.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test_that("rcmd show works", {
})

test_that("rcmd echo works", {
skip_if_not_installed("withr")
withr::local_options(width = 500)
expect_output(rcmd("config", "CC", echo = TRUE), "config\\s+CC")
gc()
Expand Down Expand Up @@ -39,6 +40,7 @@ test_that("wd argument", {
})

test_that("fail_on_status", {
skip_if_not_installed("withr")
rand <- tempfile()
expect_error(
withr::with_dir(
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-spelling.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
test_that("spell check", {
skip_on_cran()
skip_in_covr()
skip_if_not_installed("spelling")
pkg_dir <- test_package_root()
results <- spelling::spell_check_package(pkg_dir)

Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

test_that("is_complete_expression", {
skip_if_not_installed("withr")
do_tests <- function() {
expect_true(is_complete_expression(""))
expect_true(is_complete_expression("1"))
Expand All @@ -20,6 +21,7 @@ test_that("is_complete_expression", {
})

test_that("default_repos", {
skip_if_not_installed("withr")
def <- "https://cloud.r-project.org"

withr::with_options(list(repos = NULL),
Expand Down

0 comments on commit 371467a

Please sign in to comment.