tests/testthat/test-clean-subprocess.R

test_that("r() does not load anything", {
  skip_in_covr()
  skip_if_not_installed("withr")
  pkgs <- withr::with_envvar(
    clean_envvars(),
    r(without_env(function() loadedNamespaces())))
  if (length(pkgs) > 1) print(pkgs)
  ## Some R versions still load compiler...
  expect_true(all(pkgs %in% c("base", "compiler")))
})

test_that("r_bg() does not load anything", {
  skip_in_covr()
  skip_if_not_installed("withr")
  p <- withr::with_envvar(
    clean_envvars(),
    r_bg(without_env(function() loadedNamespaces())))
  on.exit(p$kill(), add = TRUE)
  p$wait(3000)
  pkgs <- p$get_result()
  if (length(pkgs) > 1) print(pkgs)
  ## Some R versions still load compiler...
  expect_true(all(pkgs %in% c("base", "compiler")))
})

test_that("r_session does not load anything", {
  skip_in_covr()
  skip_if_not_installed("withr")
  rs <- withr::with_envvar(clean_envvars(), r_session$new())
  on.exit(rs$close(), add = TRUE)
  pkgs <- rs$run(without_env(function() loadedNamespaces()))
  if (length(pkgs) > 1) print(pkgs)
  ## Some R versions still load compiler...
  expect_true(all(pkgs %in% c("base", "compiler")))
  gc()
})

test_that("r() does not create objects in global env", {
  vars <- r(function() ls(.GlobalEnv))
  expect_identical(vars, character())
})

test_that("r_bg() does not create objects in global env", {
  p <- r_bg(function() ls(.GlobalEnv))
  on.exit(p$kill(), add = TRUE)
  p$wait(3000)
  vars <- p$get_result()
  expect_identical(vars, character())
})

test_that("r_session does not create objects in global env", {
  rs <- r_session$new()
  on.exit(rs$close(), add = TRUE)
  vars <- rs$run(function() ls(.GlobalEnv))
  expect_identical(vars, character())
  rs$close()
  gc()
})
MangoTheCat/callr documentation built on March 26, 2024, 11:23 a.m.