Debian Codegraph @

tests/testthat/test-options.R

raw
1 test_that("Shared secret", {
2 op <- options(shiny.sharedSecret = "This is a secret string")
3 on.exit(options(op))
4
5 checkSharedSecret <- loadSharedSecret()
6
7 expect_true(checkSharedSecret("This is a secret string"))
8 expect_true(checkSharedSecret(charToRaw("This is a secret string")))
9
10 expect_false(checkSharedSecret("this is a secret string"))
11 expect_false(checkSharedSecret("This is a secret string "))
12 expect_false(checkSharedSecret(""))
13 expect_false(checkSharedSecret(NULL))
14 expect_error(checkSharedSecret(1:10))
15 })
16
17 test_that("Captured options contain expected elements", {
18 old_options <- shinyOptions(); on.exit(do.call(shinyOptions, old_options))
19
20 shinyOptions(bookmarkStore = 123)
21 shinyOptions(appDir = normalizePath("../")) # stomped
22 caps <- captureAppOptions()
23
24 expect_equal(sort(names(caps)), c("appDir", "bookmarkStore"))
25 expect_equal(caps$appDir, getwd())
26 expect_equal(caps$bookmarkStore, 123)
27
28 # verify that options are reset
29 expect_equal(getShinyOption("bookmarkStore"), NULL)
30 expect_equal(getShinyOption("appDir"), NULL)
31 })
32
33 test_that("Capturing options at creation time", {
34 old_options <- shinyOptions(); on.exit(do.call(shinyOptions, old_options))
35
36 # These are tests of captureAppOptions. Specific options should be captured
37 # and stored at app creation time and immediately reset. If another app is
38 # created, it will also capture these options at creation time.
39 # When these apps are started, they will apply the options.
40 shinyOptions(bookmarkStore = 123)
41 value <- NULL
42 s <- shinyApp(basicPage(), function(input, output) {},
43 onStart = function() {
44 value <<- getShinyOption("bookmarkStore")
45 observe(stopApp())
46 }
47 )
48 # After creating app, bookmarkStore option should be reset
49 expect_identical(getShinyOption("bookmarkStore"), NULL)
50 expect_identical(value, NULL)
51
52 # Create another app
53 shinyOptions(bookmarkStore = 456)
54 value2 <- NULL
55 s2 <- shinyApp(basicPage(), function(input, output) {},
56 onStart = function() {
57 value2 <<- getShinyOption("bookmarkStore")
58 observe(stopApp())
59 }
60 )
61 expect_identical(getShinyOption("bookmarkStore"), NULL)
62 expect_identical(value2, NULL)
63
64 # Running second app will cause the option to be restored while it's running,
65 # and in our onStart, we save it into value2.
66 runApp(s2, launch.browser = FALSE, quiet = TRUE)
67 expect_identical(value, NULL)
68 expect_identical(value2, 456)
69 expect_identical(getShinyOption("bookmarkStore"), NULL)
70
71 # Same, for first app and value1.
72 runApp(s, launch.browser = FALSE, quiet = TRUE)
73 expect_identical(value, 123)
74 expect_identical(value2, 456)
75 expect_identical(getShinyOption("bookmarkStore"), NULL)
76 })
77
78
79 test_that("Option scoping", {
80 old_options <- shinyOptions(); on.exit(do.call(shinyOptions, old_options))
81 # Tests for global and app-level scoping for shinyOptions().
82 shinyOptions(foo = NULL)
83 values <- list()
84 values["global_in"] <- list(getShinyOption("foo"))
85 shinyOptions(foo = "global")
86
87 s <- shinyApp(
88 basicPage(),
89 function(input, output, session) {},
90 onStart = function() {
91 values["app_in"] <<- list(getShinyOption("foo"))
92
93 shinyOptions(foo = "app")
94
95 onStop(function() {
96 values["app_out"] <<- list(getShinyOption("foo"))
97 })
98 observe(stopApp())
99 }
100 )
101
102 # Run the app
103 runApp(s, launch.browser = FALSE, quiet = TRUE)
104 values["global_out"] <- list(getShinyOption("foo"))
105
106 expect_identical(
107 values,
108 list(
109 global_in = NULL,
110 app_in = "global",
111 app_out = "app",
112 global_out = "global"
113 )
114 )
115
116 # Note: The code below also tests for session-level scoping of shinyOptions(),
117 # but it can't be easily run in CI, since it requires a browser to connect to
118 # the app to run the session-level code.
119 #
120 # # Tests for global, app, and session-level scoping for shinyOptions().
121 # shinyOptions(foo = NULL)
122 # values <- list()
123 # values["global_in"] <- list(getShinyOption("foo"))
124 # shinyOptions(foo = "global")
125 #
126 # s <- shinyApp(
127 # basicPage(),
128 # function(input, output, session) {
129 # values["session_in"] <<- list(getShinyOption("foo"))
130 # shinyOptions(foo = "session")
131 # onStop(function() {
132 # values["session_out"] <<- list(getShinyOption("foo"))
133 # })
134 #
135 # observe(stopApp())
136 # },
137 # onStart = function() {
138 # values["app_in"] <<- list(getShinyOption("foo"))
139 #
140 # shinyOptions(foo = "app")
141 #
142 # onStop(function() {
143 # values["app_out"] <<- list(getShinyOption("foo"))
144 # })
145 # }
146 # )
147 #
148 # # Run the app
149 # runApp(s)
150 # # Need this to run the session's onStop callbacks. But this should happen
151 # # automatically, without needing to pump the event loop an extra time.
152 # later::run_now()
153 # values["global_out"] <- list(getShinyOption("foo"))
154 #
155 # expect_identical(
156 # values,
157 # list(
158 # global_in = NULL,
159 # app_in = "global",
160 # session_in = "app",
161 # app_out = "app",
162 # # TODO: It would be more correct if session_out came before app_out,
163 # # because it makes more sense for the session's onStop callback to run
164 # # before the app's onStop. But that can be revisited at some point in the
165 # # future.
166 # session_out = "session",
167 # global_out = "global"
168 # )
169 # )
170
171 })
172
173 test_that("Unsetting app-level option NULL won't affect global option", {
174 old_options <- shinyOptions(); on.exit(do.call(shinyOptions, old_options))
175
176 shinyOptions(foo = 1)
177 value <- NULL
178 s <- shinyApp(basicPage(), function(input, output) {},
179 onStart = function() {
180 shinyOptions(foo = NULL)
181 value <<- getShinyOption("foo")
182 observe(stopApp())
183 }
184 )
185
186 runApp(s, launch.browser = FALSE, quiet = TRUE)
187 expect_identical(getShinyOption("foo"), 1)
188 expect_identical(value, NULL)
189 })