mirror of
https://github.com/msberends/AMR.git
synced 2026-11-16 01:30:54 +01:00
(v3.0.1.9114) Fix errors without cli and on R 3.0, and make the tinytest pipeline actually run the unit tests
Package: - stop_() called cli::ansi_has_hyperlink_support() before checking that cli is available, so without cli every error became "there is no package called 'cli'" - interpretive_rules() failed on R 3.0 for guidelines without AmpC rules (assignment to zero rows) - Guard the ggplot2 example of wisca_plot() CI: - check-old: R 3.0 could not install tinytest over HTTPS, so no unit tests ran; install it with curl, and fail when tinytest is missing or any test fails - tinytest shim: record expectations in the test file environment, support skips and errors per test_that() block, add missing testthat equivalents, source helper files - Add R 4.0 (oldrel-6) to check-old, and oldrel-5 to check-recent
This commit is contained in:
1 parent
f0699c6584
commit
9e609dae83
13 files changed
+68
-32
No files matched your search
@@ -98,7 +98,7 @@ mo_repository_root <- function() {
|
||||
NULL
|
||||
}
|
||||
skip_if_no_mo_repository <- function() {
|
||||
testthat::skip_if(is.null(mo_repository_root()), "Source repository not available (data-raw/ does not ship with the package)")
|
||||
skip_if(is.null(mo_repository_root()), "Source repository not available (data-raw/ does not ship with the package)")
|
||||
}
|
||||
|
||||
# names without a rank suffix, e.g. "Kapabacteria {class}" and "Nitrospira (class)" are both the class
|
||||
|
||||
@@ -37,7 +37,7 @@ test_that("test-_deprecated.R", {
|
||||
# deprecated custom_interpretive_rules() still works and emits a warning
|
||||
expect_warning(
|
||||
x_old <- custom_eucast_rules(AMC == "R" ~ aminopenicillins == "R"),
|
||||
regexp = "custom_eucast_rules"
|
||||
"custom_eucast_rules"
|
||||
)
|
||||
expect_inherits(x_old, "custom_interpretive_rules")
|
||||
}
|
||||
|
||||
@@ -62,8 +62,10 @@ test_that("test-misc.R", {
|
||||
check_df <- function(check_element, return_val = 0) {
|
||||
is_right <<- FALSE
|
||||
for (env in sys.frames()) {
|
||||
if (!is.null(env[[check_element]]) && is.data.frame(env[[check_element]])) {
|
||||
is_right <<- all(colnames(example_isolates) %in% colnames(env[[check_element]]))
|
||||
# in R < 4.2, reading a promise that is still being evaluated (such as `x` in tibble's `[`) gives an error
|
||||
val <- tryCatch(env[[check_element]], error = function(e) NULL)
|
||||
if (!is.null(val) && is.data.frame(val)) {
|
||||
is_right <<- all(colnames(example_isolates) %in% colnames(val))
|
||||
}
|
||||
}
|
||||
return_val
|
||||
|
||||
@@ -242,9 +242,10 @@ test_that("test-interpretive_rules.R", {
|
||||
tolerance = 0.5
|
||||
)
|
||||
|
||||
# clsi_rules() no longer errors (returns data unchanged until CLSI rows are added)
|
||||
# clsi_rules() no longer errors (returns data unchanged until CLSI rows are added);
|
||||
# without the tibble package, a plain data.frame is returned
|
||||
expect_identical(
|
||||
suppressWarnings(clsi_rules(example_isolates, info = FALSE)),
|
||||
example_isolates
|
||||
if (AMR:::pkg_is_available("tibble")) example_isolates else as.data.frame(example_isolates, stringsAsFactors = FALSE)
|
||||
)
|
||||
})
|
||||
+30
-7
@@ -34,16 +34,30 @@ if (identical(Sys.getenv("R_RUN_TINYTEST"), "true")) {
|
||||
# env var 'R_LIBS_USER' gets overwritten during 'R CMD check' in GitHub Actions, so:
|
||||
.libPaths(c(Sys.getenv("R_LIBS_USER_GH_ACTIONS"), .libPaths()))
|
||||
|
||||
if (AMR:::pkg_is_available("tinytest", also_load = TRUE)) {
|
||||
if (!AMR:::pkg_is_available("tinytest", also_load = TRUE)) {
|
||||
stop("R_RUN_TINYTEST is 'true', but the tinytest package could not be loaded, so no unit tests were run")
|
||||
} else {
|
||||
|
||||
# load the package
|
||||
library(AMR)
|
||||
|
||||
# testthat equivalents; tinytest only records expectations that are called from the environment
|
||||
# of a test file, so these call the expectations found there (via `env`) instead of tinytest::
|
||||
test_that <- function(desc, code) {
|
||||
code
|
||||
env <- parent.frame()
|
||||
tryCatch(code,
|
||||
skip_test = function(s) invisible(NULL),
|
||||
error = function(e) get("expect_true", envir = env)(FALSE, info = paste0("Error in '", desc, "': ", conditionMessage(e)))
|
||||
)
|
||||
}
|
||||
skip_if <- function(condition, message = "") {
|
||||
if (isTRUE(condition)) stop(structure(class = c("skip_test", "condition"), list(message = message, call = NULL)))
|
||||
}
|
||||
expect_output <- tinytest::expect_stdout
|
||||
skip_on_cran <- function() invisible(NULL)
|
||||
expect_output <- function(...) get("expect_stdout", envir = parent.frame())(...)
|
||||
expect_named <- function(object, expected, ...) get("expect_identical", envir = parent.frame())(names(object), expected, ...)
|
||||
expect_setequal <- function(object, expected, ...) get("expect_true", envir = parent.frame())(setequal(object, expected), ...)
|
||||
expect_lte <- function(object, expected, ...) get("expect_true", envir = parent.frame())(isTRUE(all(object <= expected)), ...)
|
||||
|
||||
# set language
|
||||
set_AMR_locale("English")
|
||||
@@ -75,17 +89,26 @@ if (identical(Sys.getenv("R_RUN_TINYTEST"), "true")) {
|
||||
`%||%` <- AMR:::`%||%`
|
||||
}
|
||||
|
||||
testdir <- ifelse(dir.exists("inst/testthat"), "inst/testthat", "testthat")
|
||||
# helper files are sourced by testthat, but not by tinytest; helper-functions.R only contains expect_inherits(), which tinytest has itself
|
||||
helpers <- list.files(system.file(testdir, package = "AMR"), pattern = "^helper.*[.]R$", full.names = TRUE)
|
||||
for (helper in helpers[basename(helpers) != "helper-functions.R"]) {
|
||||
source(helper)
|
||||
}
|
||||
|
||||
suppressMessages(
|
||||
out <- test_package("AMR",
|
||||
testdir = ifelse(dir.exists("inst/testthat"),
|
||||
"inst/testthat",
|
||||
"testthat"
|
||||
),
|
||||
testdir = testdir,
|
||||
verbose = FALSE,
|
||||
color = FALSE
|
||||
)
|
||||
)
|
||||
cat("\n\nSUMMARY:\n")
|
||||
print(summary(out))
|
||||
if (!all_pass(out)) {
|
||||
cat("\n\nFAILURES:\n")
|
||||
print(out, passes = FALSE, limit = 100)
|
||||
stop("Unit tests failed, see above")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user