|
| 1 | +test_that("newKaleidoScope does not inline Windows temp paths into Python code", { |
| 2 | + skip_if_not_installed("reticulate") |
| 3 | + skip_if_not(suppressWarnings(reticulate::py_available(TRUE))) |
| 4 | + |
| 5 | + win_path <- "C:\\users\\name\\AppData\\Local\\Temp\\Rtmp\\file.json" |
| 6 | + py_calls <- character() |
| 7 | + write_fig_args <- NULL |
| 8 | + |
| 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 | + ) |
| 17 | + |
| 18 | + testthat::local_mocked_bindings( |
| 19 | + tempfile = function(...) win_path, |
| 20 | + writeLines = function(...) NULL, |
| 21 | + .package = "base" |
| 22 | + ) |
| 23 | + |
| 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" |
| 29 | + invisible(NULL) |
| 30 | + }, |
| 31 | + .package = "reticulate" |
| 32 | + ) |
| 33 | + |
| 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) |
| 37 | + }) |
| 38 | + |
| 39 | + scope <- newKaleidoScope(kaleido) |
| 40 | + scope$transform(list(), "figure.png") |
| 41 | + |
| 42 | + expect_identical(py_calls, "import json; fig = json.load(open(tmp_json_path))") |
| 43 | + expect_identical(write_fig_args$fig, "fake-fig") |
| 44 | + expect_identical(write_fig_args$file, "figure.png") |
| 45 | +}) |
0 commit comments