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

Improve test coverage functionality #2537

Merged
merged 3 commits into from
Sep 29, 2023
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
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
39 changes: 26 additions & 13 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ test_coverage <- function(pkg = ".", show_report = interactive(), ...) {
check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))

withr::local_envvar(r_env_vars())
testthat::local_test_directory(pkg$path, pkg$package)
coverage <- covr::package_coverage(pkg$path, ...)

if (isTRUE(show_report)) {
Expand All @@ -119,28 +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())
testthat::local_test_directory(pkg$path, pkg$package)
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
Loading