(v3.0.1.9135) Fix get_mo_source() removing the mo_source file at the default location, returning the wrong mo_source, and throwing an error after the original file was moved, and run the unit tests in CI also in a real interactive session and with a user setup, a Dutch locale, and CLSI as default guideline, with tests that no longer assume an English locale or EUCAST

This commit is contained in:
dr. M.S. (Matthijs) Berends committed 2026-10-14 23:34:45 +02:00
1 parent 4be7352843
commit 8b01bcb23c
16 files changed
+360 -38

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
# ==================================================================== #
# TITLE: #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
# https://github.com/msberends/AMR #
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
# https://doi.org/10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen and the University Medical #
# Center Groningen in the Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://amr-for-r.org #
# ==================================================================== #
# Runs all unit tests from source, see run_tests.sh. Quits with status 1 on any failure or error, and with status 2 if the tests could not be run at all.
# Every top-level error must end in quit(), since an interactive session would otherwise continue reading "1" from stdin.
status <- tryCatch(
{
if (identical(Sys.getenv("AMR_TEST_INTERACTIVE"), "true") && !interactive()) {
stop("AMR_TEST_INTERACTIVE is set, but this R session is not interactive")
}
cat("Interactive:", interactive(), "| LANG:", Sys.getenv("LANG"), "| AMR options:", paste(names(options())[grepl("^AMR_", names(options()))], collapse = ", "), "\n")
# report all failures, not only the first 10
options(warn = 1, testthat.summary.max_reports = Inf, testthat.progress.max_fails = Inf)
res <- as.data.frame(testthat::test_local(Sys.getenv("AMR_TEST_PKG_DIR", "."), reporter = "summary", stop_on_failure = FALSE))
cat("\nFiles:", length(unique(res$file)), "| failed:", sum(res$failed), "| errors:", sum(res$error), "| skipped:", sum(res$skipped), "| passed:", sum(res$passed), "\n")
as.integer(sum(res$failed) > 0 || any(res$error))
},
error = function(e) {
cat("Tests could not be run:", conditionMessage(e), "\n")
2L
}
)
quit(save = "no", status = status)
+51
View File
@@ -0,0 +1,51 @@
#!/bin/bash
# ==================================================================== #
# TITLE: #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
# https://github.com/msberends/AMR #
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
# https://doi.org/10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen and the University Medical #
# Center Groningen in the Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://amr-for-r.org #
# ==================================================================== #
# Runs an R script, in a real interactive R session if AMR_TEST_INTERACTIVE is "true", and exits with its status.
# Usage: run_tests.sh <script.R>
# R CMD check and Rscript are never interactive, so code behind interactive() is otherwise never run in CI.
# In an interactive session, R reads both its commands and the answers to prompts from stdin, and it ignores -f. Therefore stdin holds one source() line, followed by an endless stream of "1", which means 'proceed' in every utils::menu() prompt of this package.
# A timeout prevents an unanswerable prompt from hanging the job.
set +e +o pipefail
script="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
if [ "$AMR_TEST_INTERACTIVE" = "true" ]; then
{ echo "source('$script')"; yes 1; } | timeout 3600 R --interactive --no-save --quiet
status=${PIPESTATUS[1]}
else
timeout 3600 Rscript "$script"
status=$?
fi
if [ "$status" = "124" ]; then
echo "Timed out, possibly on a prompt that could not be answered"
fi
exit $status
+67
View File
@@ -0,0 +1,67 @@
# ==================================================================== #
# TITLE: #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
# https://github.com/msberends/AMR #
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
# https://doi.org/10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen and the University Medical #
# Center Groningen in the Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://amr-for-r.org #
# ==================================================================== #
# Sets up the user environment of a test scenario in the home directory, before run_tests.sh runs the tests. Run it through run_tests.sh with AMR_TEST_INTERACTIVE=true, since set_mo_source() only works in interactive sessions.
# AMR_TEST_SCENARIO can contain one or more of these words, separated by a comma:
# - "user": an existing ~/mo_source.rds, and the options AMR_custom_ab and AMR_custom_mo set to RDS files
# - "clsi": the option AMR_guideline set to "CLSI"
# The options are written to ~/.Rprofile, so that they are set at start-up as with users.
status <- tryCatch(
{
stopifnot(interactive())
scenarios <- trimws(strsplit(Sys.getenv("AMR_TEST_SCENARIO"), ",")[[1]])
stopifnot(length(scenarios) > 0, all(scenarios %in% c("user", "clsi")))
pkgload::load_all(Sys.getenv("AMR_TEST_PKG_DIR", "."), quiet = TRUE)
opts <- character(0)
if ("user" %in% scenarios) {
dir.create("~/amr_fixtures", showWarnings = FALSE)
write.csv(data.frame(lab_code = c("AMRTEST_ECOLI", "AMRTEST_SAUR"), mo = c("B_ESCHR_COLI", "B_STPHY_AURS")), "~/amr_fixtures/mo_source.csv", row.names = FALSE)
set_mo_source("~/amr_fixtures/mo_source.csv")
stopifnot(file.exists("~/mo_source.rds"))
saveRDS(data.frame(ab = "AMRTST", name = "Amrtestacillin", group = "Test antimicrobials"), "~/amr_fixtures/custom_ab.rds")
saveRDS(data.frame(genus = "Amrtestbacterium", species = "scenarii"), "~/amr_fixtures/custom_mo.rds")
opts <- c(opts, paste0("AMR_custom_ab = '", path.expand("~/amr_fixtures/custom_ab.rds"), "'"), paste0("AMR_custom_mo = '", path.expand("~/amr_fixtures/custom_mo.rds"), "'"))
}
if ("clsi" %in% scenarios) {
opts <- c(opts, "AMR_guideline = 'CLSI'")
}
if (length(opts) > 0) {
write(paste0("options(", paste(opts, collapse = ", "), ")"), "~/.Rprofile", append = TRUE)
}
cat("Scenario set up:", paste(scenarios, collapse = ", "), "\n")
if (file.exists("~/.Rprofile")) cat(readLines("~/.Rprofile"), sep = "\n")
0L
},
error = function(e) {
cat("Scenario could not be set up:", conditionMessage(e), "\n")
2L
}
)
quit(save = "no", status = status)
+30 -4
View File
@@ -41,6 +41,11 @@ on:
name: check-recent
# a new commit cancels the runs of the previous commit on the same PR or branch, a scheduled run is never cancelled by a push
concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
setup:
runs-on: ubuntu-latest
@@ -49,11 +54,15 @@ jobs:
steps:
- id: set-matrix
shell: bash
# entries with "tests": true do not run R CMD check, but run the unit tests from source with .github/scripts/run_tests.sh, optionally:
# - "interactive": true, in a real interactive R session, so that code behind interactive() is also tested
# - "scenario", the user environment set up with .github/scripts/setup_test_scenario.R
# - "lang", the LANG environment variable
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo 'matrix={"config":[{"os":"ubuntu-latest","r":"release","allowfail":false}]}' >> "$GITHUB_OUTPUT"
echo 'matrix={"config":[{"os":"ubuntu-latest","r":"release","allowfail":false},{"os":"ubuntu-latest","r":"release","allowfail":false,"tests":true,"label":"interactive","interactive":true}]}' >> "$GITHUB_OUTPUT"
else
echo 'matrix={"config":[{"os":"windows-latest","r":"devel","allowfail":false},{"os":"ubuntu-latest","r":"devel","allowfail":false,"http-user-agent":"release"},{"os":"macOS-latest","r":"release","allowfail":true},{"os":"windows-latest","r":"release","allowfail":false},{"os":"ubuntu-latest","r":"release","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-1","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-2","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-3","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-4","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-5","allowfail":false}]}' >> "$GITHUB_OUTPUT"
echo 'matrix={"config":[{"os":"windows-latest","r":"devel","allowfail":false},{"os":"ubuntu-latest","r":"devel","allowfail":false,"http-user-agent":"release"},{"os":"macOS-latest","r":"release","allowfail":true},{"os":"windows-latest","r":"release","allowfail":false},{"os":"ubuntu-latest","r":"release","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-1","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-2","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-3","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-4","allowfail":false},{"os":"ubuntu-latest","r":"oldrel-5","allowfail":false},{"os":"ubuntu-latest","r":"release","allowfail":false,"tests":true,"label":"interactive","interactive":true},{"os":"ubuntu-latest","r":"release","allowfail":false,"tests":true,"label":"interactive, user setup","interactive":true,"scenario":"user"},{"os":"ubuntu-latest","r":"release","allowfail":false,"tests":true,"label":"Dutch locale","lang":"nl_NL.UTF-8"},{"os":"ubuntu-latest","r":"release","allowfail":false,"tests":true,"label":"CLSI as default guideline","scenario":"clsi"}]}' >> "$GITHUB_OUTPUT"
fi
R-code-check:
@@ -62,7 +71,7 @@ jobs:
continue-on-error: ${{ matrix.config.allowfail }}
name: ${{ matrix.config.os }} (R-${{ matrix.config.r }})
name: ${{ matrix.config.os }} (R-${{ matrix.config.r }})${{ matrix.config.label && format(' {0}', matrix.config.label) || '' }}
strategy:
fail-fast: false
@@ -71,6 +80,8 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
AMR_TEST_INTERACTIVE: ${{ matrix.config.interactive && 'true' || 'false' }}
AMR_TEST_SCENARIO: ${{ matrix.config.scenario || '' }}
steps:
- uses: actions/checkout@v4
@@ -86,11 +97,26 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
extra-packages: ${{ matrix.config.tests && 'any::pkgload' || 'any::rcmdcheck' }}
needs: check
upgrade: 'TRUE'
- name: Set LANG
if: matrix.config.lang
run: echo "LANG=${{ matrix.config.lang }}" >> "$GITHUB_ENV"
- name: Set up scenario '${{ matrix.config.scenario }}'
if: matrix.config.scenario
run: AMR_TEST_INTERACTIVE=true .github/scripts/run_tests.sh .github/scripts/setup_test_scenario.R
- name: Run unit tests
if: matrix.config.tests
env:
NOT_CRAN: true
run: .github/scripts/run_tests.sh .github/scripts/run_tests.R
- uses: r-lib/actions/check-r-package@v2
if: ${{ !matrix.config.tests }}
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
@@ -43,6 +43,11 @@ on:
name: check-without-dplyr
# a new commit cancels the runs of the previous commit on the same PR or branch, a scheduled run is never cancelled by a push
concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
setup:
runs-on: ubuntu-latest
+5
View File
@@ -37,6 +37,11 @@ on:
name: code-coverage
# a new commit cancels the runs of the previous commit on the same PR or branch, a scheduled run is never cancelled by a push
concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
code-coverage:
runs-on: ubuntu-latest