Skip to content

Commit

Permalink
install the package in a temp directory and load it before running te…
Browse files Browse the repository at this point in the history
…sts; the temp directory will be deleted after the R session ends

this will no longer be necessary: yihui/xfun#5 (comment)
  • Loading branch information
yihui committed Sep 22, 2020
1 parent 31e252d commit cd968ba
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Steven", "Mortimer", role = "ctb", email="[email protected]")
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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

Expand Down
23 changes: 23 additions & 0 deletions R/testit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit cd968ba

Please sign in to comment.