File: test-python-virtual-environments.R

package info (click to toggle)
r-cran-reticulate 1.41.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 5,154; python: 620; sh: 13; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,305 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
context("virtual environments")

test_that("reticulate can bind to virtual environments created with venv", {

  skip_if_no_python()
  skip_on_cran()
  skip_on_os("windows")

  Sys.unsetenv("RETICULATE_PYTHON")
  Sys.unsetenv("RETICULATE_PYTHON_ENV")

  # ensure cacert.pem goes to right folder
  withr::local_envvar(TMPDIR = tempdir())

  # find Python 3 binary for testing
  python3 <- Sys.which("python3")
  if (!nzchar(python3))
    skip("test requires Python 3 binary with venv module")

  # create a virtual environment in the tempdir
  venv <- tempfile("python-3-venv")
  status <- tryCatch(system(paste(python3, "-m venv", venv)), error = identity)
  if (inherits(status, "error"))
    skip("test requires Python 3 binary with venv module")

  on.exit(unlink(venv, recursive = TRUE), add = TRUE)
  venv <- normalizePath(venv, mustWork = TRUE)

  # try running reticulate and binding to that virtual environment
  R <- file.path(R.home("bin"), "R")
  script <- normalizePath("resources/venv-activate.R")
  args <- c("--vanilla", "--slave", "-f", shQuote(script), "--args", shQuote(venv))
  output <- system2(R, args, stdout = TRUE)

  # test that we're using the site-packages dir in the venv
  site <- grep("site-packages", output, fixed = TRUE, value = TRUE)
  expect_true(grepl(venv, site))

})