Skip to content

Commit

Permalink
support attaching internal namespace in snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski committed Jan 13, 2025
1 parent b094493 commit c7bbc59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
34 changes: 21 additions & 13 deletions tests/testthat/helper-py-require.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ test_py_require_reset <- function() {
.globals$python_requirements <- NULL
}

r_session <- function(expr, echo = TRUE, color = FALSE) {
expr <- substitute(expr)
if (is.call(expr) && identical(expr[[1]], quote(`{`))) {
exprs <- as.list(expr)[-1]
} else {
exprs <- list(expr)
}
exprs <- unlist(lapply(exprs, deparse))
r_session <- function(exprs, echo = TRUE, color = FALSE,
attach_namespace = FALSE) {
exprs <- substitute(exprs)
if (!is.call(exprs))
stop("exprs must be a call")

exprs <- if (identical(exprs[[1]], quote(`{`)))
as.list(exprs)[-1]
else
list(exprs)

exprs <- unlist(c(
if (attach_namespace)
'attach(asNamespace("reticulate"), name = "namespace:reticulate", warn.conflicts = FALSE)',
if (echo)
"options(echo = TRUE)",
lapply(exprs, deparse)
))

writeLines(exprs, file <- tempfile(fileext = ".R"))
on.exit(unlink(file), add = TRUE)

result <- suppressWarnings(system2(
R.home("bin/R"),
c("--quiet", "--no-save", "--no-restore",
if (!echo) "--no-echo",
"-f", file
),
c("--quiet", "--no-save", "--no-restore", "--no-echo", "-f", file),
stdout = TRUE, stderr = TRUE,
env = c(if (isFALSE(color)) "NO_COLOR=1")
env = c(character(), if (isFALSE(color)) "NO_COLOR=1")
))
class(result) <- "r_session_record"
result
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-py_require.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

test_that("Error requesting newer package version against an older snapshot", {
local_edition(3)
expect_snapshot(r_session({
reticulate:::get_or_create_venv(c("numpy<2", "numpy>=2"))
expect_snapshot(r_session(attach_namespace = TRUE, {
get_or_create_venv(c("numpy<2", "numpy>=2"))
}))
})
# test_py_require_reset()
Expand Down

0 comments on commit c7bbc59

Please sign in to comment.