Debian Codegraph @

R/tests.R

raw
1
2 the$tests_root <- NULL
3
4 # NOTE: Prefer using 'testing()' to 'renv_tests_running()' for behavior
5 # that should apply regardless of the package currently being tested.
6 #
7 # renv_tests_running() is appropriate when running renv's own tests.
8 renv_tests_running <- function() {
9 getOption("renv.tests.running", default = FALSE)
10 }
11
12 renv_test_code <- function(code, data = list(), fileext = ".R", scope = parent.frame()) {
13 code <- do.call(substitute, list(substitute(code), data))
14 file <- renv_scope_tempfile("renv-code-", fileext = fileext, scope = scope)
15
16 writeLines(deparse(code), con = file)
17 file
18 }
19
20 renv_test_retrieve <- function(record) {
21
22 renv_scope_error_handler()
23
24 # avoid using cache
25 cache <- renv_scope_tempfile()
26 renv_scope_envvars(RENV_PATHS_CACHE = cache)
27
28 # construct records
29 package <- record$Package
30 records <- list(record)
31 names(records) <- package
32
33 # prepare dummy library
34 templib <- renv_scope_tempfile("renv-library-")
35 ensure_directory(templib)
36 renv_scope_libpaths(c(templib, .libPaths()))
37
38 # attempt a restore into that library
39 renv_scope_restore(
40 project = getwd(),
41 library = templib,
42 records = records,
43 packages = package,
44 recursive = TRUE
45 )
46
47 records <- renv_retrieve_impl(record$Package)
48 renv_install_impl(records)
49
50 descpath <- file.path(templib, package)
51 if (!file.exists(descpath))
52 stopf("failed to retrieve package '%s'", package)
53
54 desc <- renv_description_read(descpath)
55 fields <- grep("^Remote", names(record), value = TRUE)
56
57 testthat::expect_identical(
58 as.list(desc[fields]),
59 as.list(record[fields])
60 )
61
62 }
63
64 renv_tests_diagnostics <- function() {
65
66 # print library paths
67 bulletin(
68 "The following R libraries are set:",
69 paste("-", .libPaths())
70 )
71
72 # print repositories
73 repos <- getOption("repos")
74 bulletin(
75 "The following repositories are set:",
76 paste(names(repos), repos, sep = ": ")
77 )
78
79 # print renv root
80 bulletin(
81 "The following renv root directory is being used:",
82 paste("-", paths$root())
83 )
84
85 # print cache root
86 bulletin(
87 "The following renv cache directory is being used:",
88 paste("-", paths$cache())
89 )
90
91 writeLines("The following packages are available in the test repositories:")
92
93 dbs <-
94 available_packages(type = "source", quiet = TRUE) %>%
95 map(function(db) {
96 rownames(db) <- NULL
97 db[c("Package", "Version", "File")]
98 })
99
100 print(dbs)
101
102 path <- Sys.getenv("PATH")
103 splat <- strsplit(path, .Platform$path.sep, fixed = TRUE)[[1]]
104
105 bulletin(
106 "The following PATH is set:",
107 paste("-", splat)
108 )
109
110 envvars <- c(
111 grep("^_R_", names(Sys.getenv()), value = TRUE),
112 "HOME",
113 "R_ARCH", "R_HOME",
114 "R_LIBS", "R_LIBS_SITE", "R_LIBS_USER", "R_USER",
115 "R_ZIPCMD",
116 "TAR", "TEMP", "TMP", "TMPDIR"
117 )
118
119 keys <- format(envvars)
120 vals <- Sys.getenv(envvars, unset = "<NA>")
121 vals[vals != "<NA>"] <- renv_json_quote(vals[vals != "<NA>"])
122
123 bulletin(
124 "The following environment variables of interest are set:",
125 paste(keys, vals, sep = " : ")
126 )
127
128 }
129
130 renv_tests_root <- function() {
131 the$tests_root <- the$tests_root %||% {
132 renv_path_normalize(testthat::test_path("."))
133 }
134 }
135
136 renv_tests_path <- function(path = NULL) {
137
138 # special case for NULL path
139 if (is.null(path))
140 return(renv_tests_root())
141
142 # otherwise, form path from root
143 file.path(renv_tests_root(), path)
144
145 }
146
147 renv_tests_supported <- function() {
148
149 # supported when running locally + on CI
150 for (envvar in c("NOT_CRAN", "CI"))
151 if (renv_envvar_exists(envvar))
152 return(TRUE)
153
154 # disabled on older macOS releases (credentials fails to load)
155 if (renv_platform_macos() && getRversion() < "4.0.0")
156 return(FALSE)
157
158 # disabled on Windows
159 if (renv_platform_windows())
160 return(FALSE)
161
162 # true otherwise
163 TRUE
164
165 }