@@ -100,7 +100,6 @@ test_coverage <- function(pkg = ".", show_report = interactive(), ...) {
100
100
check_dots_used(action = getOption(" devtools.ellipsis_action" , rlang :: warn ))
101
101
102
102
withr :: local_envvar(r_env_vars())
103
- testthat :: local_test_directory(pkg $ path , pkg $ package )
104
103
coverage <- covr :: package_coverage(pkg $ path , ... )
105
104
106
105
if (isTRUE(show_report )) {
@@ -119,28 +118,42 @@ test_coverage_file <- function(file = find_active_file(), ...) {
119
118
120
119
# ' @rdname test
121
120
# ' @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
+ ... ) {
123
126
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
-
129
127
check_dots_used(action = getOption(" devtools.ellipsis_action" , rlang :: warn ))
130
128
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 )
135
132
136
133
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())
137
149
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
139
152
})
140
153
141
154
if (isTRUE(filter )) {
142
155
coverage_name <- name_source(covr :: display_name(coverage ))
143
- local_name <- name_test(file )
156
+ local_name <- name_test(test_file )
144
157
coverage <- coverage [coverage_name %in% local_name ]
145
158
}
146
159
0 commit comments