Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
^.codecov\.yml$
^.appveyor\.yml$
^\.github$
^docs
^pkgdown$
^_pkgdown\.yml$
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ LazyData: yes
Additional_repositories:
https://r-hyperspec.github.io/pkg-repo/
Depends:
testthat
RoxygenNote: 7.1.0
devtools,
testthat,
utils
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE)
Collate:
'test.R'
'get_package_env.R'
'gettest.R'
'test-fun.R'
'unittest.R'
65 changes: 65 additions & 0 deletions R/get_package_env.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' Get environment belonging to a package
#'
#' Check whether the package is currently attached via devtools.
#' If so, get the environment of the attached version, otherwise retrieve the
#' namespace of the installed package.
#'
#' @param pkgname name of the package
#'
#' @return environment with `pkgname`'s namespace
#' @include test.R
#' @importFrom utils maintainer
#' @export
#'
#' @examples
#' get_package_env("base")
get_package_env <- function(pkgname){

## be graceful if it's already the environment
if (is.environment(pkgname))
return(pkgname)

## find out whether we're testing a package that is loaded by devtools
## and get that environment rather than a potentially older installed version.
## see https://github.com/r-hyperspec/hySpc.testthat/issues/16
##
## If not, return the installed version
if (!pkgname %in% dev_packages())
return(getNamespace(pkgname))

pkgname <- paste0("package:", pkgname)

pos <- grep (pkgname, search())

if (length (pos) == 0L)
stop (pkgname, " is supposed to be loaded by devtools,",
" but is not available in search path.")

env <- .GlobalEnv
for (i in seq_len (pos[1] - 1L)) {
env <- parent.env(env)
}

if (environmentName(env) != pkgname)
stop("Incorrect location of ", pkgname, " in search path.",
"\nPlease contact ", maintainer("hySpc.testthat"),
"with the output of search() and sessionInfo().")

env
}

test(get_package_env) <- function(){
context("get_package_env")

env <- getNamespace("base")

test_that("environment is left unchanged", {
expect_equal(get_package_env(env), env)
})

test_that("correct environment is retrieved", {
expect_equal(get_package_env("base"), env)
})

test_that("version loaded by devtools is retrieved", {})
}
6 changes: 2 additions & 4 deletions R/unittest.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
##'
##' @keywords programming utilities
##' @importFrom testthat with_reporter
##' @importFrom devtools dev_packages
##' @include gettest.R
##' @export
##' @examples
Expand All @@ -27,10 +28,7 @@ unittest <- function(ns, standalone = TRUE, reporter = "progress") {
if (!"package:testthat" %in% search())
attachNamespace("testthat")

if (is.character(ns))
ns <- getNamespace(ns)

tests <- eapply(env = ns, FUN = get_test, all.names = TRUE)
tests <- eapply(env = get_package_env(ns), FUN = get_test, all.names = TRUE)
tests <- tests[!sapply(tests, is.null)]

if (standalone) {
Expand Down