Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with webR #1714

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Depends:
R (>= 3.1)
Imports:
brio,
callr (>= 3.5.1),
cli (>= 3.4.0),
desc,
digest,
Expand All @@ -30,19 +29,20 @@ Imports:
methods,
pkgload,
praise,
processx,
ps (>= 1.3.4),
R6 (>= 2.2.0),
rlang (>= 1.0.1),
rlang (>= 1.0.2),
utils,
waldo (>= 0.4.0),
withr (>= 2.4.3)
Suggests:
callr (>= 3.5.1),
covr,
curl (>= 0.9.5),
diffviewer (>= 0.1.0),
knitr,
mockery,
processx,
ps (>= 1.3.4),
rmarkdown,
rstudioapi,
shiny,
Expand Down
1 change: 1 addition & 0 deletions R/parallel-taskq.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ task_q <- R6::R6Class(
"task_q",
public = list(
initialize = function(concurrency = 4L, ...) {
rlang::check_installed(c("callr", "processx", "ps"))
private$start_workers(concurrency, ...)
invisible(self)
},
Expand Down
19 changes: 19 additions & 0 deletions R/test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ test_code <- function(test, code, env = test_env(), reporter = get_reporter(), s
e[["handled"]] <- TRUE
test_error <<- e
}
handle_package_not_found_error <- function(missing_pkg) {
pkg <- Sys.getenv("TESTTHAT_PKG")
if (!nzchar(pkg)) {
return(zap())
}

desc <- desc::desc(package = pkg)
if (is.null(desc)) {
return(zap())
}

if (missing_pkg %in% desc$get_list("Suggests")) {
skip_if_not_installed(missing_pkg)
}

zap()
}
handle_fatal <- function(e) {
handled <<- TRUE
# Error caught in handle_error() has precedence
Expand Down Expand Up @@ -203,6 +220,8 @@ test_code <- function(test, code, env = test_env(), reporter = get_reporter(), s
}
},
expectation = handle_expectation,
rlib_error_package_not_found = function(cnd) handle_package_not_found_error(cnd$pkg),
packageNotFoundError = function(cnd) handle_package_not_found_error(cnd$package),
skip = handle_skip,
warning = handle_warning,
message = handle_message,
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-parallel-crash.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
test_that("crash", {
# https://github.com/r-lib/rlang/commit/c84d52b6
skip_if_not_installed("rlang", "1.0.1.9000")

skip_on_cran()
skip_on_covr()
skip_if_not_installed("callr")

withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")

do <- function() {
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,25 @@ test_that("no braces required in testthat 2e", {
NA
)
})

test_that("packageNotFoundError causes skips for Suggested packages", {
# No `packageNotFoundError` on old R versions
skip_if(getRversion() < "3.6.0")

path <- test_path("testSuggestsSkip")
results <- test_local(
path,
reporter = "silent",
stop_on_failure = FALSE
)
results

for (res in results[1:2]) {
skip <- res$results[[1]]
expect_s3_class(skip, "expectation_skip")
expect_match(conditionMessage(skip), "foobarbaz cannot be loaded")
}

fail <- results[[3]]$results[[1]]
expect_s3_class(fail, "error")
})
16 changes: 16 additions & 0 deletions tests/testthat/testSuggestsSkip/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Package: testSuggestsSkip
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
Suggests:
testthat (>= 3.0.0),
foobarbaz
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions tests/testthat/testSuggestsSkip/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by roxygen2: do not edit by hand

12 changes: 12 additions & 0 deletions tests/testthat/testSuggestsSkip/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/tests.html
# * https://testthat.r-lib.org/reference/test_package.html#special-files

library(testthat)
library(testSuggestsSkip)

test_check("testSuggestsSkip")
11 changes: 11 additions & 0 deletions tests/testthat/testSuggestsSkip/tests/testthat/test-foobarbaz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("attaching foobarbaz skips because in Suggests", {
library("foobarbaz")
})

test_that("checking foobarbaz skips because in Suggests", {
rlang::check_installed("foobarbaz")
})

test_that("attaching quux fails because not in Suggests", {
library("quux")
})