Debian Codegraph @

tests/testthat/test-test-runTests.R

raw
1 test_that("runTests works", {
2 calls <- list()
3 # Tracks the working directory we were in as of the last call
4 wd <- NULL
5
6 # A collection of file names that should throw an error when sourced
7 filesToError <- "runner2.R"
8
9 sourceStub <- function(...){
10 calls[[length(calls)+1]] <<- list(...)
11 wd <<- getwd()
12
13 if(list(...)[[1]] %in% filesToError){
14 stop("I was told to throw an error")
15 }
16
17 NULL
18 }
19
20 loadCalls <- list()
21 loadSupportStub <- function(...){
22 loadCalls[[length(calls)+1]] <<- list(...)
23 NULL
24 }
25
26 runTestsSpy <- rewire(runTests, sourceUTF8 = sourceStub, loadSupport=loadSupportStub)
27
28 res <- suppressMessages(
29 runTestsSpy(test_path("../test-helpers/app1-standard"), assert = FALSE)
30 )
31
32 # Should have seen two calls to each test runner
33 expect_length(calls, 2)
34 expect_match(calls[[1]][[1]], "runner1\\.R$", perl=TRUE)
35 expect_match(calls[[2]][[1]], "runner2\\.R$", perl=TRUE)
36
37 # Check environments
38 # Each should be loaded into an isolated env that has a common parent
39 env1 <- calls[[1]]$envir
40 env2 <- calls[[2]]$envir
41 expect_identical(parent.env(env1), parent.env(env2))
42 expect_true(!identical(env1, env2))
43
44 # Check working directory
45 expect_equal(normalizePath(wd), normalizePath(
46 file.path(test_path("../test-helpers/app1-standard"), "tests")))
47
48 # Check the results
49 expect_equal(all(res$pass), FALSE)
50 expect_length(res$file, 2)
51 expect_equal(basename(res$file[1]), "runner1.R")
52 expect_equal(res[2,]$result[[1]]$message, "I was told to throw an error")
53 expect_s3_class(res, "shiny_runtests")
54
55 # Check that supporting files were NOT loaded using Spy Functions
56 expect_length(loadCalls, 0)
57
58 # Clear out err'ing files and rerun
59 filesToError <- character(0)
60
61 calls <- list()
62 res <- runTestsSpy(test_path("../test-helpers/app1-standard"))
63 expect_equal(all(res$pass), TRUE)
64 expect_equal(basename(res$file), c("runner1.R", "runner2.R"))
65 expect_length(calls, 2)
66 expect_match(calls[[1]][[1]], "runner1\\.R", perl=TRUE)
67 expect_match(calls[[2]][[1]], "runner2\\.R", perl=TRUE)
68
69 })
70
71 test_that("calls out to shinytest when appropriate", {
72 is_legacy_shinytest_val <- TRUE
73 is_legacy_shinytest_dir_stub <- function(...){
74 is_legacy_shinytest_val
75 }
76
77 # All are shinytests
78 runTestsSpy <- rewire(runTests, is_legacy_shinytest_dir = is_legacy_shinytest_dir_stub)
79 expect_error(
80 runTestsSpy(test_path("../test-helpers/app1-standard"), assert = FALSE),
81 "not supported"
82 )
83
84 # Not shinytests
85 is_legacy_shinytest_val <- FALSE
86 res <- runTestsSpy(test_path("../test-helpers/app1-standard"))
87 expect_s3_class(res, "shiny_runtests")
88 })
89
90 test_that("runTests filters", {
91 calls <- list()
92 sourceStub <- function(...){
93 calls[[length(calls)+1]] <<- list(...)
94 NULL
95 }
96
97 runTestsSpy <- rewire(runTests, sourceUTF8 = sourceStub)
98
99 # No filter should see two tests (global.R is sourced from another function)
100 runTestsSpy(test_path("../test-helpers/app1-standard"))
101 expect_length(calls, 2)
102
103 # Filter down to one (global.R is sourced from another function)
104 calls <- list()
105 runTestsSpy(test_path("../test-helpers/app1-standard"), filter="runner1")
106 expect_length(calls, 1)
107
108 calls <- list()
109 expect_error(runTestsSpy(test_path("../test-helpers/app1-standard"), filter="i don't exist"), "matched the given filter")
110 })
111
112 test_that("runTests handles the absence of tests", {
113 expect_error(runTests(test_path("../test-helpers/app2-nested")), "No tests directory found")
114 expect_message(res <- runTests(test_path("../test-helpers/app6-empty-tests")), "No test runners found in")
115 expect_equal(res$file, character(0))
116 expect_equal(res$pass, logical(0))
117 expect_equal(res$result, I(list()))
118 expect_s3_class(res, "shiny_runtests")
119 })
120
121 test_that("runTests runs as expected without rewiring", {
122 appDir <- test_path(file.path("..", "test-helpers", "app1-standard"))
123 df <- testthat::expect_output(
124 print(runTests(appDir = appDir, assert = FALSE)),
125 "Shiny App Test Results\\n\\v Success\\n - app1-standard/tests/runner1\\.R\\n - app1-standard/tests/runner2\\.R"
126 )
127
128 expect_equal(df, data.frame(
129 file = file.path(appDir, "tests", c("runner1.R", "runner2.R")),
130 pass = c(TRUE, TRUE),
131 result = I(list(1, NULL)),
132 stringsAsFactors = FALSE
133 ), ignore_attr = TRUE)
134 expect_s3_class(df, "shiny_runtests")
135 })