tests/testthat/test-python-virtual-environments.R

context("virtual environments")

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

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

  # 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))

})
merlingrace/reticulate1 documentation built on Nov. 4, 2019, 6:25 p.m.