regression.R 1.84 KB
Newer Older
Dorchies David's avatar
Dorchies David committed
1
2
context("Compare example outputs with CRAN")

3
CompareWithStable <- function(refVarFile, testDir, regIgnore) {
4
5
6
  v <- data.frame(topic = basename(dirname(refVarFile)),
                  var = gsub("\\.rds$", "", basename(refVarFile)))
  if (is.null(regIgnore) || all(apply(regIgnore, 1, function(x) !all(x == v)))) {
Dorchies David's avatar
Dorchies David committed
7
    test_that(paste("Compare", v$topic, v$var), {
Dorchies David's avatar
Dorchies David committed
8
      testVarFile <- paste0(
9
        file.path(testDir, v$topic, v$var),
Dorchies David's avatar
Dorchies David committed
10
11
        ".rds"
      )
Dorchies David's avatar
Dorchies David committed
12
      expect_true(file.exists(testVarFile))
13
      if (file.exists(testVarFile)) {
Dorchies David's avatar
Dorchies David committed
14
15
16
17
18
19
20
        testVar <- readRDS(testVarFile)
        refVar <- readRDS(refVarFile)
        expect_equivalent(testVar, refVar)
      }
    })
  }
}
21
22
23
24
25

tmp_path <- file.path("../tmp", Sys.getenv("R_VERSION"));

if (dir.exists(file.path(tmp_path, "stable")) & dir.exists(file.path(tmp_path, "dev"))) {
  refVarFiles <- list.files(file.path(tmp_path, "stable"), recursive = TRUE, full.names = TRUE)
26
  regIgnoreFile <- "../../.regressionignore"
27
  if (file.exists(regIgnoreFile)) {
28
    message("Using .regressionignore file. The following variables are going to be skipped:")
29
30
31
32
    regIgnore <- read.table(file = regIgnoreFile,
                            sep = " ", header = FALSE, skip = 5,
                            col.names = c("topic", "var"),
                            stringsAsFactors = FALSE)
33
    apply(regIgnore, 1, function(x) message(x[1], ": ", x[2]))
Dorchies David's avatar
Dorchies David committed
34
  } else {
35
    message("File ", file.path(getwd(), regIgnoreFile), " not found")
Dorchies David's avatar
Dorchies David committed
36
37
    regIgnore <- NULL
  }
38
  lapply(refVarFiles, FUN = CompareWithStable, testDir = file.path(tmp_path, "dev"), regIgnore = regIgnore)
Dorchies David's avatar
Dorchies David committed
39
} else {
40
  stop("Regression tests compared to released version needs that you run the following instructions first:\n",
41
42
43
       "Rscript tests/testthat/regression_tests.R stable\n",
       "R CMD INSTALL .\n",
       "Rscript tests/testthat/regression_tests.R dev")
Dorchies David's avatar
Dorchies David committed
44
}