Skip to content

Commit 918dfd6

Browse files
authored
Merge b7bf614 into d975fa0
2 parents d975fa0 + b7bf614 commit 918dfd6

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
testthat::describe("ui_teal_lockfile", {
2+
testthat::it("returns a shiny tagList", {
3+
result <- ui_teal_lockfile("test_id")
4+
testthat::expect_s3_class(result, "shiny.tag.list")
5+
})
6+
7+
testthat::it("creates namespaced elements with correct IDs", {
8+
result <- ui_teal_lockfile("test_id")
9+
result_html <- as.character(result)
10+
testthat::expect_true(grepl("test_id-lockFileStatus", result_html))
11+
testthat::expect_true(grepl("test_id-lockFileLink", result_html))
12+
})
13+
14+
testthat::it("creates disabled download link", {
15+
result <- ui_teal_lockfile("test_id")
16+
result_html <- as.character(result)
17+
testthat::expect_true(grepl("disabled", result_html))
18+
})
19+
20+
testthat::it("creates elements with correct structure", {
21+
result <- ui_teal_lockfile("test_id")
22+
testthat::expect_length(result, 2)
23+
# First element should be a span tag
24+
testthat::expect_true(grepl("<span", as.character(result[[1]])))
25+
# Second element should be a download link
26+
testthat::expect_true(grepl("Download lockfile", as.character(result[[2]])))
27+
})
28+
})
29+
30+
testthat::describe(".is_lockfile_deps_installed", {
31+
testthat::it("returns TRUE when both mirai and renv are installed", {
32+
testthat::skip_if_not_installed("mirai")
33+
testthat::skip_if_not_installed("renv")
34+
testthat::expect_true(.is_lockfile_deps_installed())
35+
})
36+
37+
testthat::it("returns logical value", {
38+
result <- .is_lockfile_deps_installed()
39+
testthat::expect_type(result, "logical")
40+
testthat::expect_length(result, 1)
41+
})
42+
})
43+
44+
testthat::describe(".is_disabled_lockfile_scenario", {
45+
testthat::it("returns TRUE when CALLR_IS_RUNNING is 'true'", {
46+
withr::with_envvar(
47+
list(CALLR_IS_RUNNING = "true"),
48+
{
49+
testthat::expect_true(.is_disabled_lockfile_scenario())
50+
}
51+
)
52+
})
53+
54+
testthat::it("returns TRUE when TESTTHAT is 'true'", {
55+
withr::with_envvar(
56+
list(TESTTHAT = "true"),
57+
{
58+
testthat::expect_true(.is_disabled_lockfile_scenario())
59+
}
60+
)
61+
})
62+
63+
testthat::it("returns TRUE when QUARTO_PROJECT_ROOT is set", {
64+
withr::with_envvar(
65+
list(QUARTO_PROJECT_ROOT = "/some/path"),
66+
{
67+
testthat::expect_true(.is_disabled_lockfile_scenario())
68+
}
69+
)
70+
})
71+
72+
testthat::it("returns TRUE when _R_CHECK_TIMINGS_ is set", {
73+
withr::with_envvar(
74+
list("_R_CHECK_TIMINGS_" = "true"),
75+
{
76+
testthat::expect_true(.is_disabled_lockfile_scenario())
77+
}
78+
)
79+
})
80+
81+
testthat::it("returns TRUE when _R_CHECK_LICENSE_ is set", {
82+
withr::with_envvar(
83+
list("_R_CHECK_LICENSE_" = "true"),
84+
{
85+
testthat::expect_true(.is_disabled_lockfile_scenario())
86+
}
87+
)
88+
})
89+
90+
testthat::it("returns FALSE when none of the disabled scenarios are active", {
91+
withr::with_envvar(
92+
list(
93+
CALLR_IS_RUNNING = "",
94+
TESTTHAT = "",
95+
QUARTO_PROJECT_ROOT = "",
96+
"_R_CHECK_TIMINGS_" = NULL,
97+
"_R_CHECK_LICENSE_" = NULL
98+
),
99+
{
100+
# This test runs in a testthat environment, so TESTTHAT will be "true"
101+
# We need to temporarily unset it
102+
old_testthat <- Sys.getenv("TESTTHAT")
103+
Sys.unsetenv("TESTTHAT")
104+
on.exit(Sys.setenv(TESTTHAT = old_testthat), add = TRUE)
105+
106+
result <- .is_disabled_lockfile_scenario()
107+
# Result may still be TRUE due to CheckExEnv in search path during tests
108+
testthat::expect_type(result, "logical")
109+
}
110+
)
111+
})
112+
})
113+
114+
testthat::describe(".renv_snapshot", {
115+
testthat::it("returns a list with 'out' and 'res' components", {
116+
testthat::skip_if_not_installed("renv")
117+
118+
# Create a temporary directory for the test
119+
temp_dir <- withr::local_tempdir()
120+
lockfile_path <- file.path(temp_dir, "test.lock")
121+
122+
# Run in a controlled environment
123+
withr::with_dir(temp_dir, {
124+
result <- .renv_snapshot(lockfile_path)
125+
126+
testthat::expect_type(result, "list")
127+
testthat::expect_true("out" %in% names(result))
128+
testthat::expect_true("res" %in% names(result))
129+
testthat::expect_type(result$out, "character")
130+
})
131+
})
132+
})
133+
134+
testthat::describe(".teal_lockfile_process_invoke", {
135+
testthat::it("returns an ExtendedTask object", {
136+
testthat::skip_if_not_installed("mirai")
137+
138+
temp_dir <- withr::local_tempdir()
139+
lockfile_path <- file.path(temp_dir, "test.lock")
140+
141+
withr::with_dir(temp_dir, {
142+
result <- .teal_lockfile_process_invoke(lockfile_path)
143+
testthat::expect_s3_class(result, "ExtendedTask")
144+
})
145+
})
146+
})
147+
148+
testthat::describe("srv_teal_lockfile", {
149+
testthat::it("throws error for invalid teal.lockfile.mode option", {
150+
withr::with_options(
151+
list(teal.lockfile.mode = "invalid"),
152+
{
153+
testthat::expect_error(
154+
shiny::testServer(
155+
app = srv_teal_lockfile,
156+
args = list(id = "test"),
157+
expr = {}
158+
),
159+
"'teal.lockfile.mode' option can only be one of"
160+
)
161+
}
162+
)
163+
})
164+
165+
testthat::it("returns NULL when mode is 'disabled'", {
166+
withr::with_options(
167+
list(teal.lockfile.mode = "disabled"),
168+
{
169+
shiny::testServer(
170+
app = srv_teal_lockfile,
171+
args = list(id = "test"),
172+
expr = {
173+
testthat::expect_null(session$returned())
174+
}
175+
)
176+
}
177+
)
178+
})
179+
180+
testthat::it("enables download when lockfile already exists", {
181+
testthat::skip_if_not_installed("mirai")
182+
testthat::skip_if_not_installed("renv")
183+
184+
# Create a temporary lockfile
185+
temp_lockfile <- "teal_app.lock"
186+
file.create(temp_lockfile)
187+
on.exit(if (file.exists(temp_lockfile)) file.remove(temp_lockfile), add = TRUE)
188+
189+
withr::with_options(
190+
list(teal.lockfile.mode = "enabled"),
191+
{
192+
shiny::testServer(
193+
app = srv_teal_lockfile,
194+
args = list(id = "test"),
195+
expr = {
196+
# The server should detect existing lockfile and enable download
197+
testthat::expect_null(session$returned())
198+
}
199+
)
200+
}
201+
)
202+
203+
# Clean up
204+
if (file.exists(temp_lockfile)) file.remove(temp_lockfile)
205+
})
206+
207+
testthat::it("returns NULL in auto mode when in disabled scenario", {
208+
withr::with_options(
209+
list(teal.lockfile.mode = "auto"),
210+
{
211+
# In test environment, .is_disabled_lockfile_scenario() returns TRUE
212+
shiny::testServer(
213+
app = srv_teal_lockfile,
214+
args = list(id = "test"),
215+
expr = {
216+
testthat::expect_null(session$returned())
217+
}
218+
)
219+
}
220+
)
221+
})
222+
223+
testthat::it("handles default empty mode option", {
224+
withr::with_options(
225+
list(teal.lockfile.mode = NULL),
226+
{
227+
# Default mode is "" (empty string)
228+
# In test environment, .is_disabled_lockfile_scenario() returns TRUE
229+
# so it should return NULL
230+
shiny::testServer(
231+
app = srv_teal_lockfile,
232+
args = list(id = "test"),
233+
expr = {
234+
testthat::expect_null(session$returned())
235+
}
236+
)
237+
}
238+
)
239+
})
240+
241+
testthat::it("accepts valid mode values: auto, enabled, disabled", {
242+
# Test 'disabled' - doesn't need mirai/renv
243+
withr::with_options(
244+
list(teal.lockfile.mode = "disabled"),
245+
{
246+
testthat::expect_silent(
247+
shiny::testServer(
248+
app = srv_teal_lockfile,
249+
args = list(id = "test"),
250+
expr = {}
251+
)
252+
)
253+
}
254+
)
255+
256+
# Test 'auto' - in test env, should be disabled
257+
withr::with_options(
258+
list(teal.lockfile.mode = "auto"),
259+
{
260+
testthat::expect_silent(
261+
shiny::testServer(
262+
app = srv_teal_lockfile,
263+
args = list(id = "test"),
264+
expr = {}
265+
)
266+
)
267+
}
268+
)
269+
})
270+
})

0 commit comments

Comments
 (0)