Debian Codegraph @

tests/testthat/helper-scope.R

raw
1
2 renv_tests_scope <- function(packages = character(),
3 project = NULL,
4 isolated = FALSE,
5 libpaths = FALSE,
6 scope = parent.frame())
7 {
8 # use private repositories in this scope
9 renv_tests_scope_repos(scope = scope)
10
11 # disable network-backed crandb lookups; test fixture packages
12 # (e.g. "breakfast") collide with real CRAN package names
13 renv_tests_scope_crandb(enabled = FALSE, scope = scope)
14
15 # use sandbox in this scope, to guard against packages like 'bread'
16 # being installed into the system library
17 renv_scope_sandbox(scope = scope)
18
19 # most tests will call init() which changes `R_LIBS_USER`;
20 # this ensures we reset to the original value when the test is done
21 renv_scope_envvars(R_LIBS_USER = NULL, scope = scope)
22
23 # also ensure we 'exit' the current project after completion
24 renv_scope_envvars(RENV_PROJECT = "", scope = scope)
25
26 # if we're trying to be isolated from other test infrastructure
27 if (isolated) {
28 root <- tempfile("renv-root-")
29 ensure_directory(root)
30 renv_scope_envvars(RENV_PATHS_ROOT = root, scope = scope)
31 }
32
33 # move to own test directory
34 project <- project %||% renv_scope_tempfile("renv-test-", scope = scope)
35 ensure_directory(project)
36 renv_scope_wd(project, scope = scope)
37
38 # scope project here so that it's unset after load, just in case
39 defer(renv_project_clear(), scope = scope)
40
41 # create empty renv directory
42 dir.create("renv")
43
44 # create file with dependencies
45 code <- sprintf("library(%s)", packages)
46 writeLines(code, "dependencies.R")
47
48 # use a temporary user directory so that package-type projects
49 # don't write into the persistent user library directory
50 userdir <- renv_scope_tempfile("renv-userdir-", scope = scope)
51 renv_scope_options(renv.userdir.override = userdir, scope = scope)
52
53 # use temporary library
54 library <- renv_scope_tempfile("renv-library-", scope = scope)
55 ensure_directory(library)
56 renv_scope_libpaths(c(library, if (libpaths) .libPaths()), scope = scope)
57
58 # return path to project directory
59 invisible(renv_path_normalize(project))
60 }
61
62 renv_tests_scope_repos <- function(scope = parent.frame()) {
63
64 # get path to on-disk repository
65 repopath <- renv_tests_repopath()
66
67 # update our repos option
68 fmt <- if (renv_platform_windows()) "file:///%s" else "file://%s"
69 repos <- c(CRAN = sprintf(fmt, repopath))
70
71 renv_scope_options(
72 pkgType = "source",
73 repos = repos,
74 scope = scope
75 )
76
77 }
78
79 renv_tests_scope_crandb <- function(enabled = FALSE, scope = parent.frame()) {
80
81 if (enabled)
82 return(invisible())
83
84 renv_scope_binding(
85 envir = asNamespace("renv"),
86 symbol = "renv_graph_description_crandb",
87 replacement = function(package, version) {
88 stop("crandb disabled in renv_tests_scope")
89 },
90 scope = scope
91 )
92
93 }
94
95 renv_tests_scope_system_cache <- function(scope = parent.frame()) {
96 skip_on_cran()
97 renv_scope_options(repos = c(CRAN = "https://cloud.r-project.org"), scope = scope)
98 renv_scope_envvars(RENV_PATHS_ROOT = renv_paths_root_default_impl(), scope = scope)
99 }
100
101 renv_scope_local <- function() {
102 path <- renv_tests_path("local")
103 renv_scope_envvars(RENV_PATHS_LOCAL = path, scope = parent.frame())
104 }
105
106 renv_tests_dependencies <- function(packages) {
107 code <- sprintf("library(%s)", packages)
108 cat(code, file = "dependencies.R", sep = "\n", append = TRUE)
109 }