Skip to content

Commit cc992ea

Browse files
committed
test: tighten kaleido review follow-ups
1 parent 1282fd4 commit cc992ea

2 files changed

Lines changed: 47 additions & 28 deletions

File tree

R/kaleido.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ legacyKaleidoScope <- function(kaleido) {
226226
if (is.null(context_names) || any(context_names == "")) {
227227
rlang::abort("`context` must be a named list.")
228228
}
229+
if (any(!grepl("^[A-Za-z_][A-Za-z0-9_]*$", context_names))) {
230+
rlang::abort("`context` names must be valid Python identifiers.")
231+
}
229232

230233
for (i in seq_along(context)) {
231234
name <- context_names[[i]]

tests/testthat/test-kaleido.R

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,57 @@ test_that("newKaleidoScope does not inline Windows temp paths into Python code",
66
py_calls <- character()
77
write_fig_args <- NULL
88

9-
testthat::local_mocked_bindings(
10-
plotly_build = function(...) {
11-
list(x = list(data = list(), layout = list(), config = list()))
12-
},
13-
to_JSON = function(...) "{}",
14-
plotlyMainBundlePath = function() "plotly.min.js",
15-
.package = "plotly"
16-
)
9+
local({
10+
testthat::local_mocked_bindings(
11+
plotly_build = function(...) {
12+
list(x = list(data = list(), layout = list(), config = list()))
13+
},
14+
to_JSON = function(...) "{}",
15+
plotlyMainBundlePath = function() "plotly.min.js",
16+
.package = "plotly"
17+
)
1718

18-
testthat::local_mocked_bindings(
19-
tempfile = function(...) win_path,
20-
writeLines = function(...) NULL,
21-
.package = "base"
22-
)
19+
testthat::local_mocked_bindings(
20+
tempfile = function(...) win_path,
21+
writeLines = function(...) NULL,
22+
unlink = function(...) 0,
23+
.package = "base"
24+
)
2325

24-
testthat::local_mocked_bindings(
25-
py_run_string = function(code, ...) {
26-
py_calls <<- c(py_calls, code)
27-
py_obj <- get("py", envir = asNamespace("reticulate"))
28-
py_obj$fig <- "fake-fig"
26+
testthat::local_mocked_bindings(
27+
py_run_string = function(code, ...) {
28+
py_calls <<- c(py_calls, code)
29+
py_obj <- get("py", envir = asNamespace("reticulate"))
30+
py_obj$fig <- "fake-fig"
31+
invisible(NULL)
32+
},
33+
.package = "reticulate"
34+
)
35+
36+
kaleido <- list(write_fig_sync = function(fig, file, opts, kopts) {
37+
write_fig_args <<- list(fig = fig, file = file, opts = opts, kopts = kopts)
2938
invisible(NULL)
30-
},
31-
.package = "reticulate"
32-
)
39+
})
3340

34-
kaleido <- list(write_fig_sync = function(fig, file, opts, kopts) {
35-
write_fig_args <<- list(fig = fig, file = file, opts = opts, kopts = kopts)
36-
invisible(NULL)
41+
scope <- plotly:::newKaleidoScope(kaleido)
42+
scope$transform(list(), "figure.png")
3743
})
3844

39-
scope <- newKaleidoScope(kaleido)
40-
scope$transform(list(), "figure.png")
41-
4245
expect_identical(py_calls[[1]], "import json; fig = json.load(open(tmp_json_path))")
43-
expect_identical(py_calls[[2]], "del tmp_json_path")
46+
expect_true(!grepl(win_path, py_calls[[1]], fixed = TRUE))
47+
if (length(py_calls) >= 2) {
48+
expect_identical(py_calls[[2]], "del tmp_json_path")
49+
}
4450
expect_identical(write_fig_args$fig, "fake-fig")
4551
expect_identical(write_fig_args$file, "figure.png")
4652
})
53+
54+
test_that("py_run_string_with_context rejects invalid Python identifiers", {
55+
expect_error(
56+
plotly:::.py_run_string_with_context(
57+
"value = 1",
58+
context = list("bad name" = 1)
59+
),
60+
"`context` names must be valid Python identifiers\\."
61+
)
62+
})

0 commit comments

Comments
 (0)