Debian Codegraph @

R/test.R

raw
1 #' Creates and returns run result data frame.
2 #'
3 #' @param file Name of the test runner file, a character vector of length 1.
4 #' @param pass Whether or not the test passed, a logical vector of length 1.
5 #' @param result Value (wrapped in a list) obtained by evaluating `file`.
6 #' This can also by any errors signaled when evaluating the `file`.
7 #'
8 #' @return A 1-row data frame representing a single test run. `result` and
9 #' is a "list column", or a column that contains list elements.
10 #' @noRd
11 result_row <- function(file, pass, result) {
12 stopifnot(is.list(result))
13 df <- data.frame(
14 file = file,
15 pass = pass,
16 result = I(result),
17 stringsAsFactors = FALSE
18 )
19 class(df) <- c("shiny_runtests", class(df))
20 df
21 }
22
23 #' Check to see if the given directory contains at least one script, and that
24 #' all scripts in the directory are shinytest scripts.
25 #' Scans for the magic string of `app <- ShinyDriver$new(` as an indicator that
26 #' this is a shinytest.
27 #' @noRd
28 is_legacy_shinytest_dir <- function(path){
29 is_shinytest_script <- function(file) {
30 if (!file.exists(file)) {
31 return(FALSE)
32 }
33
34 text <- readLines(file, warn = FALSE)
35 any(
36 grepl("app\\s*<-\\s*ShinyDriver\\$new\\(", text, perl=TRUE)
37 )
38 }
39
40 files <- dir(path, full.names = TRUE)
41 files <- files[!file.info(files)$isdir]
42 if (length(files) == 0) {
43 return(FALSE)
44 }
45 all(vapply(files, is_shinytest_script, logical(1)))
46 }
47
48 #' Runs the tests associated with this Shiny app
49 #'
50 #' Sources the `.R` files in the top-level of `tests/` much like `R CMD check`.
51 #' These files are typically simple runners for tests nested in other
52 #' directories under `tests/`.
53 #'
54 #' @param appDir The base directory for the application.
55 #' @param filter If not `NULL`, only tests with file names matching this regular
56 #' expression will be executed. Matching is performed on the file name
57 #' including the extension.
58 #' @param assert Logical value which determines if an error should be thrown if any error is captured.
59 #' @param envir Parent testing environment in which to base the individual testing environments.
60 #'
61 #' @return A data frame classed with the supplemental class `"shiny_runtests"`.
62 #' The data frame has the following columns:
63 #'
64 #' | **Name** | **Type** | **Meaning** |
65 #' | :-- | :-- | :-- |
66 #' | `file` | `character(1)` | File name of the runner script in `tests/` that was sourced. |
67 #' | `pass` | `logical(1)` | Whether or not the runner script signaled an error when sourced. |
68 #' | `result` | any or `NA` | The return value of the runner |
69 #'
70 #' @details Historically, [shinytest](https://rstudio.github.io/shinytest/)
71 #' recommended placing tests at the top-level of the `tests/` directory.
72 #' This older folder structure is not supported by runTests.
73 #' Please see [shinyAppTemplate()] for more details.
74 #' @export
75 runTests <- function(
76 appDir = ".",
77 filter = NULL,
78 assert = TRUE,
79 envir = globalenv()
80 ) {
81 # similar to runApp()
82 # Allows shiny's functions to be available in the UI, server, and test code
83 require(shiny)
84
85 testsDir <- file.path(appDir, "tests")
86 if (!dirExists(testsDir)) {
87 stop("No tests directory found: ", testsDir)
88 }
89 runners <- list.files(testsDir, pattern="\\.r$", ignore.case = TRUE)
90
91 if (length(runners) == 0) {
92 message("No test runners found in ", testsDir)
93 return(result_row(character(0), logical(0), list()))
94 }
95
96 if (!is.null(filter)) {
97 runners <- runners[grepl(filter, runners)]
98 }
99 if (length(runners) == 0) {
100 stop("No test runners matched the given filter: '", filter, "'")
101 }
102
103 # See the @details section of the runTests() docs above for why this branch exists.
104 if (is_legacy_shinytest_dir(testsDir)) {
105 stop(
106 "It appears that the .R files in ", testsDir, " are all shinytests.",
107 " This is not supported by `shiny::runTests()`.",
108 "\nPlease see `?shinytest::migrateShinytestDir` to migrate your shinytest file structure to the new format (requires shinytest 1.4.0 or above).",
109 "\nSee `?shiny::shinyAppTemplate` for an example of the new testing file structure."
110 )
111 }
112
113 renv <- new.env(parent = envir)
114
115 # Otherwise source all the runners -- each in their own environment.
116 ret <- do.call(rbind, lapply(runners, function(r) {
117 pass <- FALSE
118 result <-
119 tryCatch({
120 env <- new.env(parent = renv)
121 withr::with_dir(testsDir, {
122 ret <- sourceUTF8(r, envir = env)
123 })
124 pass <- TRUE
125 ret
126 }, error = function(err) {
127 message("Error in ", r, "\n", err)
128 err
129 })
130 result_row(file.path(testsDir, r), pass, list(result))
131 }))
132
133 if (isTRUE(assert)) {
134 if (!all(ret$pass)) {
135 stop("Shiny App Test Failures detected in\n", paste0("* ", runtest_pretty_file(ret$file[!ret$pass]), collapse = "\n"), call. = FALSE)
136 }
137 }
138
139 ret
140 }
141
142
143 runtest_pretty_file <- function(f) {
144 test_folder <- dirname(f)
145 app_folder <- dirname(test_folder)
146 file.path(
147 basename(app_folder),
148 basename(test_folder),
149 basename(f)
150 )
151 }
152
153
154 #' @export
155 print.shiny_runtests <- function(x, ..., reporter = "summary") {
156
157 cat("Shiny App Test Results\n")
158
159
160 if (any(x$pass)) {
161 cli::cat_bullet("Success", bullet = "tick", bullet_col = "green")
162 mapply(
163 x$file,
164 x$pass,
165 x$result,
166 FUN = function(file, pass, result) {
167 if (!pass) return()
168 # print(result)
169 cat(" - ", runtest_pretty_file(file), "\n", sep = "")
170 }
171 )
172 }
173 if (!all(x$pass)) {
174 cli::cat_bullet("Failure", bullet = "cross", bullet_col = "red")
175 mapply(
176 x$file,
177 x$pass,
178 x$result,
179 FUN = function(file, pass, result) {
180 if (pass) return()
181 cat(" - ", runtest_pretty_file(file), "\n", sep = "")
182 }
183 )
184 }
185
186 invisible(x)
187 }