Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement test_local() #1084

Merged
merged 8 commits into from
Jul 7, 2020
Merged
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
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Imports:
Suggests:
covr,
curl (>= 0.9.5),
devtools,
knitr,
rmarkdown,
usethis,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export(test_env)
export(test_example)
export(test_examples)
export(test_file)
export(test_local)
export(test_package)
export(test_path)
export(test_rd)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# testthat (development version)

* New `test_local()` tests a local source package directory. It's equivalent
to `devtools::test()` but doesn't require devtools and all its dependencies
to be installed (#1030).

* Long deprecated `encoding` argument to `test_file()` and `test_dir()` has
been removed.

Expand Down
37 changes: 11 additions & 26 deletions R/auto-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,20 @@ auto_test <- function(code_path, test_path, reporter = default_reporter(),
#' @keywords debugging
#' @seealso [auto_test()] for details on how method works
auto_test_package <- function(pkg = ".", reporter = default_reporter(), hash = TRUE) {
if (!requireNamespace("devtools", quietly = TRUE)) {
stop(
"devtools required to run auto_test_package(). Please install.",
call. = FALSE
)
}

reporter <- find_reporter(reporter)

path <- pkg
pkg <- devtools::as.package(pkg)
path <- pkgload::pkg_path(path)
package <- pkgload::pkg_name(path)

code_path <- file.path(pkg$path, c("R", "src"))
code_path <- file.path(path, c("R", "src"))
code_path <- code_path[file.exists(code_path)]
code_path <- normalizePath(code_path)
test_path <- normalizePath(file.path(pkg$path, "tests", "testthat"))
test_path <- normalizePath(file.path(path, "tests", "testthat"))

# Start by loading all code and running all tests
env <- devtools::load_all(path)$env
withr::with_envvar(
devtools::r_env_vars(),
test_dir(test_path, env = env, reporter = reporter$clone(deep = TRUE))
)
withr::local_envvar(list("NOT_CRAN" = "true"))
pkgload::load_all(path)
test_dir(test_path, package = package, reporter = reporter$clone(deep = TRUE))

# Next set up watcher to monitor changes
watcher <- function(added, deleted, modified) {
Expand All @@ -114,26 +105,20 @@ auto_test_package <- function(pkg = ".", reporter = default_reporter(), hash = T
# Reload code and rerun all tests
cat("Changed code: ", paste0(basename(code), collapse = ", "), "\n")
cat("Rerunning all tests\n")
env <<- devtools::load_all(pkg, quiet = TRUE)$env
withr::with_envvar(
devtools::r_env_vars(),
test_dir(test_path, env = env, reporter = reporter$clone(deep = TRUE))
)
pkgload::load_all(path, quiet = TRUE)
test_dir(test_path, package = package, reporter = reporter$clone(deep = TRUE))
} else if (length(tests) > 0) {
# If test changes, rerun just that test
cat("Rerunning tests: ", paste0(basename(tests), collapse = ", "), "\n")
withr::with_envvar(
devtools::r_env_vars(),
test_files(tests, env = env, reporter = reporter$clone(deep = TRUE))
)
env <- env_clone(asNamespace(package))
test_files(tests, env, reporter = reporter$clone(deep = TRUE))
}

TRUE
}
watch(c(code_path, test_path), watcher, hash = hash)
}


# Helpers -----------------------------------------------------------------

starts_with <- function(string, prefix) {
Expand Down
57 changes: 48 additions & 9 deletions R/test-directory.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#' Run all tests in directory or package
#'
#' @description
#' Use `test_dir()` for a collection of tests in a directory; use
#' `test_package()` interactively at the console, and `test_check()`
#' inside of `R CMD check`.
#' * `test_package()` tests an installed package.
#' * `test_local()` tests a local source package.
#' * `test_check()` checks a package during `R CMD check`.
#'
#' In your own code, you can use `is_testing()` to determine if code is being
#' run as part of a test and `testing_package()` to retrieve the name of the
#' package being tested. You can also check the underlying env var directly
#' `identical(Sys.getenv("TESTTHAT"), "true")` to avoid creating a run-time
#' dependency on testthat.
#' These are all powered by the lower level `test_dir()`, which runs all test
#' files in a directory.
#'
#' @section Test files:
#' For package code, tests should live in `tests/testthat`.
Expand Down Expand Up @@ -176,12 +173,54 @@ test_check <- function(package,

#' @export
#' @rdname test_dir
test_local <- function(path = ".",
filter = NULL,
reporter = default_reporter(),
...,
stop_on_failure = TRUE,
stop_on_warning = FALSE
) {
path <- pkgload::pkg_path(path)
package <- pkgload::pkg_name(path)

test_path <- file.path(path, "tests", "testthat")
if (!dir.exists(test_path)) {
stop("No tests found for ", package, call. = FALSE)
}

library(testthat)
if (package != "testthat") {
pkgload::load_all(path, helpers = FALSE, quiet = TRUE)
}

withr::local_envvar(c(NOT_CRAN = "true"))
test_dir(
test_path,
package = package,
filter = filter,
reporter = reporter,
...,
stop_on_failure = stop_on_failure,
stop_on_warning = stop_on_warning
)
}

#' Determine testing status
#'
#' Use `is_testing()` to determine if code is being run as part of a test and
#' `testing_package()` to retrieve the name of the package being tested. You
#' can also check the underlying env var directly
#' `identical(Sys.getenv("TESTTHAT"), "true")` to avoid creating a run-time
#' dependency on testthat.
#'
#'
#' @export
is_testing <- function() {
identical(Sys.getenv("TESTTHAT"), "true")
}

#' @export
#' @rdname test_dir
#' @rdname is_testing
testing_package <- function() {
Sys.getenv("TESTTHAT_PKG")
}
18 changes: 18 additions & 0 deletions man/is_testing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 17 additions & 14 deletions man/test_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 8 additions & 16 deletions tests/test-catch.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ local({
if (isSolaris)
return()

if (!requireNamespace("devtools", quietly = TRUE))
return()

if (!requireNamespace("usethis", quietly = TRUE))
return()

devel <- try(devtools::has_devel(), silent = TRUE)
if (!isTRUE(devel))
return()
# devel <- try(pkgbuild::has_compiler(), silent = TRUE)
# if (!isTRUE(devel))
# return()

quietly <- function(expr) {
suppressMessages(capture_output(result <- expr))
Expand All @@ -44,7 +41,7 @@ local({
unlink(libPath, recursive = TRUE)
}, add = TRUE)

quietly(usethis::create_package(pkgPath))
quietly(usethis::create_package(pkgPath, open = FALSE))
quietly(testthat::use_catch(pkgPath))

cat("LinkingTo: testthat",
Expand Down Expand Up @@ -75,17 +72,12 @@ local({

}

devtools::install(pkgPath, quick = TRUE, quiet = FALSE)

install.packages(pkgs = pkgPath, repos = NULL, type = "source")
library(pkgName, character.only = TRUE)
stopifnot(.Call("run_testthat_tests", FALSE, PACKAGE = pkgName))

devtools::unload(pkgName)
pkgload::unload(pkgName)
}

withr::with_envvar(c(R_TESTS = ''),
perform_test("testthatclient1", TRUE))
withr::with_envvar(c(R_TESTS = ''),
perform_test("testthatclient2", FALSE))

withr::with_envvar(c(R_TESTS = ''), perform_test("testthatclient1", TRUE))
withr::with_envvar(c(R_TESTS = ''), perform_test("testthatclient2", FALSE))
})
8 changes: 4 additions & 4 deletions tests/testthat/test-mock.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ test_that("can access variables defined in function", {
})

test_that("can mock if package is not loaded", {
if ("package:devtools" %in% search()) {
skip("devtools is loaded")
if ("package:curl" %in% search()) {
skip("curl is loaded")
}
skip_if_not_installed("devtools")
with_mock(`devtools::test` = identity, expect_identical(devtools::test, identity))
skip_if_not_installed("curl")
with_mock(`curl::curl` = identity, expect_identical(curl::curl, identity))
})

test_that("changes to variables are preserved between calls and visible outside", {
Expand Down