Skip to content

Commit

Permalink
simplify snapshotting approach
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski committed Jan 11, 2025
1 parent 1d2324d commit b2b4ab5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
9 changes: 7 additions & 2 deletions tests/testthat/_snaps/py_require.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Error requesting newer package version against an older snapshot

Code
eval_remote(error = TRUE, {
r_session({
reticulate:::get_or_create_venv(c("numpy<2", "numpy>=2"))
})
Message
Output
> reticulate:::get_or_create_venv(c("numpy<2", "numpy>=2"))
× No solution found when resolving `--with` dependencies:
╰─▶ Because you require numpy<2 and numpy>=2, we can conclude that your
requirements are unsatisfiable.
Error in reticulate:::get_or_create_venv(c("numpy<2", "numpy>=2")) :
Python requirements could not be satisfied.
Call `py_require()` to remove or replace conflicting requirements.
Execution halted
------- session end -------
success: false
exit_code: 1

73 changes: 29 additions & 44 deletions tests/testthat/helper-py-require.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,36 @@ test_py_require_reset <- function() {
.globals$python_requirements <- NULL
}


eval_remote <- function(expr, echo = FALSE, error = TRUE) {
r_session <- function(expr, echo = TRUE, color = FALSE) {
expr <- substitute(expr)
if(echo) {
expr <- call("withAutoprint", expr)
if (is.call(expr) && identical(expr[[1]], quote(`{`))) {
exprs <- as.list(expr)[-1]
} else {
exprs <- list(expr)
}
func <- as.function.default(list(expr), envir = asNamespace("reticulate"))
func <- rlang::zap_srcref(func)

out <- tempfile("teststdout", fileext = ".out")
err <- tempfile("teststderr", fileext = ".out")
on.exit(unlink(c(stdout, stderr)))

expect_error <- error
encountered_error <- FALSE
tryCatch(
callr::r(
func,
spinner = FALSE,
stdout = out,
stderr = err,
package = "reticulate",
env = c(callr::rcmd_safe_env(), "NO_COLOR" = 1)
),

error = function(e) {

encountered_error <<- TRUE

if(file.exists(out)) writeLines(readLines(out))

# evaluate::evaluate(), testthat::expect_snapshot(), and friends
# do not capture stderr.
# https://github.com/r-lib/testthat/issues/1741
# https://github.com/r-lib/evaluate/issues/121
# to play nice with expect_snapshot(), options are to
# - print via message()
# - print to stdout() using writeLines(foo, stderr())
if(file.exists(err)) message(paste0(readLines(err), collapse = "\n"))
exprs <- unlist(lapply(exprs, deparse))
writeLines(exprs, file <- tempfile(fileext = ".R"))

result <- suppressWarnings(system2(
R.home("bin/R"),
c("--quiet", "--no-save", "--no-restore",
if (!echo) "--no-echo",
"-f", file
),
stdout = TRUE, stderr = TRUE,
env = c(if (isFALSE(color)) "NO_COLOR=1")
))
class(result) <- "r_session_record"
result
}

if (!expect_error)
stop(e)
}
)
if (expect_error && !encountered_error) {
stop("expression did not error")
}
print.r_session_record <- function(record, echo = TRUE) {
writeLines(record)
status <- attr(record, "status", TRUE)
cat(sep = "",
"------- session end -------\n",
"success: ", if (is.null(status)) "true" else "false", "\n",
"exit_code: ", status, "\n")
}
registerS3method("print", "r_session_record", print.r_session_record,
envir = environment(print))
3 changes: 1 addition & 2 deletions tests/testthat/test-py_require.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

test_that("Error requesting newer package version against an older snapshot", {
local_edition(3)
withr::local_envvar(c("NO_COLOR" = 1))
expect_snapshot(eval_remote(error = TRUE, {
expect_snapshot(r_session({
reticulate:::get_or_create_venv(c("numpy<2", "numpy>=2"))
}))
})
Expand Down

0 comments on commit b2b4ab5

Please sign in to comment.