]> git.elephly.net Git - software/r-guix-install.git/blob - tests/runUnitTests.R
Add unit tests.
[software/r-guix-install.git] / tests / runUnitTests.R
1 pkgname <- "guix.install"
2 subdir <- "unitTests"
3 path <- getwd()
4
5 .failure_details <- function(result) {
6 res <- result[[1L]]
7 if (res$nFail > 0 || res$nErr > 0) {
8 Filter(function(x) length(x) > 0, lapply(res$sourceFileResults,
9 function(fileRes) {
10 names(Filter(function(x) x$kind != "success",
11 fileRes))
12 }))
13 }
14 else list()
15 }
16
17 root <- system.file(package = pkgname)
18 library(pkgname, character.only = TRUE, quietly = TRUE)
19 dir <- file.path(root, subdir)
20 if (!file.exists(dir)) {
21 dir <- file.path(root, "inst", subdir)
22 }
23 if (!file.exists(dir)) {
24 stop("unable to find unit tests, no subdir ", sQuote(subdir))
25 }
26
27 library("RUnit", quietly = TRUE)
28 RUnit_opts <- getOption("RUnit", list())
29 RUnit_opts$verbose <- 0L
30 RUnit_opts$silent <- TRUE
31 RUnit_opts$verbose_fail_msg <- TRUE
32 oopt <- options(RUnit = RUnit_opts)
33 on.exit(options(oopt))
34 suite <- RUnit::defineTestSuite(name = paste(pkgname, "RUnit Tests"),
35 dirs = dir, testFileRegexp = "^test_.*\\.R$", rngKind = "default",
36 rngNormalKind = "default")
37 result <- RUnit::runTestSuite(suite)
38 cat("\n\n")
39 RUnit::printTextProtocol(result, showDetails = FALSE)
40 if (length(details <- .failure_details(result)) > 0) {
41 cat("\nTest files with failing tests\n")
42 for (i in seq_along(details)) {
43 cat("\n ", basename(names(details)[[i]]), "\n")
44 for (j in seq_along(details[[i]])) {
45 cat(" ", details[[i]][[j]], "\n")
46 }
47 }
48 cat("\n\n")
49 stop("unit tests failed for package ", pkgname)
50 }