/
test-in-ide.R
188 lines (166 loc) · 5.43 KB
/
test-in-ide.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
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
#' Test apps within RStudio IDE
#'
#' Automatically runs the next app in a fresh RStudio session after closing the current app. To stop, send an interrupt signal (\verb{esc} or \verb{ctrl+c}) to the app twice in rapid succession.
#'
#' Kill testing by hitting \verb{esc} in RStudio.
#'
#' If \code{options()} need to be set, set them in your \preformatted{.Rprofile} file. See \code{usethis::edit_r_profile()}
#'
#' @inheritParams test_in_browser
#' @param delay Time to wait between applications. \[`1`\]
#' @param viewer RStudio IDE viewer to use. \[`"pane"`\]
#' @param refresh_ For internal use. If TRUE, packages will not be reinstalled.
#' @export
#' @examples
#' \dontrun{
#' test_in_ide(dir = "apps")
#' }
test_in_ide <- function(
app_name = apps[1],
apps = apps_manual,
...,
port = 8000,
host = "127.0.0.1",
delay = 1,
local_pkgs = FALSE,
viewer = NULL,
refresh_ = FALSE) {
sys_call <- match.call()
apps <- resolve_app_name(apps)
should_install <- !isTRUE(local_pkgs)
libpath <- resolve_libpath(local_pkgs = local_pkgs)
withr::local_libpaths(libpath, action = "prefix")
app_name <- resolve_app_name(app_name)
if (should_install) {
install_missing_app_deps(app_name, libpath = libpath)
}
if (rstudioapi::isAvailable()) {
# stop("This function should only be run within the RStudio IDE")
if (rstudioapi::isAvailable("1.3.387")) {
# window, pane, browser
if (missing(viewer)) {
# this should be the default value for `viewer` once IDEv1.2 is not supported
viewer <- rstudioapi::readRStudioPreference("shiny_viewer_type", "pane")
}
shiny_viewer_type <- force(viewer)
on.exit(
{
rstudioapi::writeRStudioPreference("shiny_viewer_type", shiny_viewer_type)
},
add = TRUE
)
if (is.null(viewer)) {
message("!! Setting `shiny_viewer_type` to `'pane'` !!")
viewer <- "pane"
}
# shiny viewer is not `window` or `pane`
if (!is.null(viewer)) {
viewer <- match.arg(viewer, c("pane", "window"), several.ok = FALSE)
}
# viewer supplied
rstudioapi::writeRStudioPreference("shiny_viewer_type", viewer)
} else {
# RStudio, but early version
# This feels hacky, but is necessary
# This code should mirror the code above
# shiny viewer is not `window` or `pane`
if (is.null(viewer)) {
message("!! Setting `shiny_viewer_type` to `'pane'` !!")
viewer <- "pane"
}
try({
runPane <- get(".rs.invokeShinyPaneViewer", envir = as.environment("tools:rstudio"))
runWindow <- get(".rs.invokeShinyWindowViewer", envir = as.environment("tools:rstudio"))
runFn <- switch(match.arg(viewer, c("pane", "window")),
"pane" = runPane,
"window" = runWindow
)
old_option <- options(shiny.launch.browser = runFn)
on.exit({
options(old_option)
})
})
}
}
# # make sure the apps are ok to run
# if (isTRUE(verify)) {
# app_status_verify(dir)
# }
# app_status_init(dir, user_agent = app_status_user_agent_ide())
old_ops <- options(width = 100)
on.exit(
{
options(old_ops)
},
add = TRUE
)
increment_app_and_wait <- function() {
next_app_name <- next_app_name(app_name)
restart_with_app(next_app_name)
}
restart_with_app <- function(app_name) {
if (is.null(app_name)) {
message("All done testing!")
return(invisible(NULL))
}
# do not try to update apps again. Set the next app. Keep all existing arguments.
next_sys_call_list <- as.list(sys_call)
next_sys_call_list[[1]] <- substitute(shinycoreci::test_in_ide)
next_sys_call_list$app_name <- app_name
next_sys_call_list$refresh_ <- TRUE
next_sys_call <- as.call(next_sys_call_list)
message("Restarting RStudio and launching next app in ", delay, " second... (interrupt again to stop)")
Sys.sleep(delay)
message("Restarting RStudio with next app!")
# # if this section of code is reached, it is considered a pass!
# app_status_save(
# app_dir = file.path(dir, app),
# pass = TRUE,
# log = "(unknown; can not capture)",
# user_agent = app_status_user_agent_ide()
# )
next_sys_call_txt <- format(next_sys_call)
if (shinycoreci_is_loaded_with_devtools()) {
# dev mode
next_sys_call_txt <- sub("shinycoreci::test_in_ide", "pkgload::load_all(); test_in_ide", next_sys_call_txt)
}
if (rstudioapi::isAvailable()) {
# restart RStudio, run the next app
rstudioapi::restartSession(next_sys_call_txt)
} else {
# if not in RStudio, run in the next tick...
later::later(function() {
eval(parse(text = next_sys_call_txt))
})
}
return(invisible(app_name))
}
# If not in a fresh session, restart!
if (!isTRUE(refresh_)) {
restart_with_app(app_name)
return()
}
message("Running ", app_name)
tryCatch(
{
run_app(app_name, port = port, host = host)
},
error = function(e) {
utils::alarm()
message("")
message("!! Error launching ", app_name, " !! Error: \n", e)
message("")
ans <- utils::menu(
choices = c("yes", "no"),
title = "Please mark the error above.\n\nContinue testing?"
)
if (ans == 2) {
stop("...stopping testing...")
}
increment_app_and_wait()
},
interrupt = function(cond) {
increment_app_and_wait()
}
)
}