From cd968ba4950bbb1a9f5075fd8f5666720b04103f Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Mon, 21 Sep 2020 23:41:37 -0500 Subject: [PATCH] install the package in a temp directory and load it before running tests; the temp directory will be deleted after the R session ends this will no longer be necessary: https://github.com/yihui/xfun/issues/5#issuecomment-688593940 --- DESCRIPTION | 2 +- NEWS | 3 +++ R/testit.R | 23 +++++++++++++++++++++++ R/utils.R | 6 ++++++ R/zzz.R | 7 +++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 R/zzz.R diff --git a/DESCRIPTION b/DESCRIPTION index 668f7ae..9876ba1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: testit Type: Package Title: A Simple Package for Testing R Packages -Version: 0.11.1 +Version: 0.11.2 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Steven", "Mortimer", role = "ctb", email="reportmort@gmail.com") diff --git a/NEWS b/NEWS index dfbf814..a2e71e3 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ CHANGES IN testit VERSION 0.12 +MAJOR CHANGES + + o `test_pkg()` installs the package before running tests when it is called from a non-interactive R session that is not launched by `R CMD check`, e.g., when you run tests in RStudio via `Ctrl/Cmd + Shift + T`, so you will not have to install the package manually (`Ctrl/Cmd + Shift + B`) before running tests. CHANGES IN testit VERSION 0.11 diff --git a/R/testit.R b/R/testit.R index 75c898c..f5971d5 100644 --- a/R/testit.R +++ b/R/testit.R @@ -164,6 +164,29 @@ assert2 = function(fact, exprs, envir, all = TRUE) { #' @export #' @examples \dontrun{test_pkg('testit')} test_pkg = function(package, dir = c('testit', 'tests/testit')) { + # install the source package before running tests when this function is called + # in a non-interactive R session that is not `R CMD check` + install = !.env$installed && !interactive() && + file.exists(desc <- file.path('../DESCRIPTION')) && + is.na(Sys.getenv('_R_CHECK_PACKAGE_NAME_', NA)) && + !is.na(p <- read.dcf(desc, fields = 'Package')[1, 1]) && p == package + if (install) { + .env$lib_old = lib_old = .libPaths() + .env$lib_new = lib_new = tempfile('R-lib-', '.'); dir.create(lib_new) + res = system2( + file.path(R.home('bin'), 'R'), c( + 'CMD', 'INSTALL', paste0('--library=', lib_new), + '--no-help', '--no-staged-install', '--no-test-load', '..' + ) + ) + if (res == 0) { + .libPaths(c(lib_new, lib_old)) + .env$installed = TRUE + } + } + if (!is.na(i <- match(paste0('package:', package), search()))) + detach(pos = i, unload = TRUE, force = TRUE) + library(package, character.only = TRUE) path = available_dir(c(dir, system.file('tests', 'testit', package = package))) rs = list.files(path, '^test-.+[.][rR]$', full.names = TRUE) diff --git a/R/utils.R b/R/utils.R index ea488c4..4e626f4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,3 +1,9 @@ +# an internal environment to store objects +.env = new.env(parent = emptyenv()) + +# has the package been installed once in test_pkg()? +.env$installed = FALSE + # find an available dir available_dir = function(dirs) { for (i in dirs) { diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..9be334e --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,7 @@ +# clean up the temp library created in test_pkg() at the end of the R session +.onLoad = function(libname, pkgname) { + reg.finalizer(.env, function(e) { + unlink(e$lib_new, recursive = TRUE) + if (!is.null(e$lib_old)) .libPaths(e$lib_old) + }, onexit = TRUE) +}