Skip to content

Commit

Permalink
Rewrite test_coverage_active_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 27, 2023
1 parent 65045c2 commit 0481828
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Imports:
rversions (>= 2.1.1),
sessioninfo (>= 1.2.2),
stats,
testthat (>= 3.1.5),
testthat (>= 3.1.10.9000),
tools,
urlchecker (>= 1.0.1),
utils,
Expand Down Expand Up @@ -68,3 +68,5 @@ Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Config/testthat/edition: 3
Remotes:
r-lib/testthat
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# devtools (development version)

* `test_coverage()` now works if the package has not been installed.

* `test_coverage_active_file()` now reports if any tests failed and does
a better job of executing snapshot comparisons.

# devtools 2.4.5

* `check(cleanup =)` was deprecated in devtools v1.11.0 (2016-04-12) and was
Expand Down
4 changes: 3 additions & 1 deletion R/active.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ find_test_file <- function(path, call = parent.frame()) {
)
}

pkg <- as.package(dirname(path))

is_test <- type == "test"
path[!is_test] <- paste0("tests/testthat/test-", name_source(path[!is_test]), ".R")
path[!is_test] <- paste0(pkg$path, "/tests/testthat/test-", name_source(path[!is_test]), ".R")
path <- unique(path[file_exists(path)])

if (length(path) == 0) {
Expand Down
37 changes: 26 additions & 11 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,42 @@ test_coverage_file <- function(file = find_active_file(), ...) {

#' @rdname test
#' @export
test_coverage_active_file <- function(file = find_active_file(), filter = TRUE, show_report = interactive(), export_all = TRUE, ...) {
test_coverage_active_file <- function(file = find_active_file(),
filter = TRUE,
show_report = interactive(),
export_all = TRUE,
...) {
rlang::check_installed(c("covr", "DT"))

save_all()
test_files <- find_test_file(file)
pkg <- as.package(path_dir(file)[[1]])

check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))

withr::local_envvar(r_env_vars())
reporter <- testthat::local_snapshotter()
reporter$start_file(file, "test")
test_file <- find_test_file(file)
test_dir <- path_dir(test_file)
pkg <- as.package(test_dir)

env <- load_all(pkg$path, quiet = TRUE, export_all = export_all)$env
# this always ends up using the package DESCRIPTION, which will refer
# to the source package because of the load_all() above
testthat::local_test_directory(test_dir, pkg$package)

# To correctly simulate test_file() we need to set up both a temporary
# snapshotter (with correct directory specification) for snapshot comparisons
# and a stop reporter to inform the user about test failures
snap_reporter <- testthat::local_snapshotter(file.path(test_dir, "_snaps"))
snap_reporter$start_file(basename(test_file))
reporter <- testthat::MultiReporter$new(reporters = list(
testthat::StopReporter$new(praise = FALSE),
snap_reporter
))

withr::local_envvar(r_env_vars())
testthat::with_reporter(reporter, {
coverage <- covr::environment_coverage(env, test_files, ...)
coverage <- covr::environment_coverage(env, test_file, ...)
reporter$end_file() # needed to write new snapshots
})

if (isTRUE(filter)) {
coverage_name <- name_source(covr::display_name(coverage))
local_name <- name_test(file)
local_name <- name_test(test_file)
coverage <- coverage[coverage_name %in% local_name]
}

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-active.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ test_that("find_active_file() gives useful error if no RStudio", {
})

test_that("fails if can't find tests", {
dir <- local_package_create()
withr::local_dir(dir)

expect_snapshot(error = TRUE, {
find_test_file("R/foo.blah")
find_test_file("R/foo.R")
Expand Down

0 comments on commit 0481828

Please sign in to comment.