|
| 1 | +source <- "./inst/testme" |
| 2 | +if (!utils::file_test("-d", source)) { |
| 3 | + stop("Source 'testme' folder not found: ", sQuote(source)) |
| 4 | +} |
| 5 | + |
| 6 | +target <- "./tests" |
| 7 | +if (!utils::file_test("-d", target)) { |
| 8 | + stop("Target 'tests' folder not found: ", sQuote(target)) |
| 9 | +} |
| 10 | + |
| 11 | +r_path <- "./R" |
| 12 | +if (!utils::file_test("-d", r_path)) { |
| 13 | + stop("Target 'R' folder not found: ", sQuote(r_path)) |
| 14 | +} |
| 15 | + |
| 16 | +desc <- "./DESCRIPTION" |
| 17 | +if (!utils::file_test("-f", desc)) { |
| 18 | + stop("'DESCRIPTION' file not found: ", sQuote(desc)) |
| 19 | +} |
| 20 | +pkgname <- read.dcf(desc)[, "Package"] |
| 21 | +if (is.na(pkgname) || !nzchar(pkgname)) { |
| 22 | + stop("Failed to infer package name from 'DESCRIPTION' file: ", sQuote(pkgname)) |
| 23 | +} else if (!requireNamespace(pkgname)) { |
| 24 | + stop("Package fail to load: ", sQuote(pkgname)) |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +files <- dir(path = source, pattern = "^test-.*[.]R$", full.names = TRUE) |
| 29 | +message(sprintf("Deploying %d test scripts ...", length(files))) |
| 30 | + |
| 31 | +## Generate R unit test script |
| 32 | +code <- c( |
| 33 | + "## This runs 'testme' test inst/testme/test-<name>.R scripts", |
| 34 | + "## Don't edit - it was autogenerated by inst/testme/deploy.R", |
| 35 | + "testme <- function(name) {", |
| 36 | + sprintf(" path <- system.file(package = '%s', 'testme', mustWork = TRUE)", pkgname), |
| 37 | + " Sys.setenv(R_TESTME_PATH = path)", |
| 38 | + sprintf(" Sys.setenv(R_TESTME_PACKAGE = '%s')", pkgname), |
| 39 | + " Sys.setenv(R_TESTME_NAME = name)", |
| 40 | + " on.exit(Sys.unsetenv('R_TESTME_NAME'))", |
| 41 | + " source(file.path(path, 'run.R'))", |
| 42 | + "}" |
| 43 | +) |
| 44 | +writeLines(code, con = file.path("./R/testme.R")) |
| 45 | + |
| 46 | +for (kk in seq_along(files)) { |
| 47 | + file <- files[kk] |
| 48 | + |
| 49 | + source_file <- basename(file) |
| 50 | + name <- sub("^test-", "", sub("[.]R$", "", source_file)) |
| 51 | + target_file <- file.path(target, source_file) |
| 52 | + |
| 53 | + message(sprintf("%02d/%02d test script %s", kk, length(files), sQuote(target_file))) |
| 54 | + |
| 55 | + ## Assert that testme script can be parsed |
| 56 | + res <- tryCatch(parse(file = file), error = identity) |
| 57 | + if (inherits(res, "error")) { |
| 58 | + stop("Syntax error: ", sQuote(file)) |
| 59 | + } |
| 60 | + |
| 61 | + ## Generate R unit test script |
| 62 | + code <- c( |
| 63 | + sprintf("## This runs testme test script incl/testme/test-%s.R", name), |
| 64 | + "## Don't edit - it was autogenerated by inst/testme/deploy.R", |
| 65 | + sprintf('%s:::testme("%s")', pkgname, name) |
| 66 | + ) |
| 67 | + writeLines(code, con = target_file) |
| 68 | +} |
| 69 | + |
| 70 | +message(sprintf("Deploying %d test scripts ... done", length(files))) |
0 commit comments