-
Notifications
You must be signed in to change notification settings - Fork 760
Expand file tree
/
Copy pathtest.R
More file actions
270 lines (233 loc) · 7.27 KB
/
Copy pathtest.R
File metadata and controls
270 lines (233 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#' Execute testthat tests in a package
#'
#' @description
#' * `test()` runs all tests in a package. It's a shortcut for
#' [testthat::test_dir()]
#' * `test_active_file()` runs `test()` on the active file.
#' * `test_coverage()` computes test coverage for your package. It's a
#' shortcut for [covr::package_coverage()] plus [covr::report()].
#' * `test_coverage_active_file()` computes test coverage for the active file. It's a
#' shortcut for [covr::file_coverage()] plus [covr::report()].
#'
#' @template devtools
#' @param ... additional arguments passed to wrapped functions.
#' @param file One or more source or test files. If a source file the
#' corresponding test file will be run. The default is to use the active file
#' in RStudio (if available).
#' @inheritParams testthat::test_dir
#' @inheritParams pkgload::load_all
#' @inheritParams run_examples
#' @export
test <- function(
pkg = ".",
filter = NULL,
stop_on_failure = FALSE,
export_all = TRUE,
...
) {
save_all()
pkg <- as.package(pkg)
if (!uses_testthat(pkg)) {
cli::cli_inform(c(i = "No testing infrastructure found."))
if (!interactive()) {
cli::cli_bullets(c(
"!" = 'Setup testing with {.run usethis::use_testthat()}.'
))
return(invisible())
}
if (yesno("Create it?")) {
return(invisible())
}
usethis_use_testthat(pkg)
return(invisible())
}
cli::cli_inform(c(i = "Testing {.pkg {pkg$package}}"))
withr::local_envvar(r_env_vars())
load_package <- load_package_for_testing(pkg)
testthat::test_local(
pkg$path,
filter = filter,
stop_on_failure = stop_on_failure,
load_package = load_package,
...
)
}
#' @export
#' @rdname test
test_active_file <- function(file = find_active_file(), ...) {
save_all()
test_files <- find_test_file(file)
pkg <- as.package(path_dir(test_files)[[1]])
withr::local_envvar(r_env_vars())
if (is_rstudio_running()) {
rstudioapi::executeCommand("activateConsole", quiet = TRUE)
}
load_package <- load_package_for_testing(pkg)
testthat::test_file(
test_files,
package = pkg$package,
load_package = load_package,
...
)
}
load_package_for_testing <- function(pkg) {
if (pkg$package == "testthat") {
# Must load testthat outside of testthat so tests are run with
# dev testthat
load_all(pkg$path, quiet = TRUE, helpers = FALSE)
"none"
} else {
"source"
}
}
#' @param report How to display the coverage report.
#' * `"html"` opens an interactive report in the browser.
#' * `"zero"` prints uncovered lines to the console.
#' * `"silent"` returns the coverage object without display.
#'
#' Defaults to `"html"` if interactive; otherwise to `"zero"`.
#' @export
#' @rdname test
test_coverage <- function(pkg = ".", report = NULL, ...) {
check_installed("covr")
save_all()
pkg <- as.package(pkg)
cli::cli_inform(c(i = "Computing test coverage for {.pkg {pkg$package}}"))
check_dots_used(action = getOption("devtools.ellipsis_action", warn))
withr::local_envvar(r_env_vars())
coverage <- covr::package_coverage(pkg$path, ...)
show_report(coverage, report = report, path = pkg$path)
}
#' @rdname test
#' @export
test_coverage_active_file <- function(
file = find_active_file(),
filter = TRUE,
report = NULL,
export_all = TRUE,
...
) {
check_installed("covr")
check_dots_used(action = getOption("devtools.ellipsis_action", warn))
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
# Must capture before local_test_directory() sets TESTTHAT envvar,
# which causes rlang::is_interactive() to return FALSE
report <- report_default(report)
# 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(
snap_dir = 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_file, ...)
reporter$end_file() # needed to write new snapshots
})
if (isTRUE(filter)) {
coverage_name <- name_source(covr::display_name(coverage))
local_name <- name_test(test_file)
coverage <- coverage[coverage_name %in% local_name]
}
# Use relative paths
attr(coverage, "relative") <- TRUE
attr(coverage, "package") <- pkg
show_report(coverage, report = report, path = pkg$path)
}
#' Does a package use testthat?
#'
#' @export
#' @keywords internal
uses_testthat <- function(pkg = ".") {
pkg <- as.package(pkg)
paths <- c(
path(pkg$path, "inst", "tests"),
path(pkg$path, "tests", "testthat")
)
any(dir_exists(paths))
}
report_default <- function(report, call = caller_env()) {
if (is.null(report)) {
if (is_llm() || !is_interactive()) "zero" else "html"
} else {
arg_match(report, c("silent", "zero", "html"), error_call = call)
}
}
show_report <- function(coverage, report, path, call = caller_env()) {
report <- report_default(report, call = call)
if (report == "html") {
check_installed("DT")
covered <- unique(covr::display_name(coverage))
if (length(covered) == 1) {
covr::file_report(coverage)
} else {
covr::report(coverage)
}
} else if (report == "zero") {
zero <- covr::zero_coverage(coverage)
if (nrow(zero) == 0) {
cli::cli_inform(c(v = "All lines covered!"))
} else {
for (file in unique(zero$filename)) {
file_zero <- zero[zero$filename == file, ]
lines_by_fun <- split(file_zero$line, file_zero$functions)
rel_path <- path_rel(file, path)
cli::cli_inform("Uncovered lines in {.file {rel_path}}:")
for (fun in names(lines_by_fun)) {
lines <- paste0(collapse_lines(lines_by_fun[[fun]]), collapse = ", ")
cli::cli_inform(c("*" = "{.fn {fun}}: {lines}"))
}
}
}
}
invisible(coverage)
}
collapse_lines <- function(x) {
x <- sort(unique(x))
breaks <- c(0, which(diff(x) != 1), length(x))
ranges <- character(length(breaks) - 1)
for (i in seq_along(ranges)) {
start <- x[breaks[i] + 1]
end <- x[breaks[i + 1]]
if (start == end) {
ranges[i] <- as.character(start)
} else {
ranges[i] <- paste0(start, "-", end)
}
}
ranges
}
#' Defunct functions
#'
#' These functions are defunct and will be removed in a future version of
#' devtools.
#' @name devtools-defunct
#' @keywords internal
NULL
#' @rdname devtools-defunct
#' @export
test_file <- function(file = find_active_file(), ...) {
lifecycle::deprecate_stop("2.5.0", "test_file()", "test_active_file()")
}
#' @rdname devtools-defunct
#' @export
test_coverage_file <- function(file = find_active_file(), ...) {
lifecycle::deprecate_stop(
"2.5.0",
"test_coverage_file()",
"test_coverage_active_file()"
)
}