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