Skip to content

Commit aebb4e3

Browse files
authored
Improve test coverage functionality (#2537)
Remove unneeded (and harmful) call to `local_test_directory()` from `test_coverage()`. Fixes r-lib/testthat#1858. Rewrite `test_coverage_active_file()` to ensure that test failures are forwarded to the user, and fix the problems (mostly with snapshotting thus revealed).
1 parent be1fc31 commit aebb4e3

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Imports:
3535
rversions (>= 2.1.1),
3636
sessioninfo (>= 1.2.2),
3737
stats,
38-
testthat (>= 3.1.5),
38+
testthat (>= 3.1.10.9000),
3939
tools,
4040
urlchecker (>= 1.0.1),
4141
utils,
@@ -68,3 +68,5 @@ Language: en-US
6868
Roxygen: list(markdown = TRUE)
6969
RoxygenNote: 7.2.3
7070
Config/testthat/edition: 3
71+
Remotes:
72+
r-lib/testthat

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# devtools (development version)
22

3+
* `test_coverage()` now works if the package has not been installed.
4+
5+
* `test_coverage_active_file()` now reports if any tests failed and does
6+
a better job of executing snapshot comparisons.
7+
38
# devtools 2.4.5
49

510
* `check(cleanup =)` was deprecated in devtools v1.11.0 (2016-04-12) and was

R/active.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ find_test_file <- function(path, call = parent.frame()) {
1515
)
1616
}
1717

18+
pkg <- as.package(dirname(path))
19+
1820
is_test <- type == "test"
19-
path[!is_test] <- paste0("tests/testthat/test-", name_source(path[!is_test]), ".R")
21+
path[!is_test] <- paste0(pkg$path, "/tests/testthat/test-", name_source(path[!is_test]), ".R")
2022
path <- unique(path[file_exists(path)])
2123

2224
if (length(path) == 0) {

R/test.R

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ test_coverage <- function(pkg = ".", show_report = interactive(), ...) {
100100
check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))
101101

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

106105
if (isTRUE(show_report)) {
@@ -119,28 +118,42 @@ test_coverage_file <- function(file = find_active_file(), ...) {
119118

120119
#' @rdname test
121120
#' @export
122-
test_coverage_active_file <- function(file = find_active_file(), filter = TRUE, show_report = interactive(), export_all = TRUE, ...) {
121+
test_coverage_active_file <- function(file = find_active_file(),
122+
filter = TRUE,
123+
show_report = interactive(),
124+
export_all = TRUE,
125+
...) {
123126
rlang::check_installed(c("covr", "DT"))
124-
125-
save_all()
126-
test_files <- find_test_file(file)
127-
pkg <- as.package(path_dir(file)[[1]])
128-
129127
check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))
130128

131-
withr::local_envvar(r_env_vars())
132-
testthat::local_test_directory(pkg$path, pkg$package)
133-
reporter <- testthat::local_snapshotter()
134-
reporter$start_file(file, "test")
129+
test_file <- find_test_file(file)
130+
test_dir <- path_dir(test_file)
131+
pkg <- as.package(test_dir)
135132

136133
env <- load_all(pkg$path, quiet = TRUE, export_all = export_all)$env
134+
# this always ends up using the package DESCRIPTION, which will refer
135+
# to the source package because of the load_all() above
136+
testthat::local_test_directory(test_dir, pkg$package)
137+
138+
# To correctly simulate test_file() we need to set up both a temporary
139+
# snapshotter (with correct directory specification) for snapshot comparisons
140+
# and a stop reporter to inform the user about test failures
141+
snap_reporter <- testthat::local_snapshotter(file.path(test_dir, "_snaps"))
142+
snap_reporter$start_file(basename(test_file))
143+
reporter <- testthat::MultiReporter$new(reporters = list(
144+
testthat::StopReporter$new(praise = FALSE),
145+
snap_reporter
146+
))
147+
148+
withr::local_envvar(r_env_vars())
137149
testthat::with_reporter(reporter, {
138-
coverage <- covr::environment_coverage(env, test_files, ...)
150+
coverage <- covr::environment_coverage(env, test_file, ...)
151+
reporter$end_file() # needed to write new snapshots
139152
})
140153

141154
if (isTRUE(filter)) {
142155
coverage_name <- name_source(covr::display_name(coverage))
143-
local_name <- name_test(file)
156+
local_name <- name_test(test_file)
144157
coverage <- coverage[coverage_name %in% local_name]
145158
}
146159

tests/testthat/test-active.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ test_that("find_active_file() gives useful error if no RStudio", {
33
})
44

55
test_that("fails if can't find tests", {
6+
dir <- local_package_create()
7+
withr::local_dir(dir)
8+
69
expect_snapshot(error = TRUE, {
710
find_test_file("R/foo.blah")
811
find_test_file("R/foo.R")

0 commit comments

Comments
 (0)