Debian Codegraph @

tests/testthat/test-cache.R

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1
2 test_that("issues within the cache are reported", {
3 skip_on_cran()
4
5 # use a temporary cache for this test as we're going
6 # to mutate and invalidate it
7 tempcache <- renv_scope_tempfile("renv-tempcache-")
8 ensure_directory(tempcache)
9 renv_scope_envvars(RENV_PATHS_CACHE = tempcache)
10
11 # initialize project
12 renv_tests_scope("breakfast")
13 init()
14
15 # find packages in the cache
16 cache <- renv_cache_list()
17
18 # diagnostics for missing DESCRIPTION
19 bread <- renv_cache_list(packages = "bread")
20 descpath <- file.path(bread, "DESCRIPTION")
21 unlink(descpath)
22 if (length(descpath) > 1L) {
23 writeLines(descpath)
24 stop("unexpected descpath")
25 }
26
27
28 # diagnostics for bad hash
29 breakfast <- renv_cache_list(packages = "breakfast")
30 descpath <- file.path(breakfast, "DESCRIPTION")
31 if (length(descpath) > 1L) {
32 writeLines(descpath)
33 stop("unexpected descpath")
34 }
35
36 desc <- renv_description_read(descpath)
37 desc$Version <- "2.0.0"
38 renv_dcf_write(desc, file = descpath)
39
40 # check problems explicitly
41 problems <- renv_cache_diagnose(verbose = FALSE)
42 expect_true(nrow(problems) == 2)
43
44 })
45
46 test_that("use.cache project setting is honored", {
47 skip_on_os("windows")
48
49 renv_tests_scope("breakfast")
50
51 init()
52
53 packages <- list.files(renv_paths_library(), full.names = TRUE)
54 types <- renv_file_type(packages)
55 expect_true(all(types == "symlink"))
56
57 settings$use.cache(FALSE)
58
59 packages <- list.files(renv_paths_library(), full.names = TRUE)
60 types <- renv_file_type(packages)
61 expect_true(all(types == "directory"))
62
63 settings$use.cache(TRUE)
64
65 packages <- list.files(renv_paths_library(), full.names = TRUE)
66 types <- renv_file_type(packages)
67 expect_true(all(types == "symlink"))
68
69 })
70
71 test_that("package installation does not fail with non-writable cache", {
72 skip_on_os("windows")
73
74 renv_tests_scope()
75
76 cache <- renv_scope_tempfile("renv-cache-")
77 dir.create(cache, mode = "0555")
78 renv_scope_envvars(RENV_PATHS_CACHE = cache)
79
80 init()
81 records <- install("bread")
82 expect_true(records$bread$Package == "bread")
83
84 location <- find.package("bread")
85 type <- renv_file_type(location)
86 expect_false(type == "symlink")
87
88 })
89
90 test_that("the cache is used even if RENV_PATHS_LIBRARY is non-canonical", {
91 skip_on_os("windows")
92
93 libpath <- renv_scope_tempfile("renv-library")
94 ensure_directory(libpath)
95 renv_scope_envvars(RENV_PATHS_LIBRARY = file.path(libpath, "."))
96
97 renv_tests_scope("bread")
98 init()
99 remove("bread")
100 restore()
101
102 bread <- system.file(package = "bread")
103 expect_true(renv_file_type(bread) == "symlink")
104
105 })
106
107 test_that("malformed folders in the cache are ignored", {
108 skip_on_cran()
109 renv_tests_scope()
110
111 cachepath <- renv_scope_tempfile("renv-cache-")
112 renv_scope_envvars(RENV_PATHS_CACHE = cachepath)
113
114 badpath <- renv_paths_cache("a-b/c-d/e-f/g-h/i-j")
115 dir.create(dirname(badpath), recursive = TRUE)
116 file.create(badpath)
117
118 paths <- list.files(renv_paths_cache(), recursive = TRUE)
119 expect_length(paths, 1)
120
121 paths <- renv_cache_list()
122 expect_length(paths, 0)
123
124 })
125
126 test_that("corrupt Meta/package.rds is detected", {
127 skip_on_cran()
128 renv_tests_scope()
129
130 cachepath <- renv_scope_tempfile("renv-cache-")
131 renv_scope_envvars(RENV_PATHS_CACHE = cachepath)
132
133 init()
134 install("bread")
135
136 path <- renv_cache_find(list(Package = "bread", Version = "1.0.0"))
137 expect_true(nzchar(path) && file.exists(path))
138
139 metapath <- file.path(path, "Meta/package.rds")
140 expect_true(file.exists(metapath))
141
142 writeLines("whoops!", con = file.path(path, "Meta/package.rds"))
143
144 diagnostics <- renv_cache_diagnose(verbose = FALSE)
145
146 expect_true(is.data.frame(diagnostics))
147 expect_true(nrow(diagnostics) == 1)
148 expect_true(diagnostics$Package == "bread")
149 expect_true(diagnostics$Version == "1.0.0")
150
151 })
152
153 test_that("invalid Built field is detected", {
154
155 skip_on_cran()
156 renv_tests_scope()
157
158 cachepath <- renv_scope_tempfile("renv-cache-")
159 renv_scope_envvars(RENV_PATHS_CACHE = cachepath)
160
161 init()
162 install("bread")
163
164 path <- renv_cache_find(list(Package = "bread", Version = "1.0.0"))
165 expect_true(nzchar(path) && file.exists(path))
166
167 descpath <- file.path(path, "DESCRIPTION")
168 contents <- readLines(descpath)
169
170 old <- paste("R", getRversion())
171 new <- paste("R 1.0.0")
172 replaced <- gsub(old, new, contents)
173 writeLines(replaced, con = descpath)
174
175 diagnostics <- renv_cache_diagnose(verbose = FALSE)
176
177 expect_true(is.data.frame(diagnostics))
178 expect_true(nrow(diagnostics) == 1)
179 expect_true(diagnostics$Package == "bread")
180 expect_true(diagnostics$Version == "1.0.0")
181
182 })
183
184 test_that("ACLs set on packages in project library are reset", {
185
186 skip_on_cran()
187 skip_if(!renv_platform_linux())
188
189 # use a custom tracer to set ACLs on a package after it's been installed
190 # but before cache sync runs (which may reset them); we trace
191 # renv_graph_install_finalize with an entry tracer so that ACLs are
192 # modified before renv_cache_synchronize is called inside it
193 renv_scope_trace(
194 what = renv:::renv_graph_install_finalize,
195 tracer = quote({
196 installpath <- file.path(library, record$Package)
197 system(paste("setfacl -m g::-", renv_shell_path(installpath)))
198 })
199 )
200
201 # use a custom cache
202 cachedir <- renv_scope_tempfile("renv-cache-")
203 ensure_directory(cachedir)
204 renv_scope_envvars(RENV_PATHS_CACHE = cachedir)
205
206 # initialize project with bread; don't try to reset ACLs
207 local({
208
209 renv_scope_envvars(RENV_CACHE_ACLS = "FALSE")
210 renv_tests_scope("bread")
211 init()
212
213 # check that the ACLs were not reset
214 pkgpath <- find.package("bread")
215 mode <- file.mode(pkgpath)
216 expect_false(file.mode(!!pkgpath) == file.mode(!!dirname(pkgpath)))
217 })
218
219 # try again, but reset ACLs this time
220 local({
221
222 renv_scope_envvars(RENV_CACHE_ACLS = "TRUE")
223 renv_tests_scope("toast")
224 init()
225
226 # check that the ACLs were reset this time
227 pkgpath <- find.package("toast")
228 mode <- file.mode(pkgpath)
229 expect_true(file.mode(!!pkgpath) == file.mode(!!dirname(pkgpath)))
230
231 })
232
233 })
234
235 # test_that("multiple cache directories are used", {
236 # skip_on_cran()
237 # skip_on_os("windows") # setting folder permissions is a bit more complex on windows
238 #
239 # chmod <- function(path, mode = c("read", "read+write")) {
240 # mode <- match.arg(mode)
241 # path <- normalizePath(path)
242 # stopifnot(file.exists(path))
243 # # '5' (read and execute) is the minimum permission that will work.
244 # # With '4' (read only) things like dir() don't work anymore.
245 # numbers <- if (mode == "read") "555" else "777"
246 # system(sprintf("chmod %s %s", numbers, path))
247 # }
248 #
249 # # use multiple temporary caches for this test
250 # tempcache1 <- tempfile("renv-tempcache-")
251 # ensure_directory(tempcache1)
252 # tempcache2 <- tempfile("renv-tempcache-")
253 # ensure_directory(tempcache2)
254 #
255 # defer({
256 # unlink(tempcache1, recursive = TRUE)
257 # unlink(tempcache2, recursive = TRUE)
258 # })
259 #
260 # # add both packages to the cache
261 # renv_scope_envvars(RENV_PATHS_CACHE = paste(tempcache1, tempcache2, sep = ";"))
262 #
263 # # initialize project
264 # renv_tests_scope()
265 # init()
266 #
267 # # there should be two paths in the cache
268 # expect_length(paths$cache(), 2L)
269 #
270 # # install bread to first cache path
271 # install("bread")
272 #
273 # # test that there is one package (bread) and it is installed in the first cache
274 # cache <- renv_cache_list()
275 # expect_length(cache, 1L)
276 # expect_true(startsWith(cache[basename(cache) == "bread"], tempcache1))
277 #
278 # # make the first cache read only
279 # chmod(paths$cache()[1L], "read")
280 #
281 # # install oatmeal to second cache path
282 # install("oatmeal")
283 #
284 # # test that there are 2 packages and the latest package (oatmeal) is installed in the second cache
285 # cache <- renv_cache_list()
286 # expect_length(cache, 2L)
287 # expect_true(startsWith(cache[basename(cache) == "oatmeal"], tempcache2))
288 #
289 # # make the first cache read+write again, should now install into the first cache again
290 # chmod(paths$cache()[1L], "read+write")
291 #
292 # install("toast")
293 #
294 # # test that there are 3 packages and the latest package (toast) is installed in the first cache
295 # cache <- renv_cache_list()
296 # expect_length(cache, 3L)
297 # expect_true(startsWith(cache[basename(cache) == "toast"], tempcache1))
298 #
299 # })