forked from r-lib/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-examples.R
96 lines (82 loc) · 3.1 KB
/
run-examples.R
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
#' Run all examples in a package.
#'
#' One of the most frustrating parts of `R CMD check` is getting all of your
#' examples to pass - whenever one fails you need to fix the problem and then
#' restart the whole process. This function makes it a little easier by
#' making it possible to run all examples from an R function.
#'
#' @param pkg package description, can be path or package name. See
#' [as.package()] for more information
#' @param start Where to start running the examples: this can either be the
#' name of `Rd` file to start with (with or without extensions), or
#' a topic name. If omitted, will start with the (lexicographically) first
#' file. This is useful if you have a lot of examples and don't want to
#' rerun them every time you fix a problem.
#' @family example functions
#' @param show DEPRECATED.
#' @param test if `TRUE`, code in \code{\\donttest{}} will be commented
#' out. If `FALSE`, code in \code{\\testonly{}} will be commented out.
#' @param run if `TRUE`, code in \code{\\dontrun{}} will be commented
#' out.
#' @param fresh if `TRUE`, will be run in a fresh R session. This has
#' the advantage that there's no way the examples can depend on anything in
#' the current session, but interactive code (like [browser()])
#' won't work.
#' @param document if `TRUE`, [document()] will be run to ensure
#' examples are updated before running them.
#' @keywords programming
#' @export
run_examples <- function(pkg = ".", start = NULL, show = TRUE, test = FALSE,
run = TRUE, fresh = FALSE, document = TRUE) {
pkg <- as.package(pkg)
if (fresh) {
to_run <-
eval(substitute(
function() devtools::run_examples(pkg = path, start = start, test = test, run = run, fresh = FALSE),
list(path = pkg$path, start = start, test = test, run = run)
))
callr::r(to_run, show = TRUE, spinner = FALSE)
return(invisible())
}
if (document) {
document(pkg)
}
if (!missing(show)) {
warning("`show` is deprecated", call. = FALSE)
}
files <- rd_files(pkg$path, start = start)
if (length(files) == 0) {
return()
}
cat_rule(
left = paste0("Running ", length(files), " example files"),
right = pkg$package
)
load_all(pkg$path, reset = TRUE, export_all = FALSE)
on.exit(load_all(pkg$path, reset = TRUE))
lapply(files, pkgload::run_example, test = test, run = run)
invisible()
}
# If an error occurs, should print out the suspect line of code, and offer
# the following options:
# * skip to the next example
# * quit
# * browser
# * rerun example and rerun
# * reload code and rerun
rd_files <- function(pkg = ".", start = NULL) {
pkg <- as.package(pkg)
path_man <- file.path(pkg$path, "man")
files <- dir(path_man, pattern = "\\.[Rr]d$", full.names = TRUE)
names(files) <- basename(files)
files <- sort_ci(files)
if (!is.null(start)) {
topic <- pkgload::dev_help(start, dev_packages = pkg$package)
start_path <- basename(topic$path)
start_pos <- which(names(files) == start_path)
if (length(start_pos) == 1) {
files <- files[-seq(1, start_pos - 1)]
}
}
files
}