Debian Codegraph @

tests/testthat/test-cache.R

raw ◀ ▶
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 skip("This test fails in Debian autopkgtest environment")
74
75 renv_tests_scope()
76
77 cache <- renv_scope_tempfile("renv-cache-")
78 dir.create(cache, mode = "0555")
79 renv_scope_envvars(RENV_PATHS_CACHE = cache)
80
81 init()
82 records <- install("bread")
83 expect_true(records$bread$Package == "bread")
84
85 location <- find.package("bread")
86 type <- renv_file_type(location)
87 expect_false(type == "symlink")
88
89 })
90
91 test_that("the cache is used even if RENV_PATHS_LIBRARY is non-canonical", {
92 skip_on_os("windows")
93
94 libpath <- renv_scope_tempfile("renv-library")
95 ensure_directory(libpath)
96 renv_scope_envvars(RENV_PATHS_LIBRARY = file.path(libpath, "."))
97
98 renv_tests_scope("bread")
99 init()
100 remove("bread")
101 restore()
102
103 bread <- system.file(package = "bread")
104 expect_true(renv_file_type(bread) == "symlink")
105
106 })
107
108 test_that("malformed folders in the cache are ignored", {
109 skip_on_cran()
110 renv_tests_scope()
111
112 cachepath <- renv_scope_tempfile("renv-cache-")
113 renv_scope_envvars(RENV_PATHS_CACHE = cachepath)
114
115 badpath <- renv_paths_cache("a-b/c-d/e-f/g-h/i-j")
116 dir.create(dirname(badpath), recursive = TRUE)
117 file.create(badpath)
118
119 paths <- list.files(renv_paths_cache(), recursive = TRUE)
120 expect_length(paths, 1)
121
122 paths <- renv_cache_list()
123 expect_length(paths, 0)
124
125 })
126
127 test_that("corrupt Meta/package.rds is detected", {
128 skip_on_cran()
129 renv_tests_scope()
130
131 cachepath <- renv_scope_tempfile("renv-cache-")
132 renv_scope_envvars(RENV_PATHS_CACHE = cachepath)
133
134 init()
135 install("bread")
136
137 path <- renv_cache_find(list(Package = "bread", Version = "1.0.0"))
138 expect_true(nzchar(path) && file.exists(path))
139
140 metapath <- file.path(path, "Meta/package.rds")
141 expect_true(file.exists(metapath))
142
143 writeLines("whoops!", con = file.path(path, "Meta/package.rds"))
144
145 diagnostics <- renv_cache_diagnose(verbose = FALSE)
146
147 expect_true(is.data.frame(diagnostics))
148 expect_true(nrow(diagnostics) == 1)
149 expect_true(diagnostics$Package == "bread")
150 expect_true(diagnostics$Version == "1.0.0")
151
152 })
153
154 test_that("invalid Built field is detected", {
155
156 skip_on_cran()
157 renv_tests_scope()
158
159 cachepath <- renv_scope_tempfile("renv-cache-")
160 renv_scope_envvars(RENV_PATHS_CACHE = cachepath)
161
162 init()
163 install("bread")
164
165 path <- renv_cache_find(list(Package = "bread", Version = "1.0.0"))
166 expect_true(nzchar(path) && file.exists(path))
167
168 descpath <- file.path(path, "DESCRIPTION")
169 contents <- readLines(descpath)
170
171 old <- paste("R", getRversion())
172 new <- paste("R 1.0.0")
173 replaced <- gsub(old, new, contents)
174 writeLines(replaced, con = descpath)
175
176 diagnostics <- renv_cache_diagnose(verbose = FALSE)
177
178 expect_true(is.data.frame(diagnostics))
179 expect_true(nrow(diagnostics) == 1)
180 expect_true(diagnostics$Package == "bread")
181 expect_true(diagnostics$Version == "1.0.0")
182
183 })
184
185 test_that("ACLs set on packages in project library are reset", {
186
187 skip_on_cran()
188 skip_if(!renv_platform_linux())
189
190 # use a custom tracer to set ACLs on a package after it's been installed
191 # but before cache sync runs (which may reset them); we trace
192 # renv_graph_install_finalize with an entry tracer so that ACLs are
193 # modified before renv_cache_synchronize is called inside it
194 renv_scope_trace(
195 what = renv:::renv_graph_install_finalize,
196 tracer = quote({
197 installpath <- file.path(library, record$Package)
198 system(paste("setfacl -m g::-", renv_shell_path(installpath)))
199 })
200 )
201
202 # use a custom cache
203 cachedir <- renv_scope_tempfile("renv-cache-")
204 ensure_directory(cachedir)
205 renv_scope_envvars(RENV_PATHS_CACHE = cachedir)
206
207 # initialize project with bread; don't try to reset ACLs
208 local({
209
210 renv_scope_envvars(RENV_CACHE_ACLS = "FALSE")
211 renv_tests_scope("bread")
212 init()
213
214 # check that the ACLs were not reset
215 pkgpath <- find.package("bread")
216 mode <- file.mode(pkgpath)
217 expect_false(file.mode(!!pkgpath) == file.mode(!!dirname(pkgpath)))
218 })
219
220 # try again, but reset ACLs this time
221 local({
222
223 renv_scope_envvars(RENV_CACHE_ACLS = "TRUE")
224 renv_tests_scope("toast")
225 init()
226
227 # check that the ACLs were reset this time
228 pkgpath <- find.package("toast")
229 mode <- file.mode(pkgpath)
230 expect_true(file.mode(!!pkgpath) == file.mode(!!dirname(pkgpath)))
231
232 })
233
234 })
235
236 # test_that("multiple cache directories are used", {
237 # skip_on_cran()
238 # skip_on_os("windows") # setting folder permissions is a bit more complex on windows
239 #
240 # chmod <- function(path, mode = c("read", "read+write")) {
241 # mode <- match.arg(mode)
242 # path <- normalizePath(path)
243 # stopifnot(file.exists(path))
244 # # '5' (read and execute) is the minimum permission that will work.
245 # # With '4' (read only) things like dir() don't work anymore.
246 # numbers <- if (mode == "read") "555" else "777"
247 # system(sprintf("chmod %s %s", numbers, path))
248 # }
249 #
250 # # use multiple temporary caches for this test
251 # tempcache1 <- tempfile("renv-tempcache-")
252 # ensure_directory(tempcache1)
253 # tempcache2 <- tempfile("renv-tempcache-")
254 # ensure_directory(tempcache2)
255 #
256 # defer({
257 # unlink(tempcache1, recursive = TRUE)
258 # unlink(tempcache2, recursive = TRUE)
259 # })
260 #
261 # # add both packages to the cache
262 # renv_scope_envvars(RENV_PATHS_CACHE = paste(tempcache1, tempcache2, sep = ";"))
263 #
264 # # initialize project
265 # renv_tests_scope()
266 # init()
267 #
268 # # there should be two paths in the cache
269 # expect_length(paths$cache(), 2L)
270 #
271 # # install bread to first cache path
272 # install("bread")
273 #
274 # # test that there is one package (bread) and it is installed in the first cache
275 # cache <- renv_cache_list()
276 # expect_length(cache, 1L)
277 # expect_true(startsWith(cache[basename(cache) == "bread"], tempcache1))
278 #
279 # # make the first cache read only
280 # chmod(paths$cache()[1L], "read")
281 #
282 # # install oatmeal to second cache path
283 # install("oatmeal")
284 #
285 # # test that there are 2 packages and the latest package (oatmeal) is installed in the second cache
286 # cache <- renv_cache_list()
287 # expect_length(cache, 2L)
288 # expect_true(startsWith(cache[basename(cache) == "oatmeal"], tempcache2))
289 #
290 # # make the first cache read+write again, should now install into the first cache again
291 # chmod(paths$cache()[1L], "read+write")
292 #
293 # install("toast")
294 #
295 # # test that there are 3 packages and the latest package (toast) is installed in the first cache
296 # cache <- renv_cache_list()
297 # expect_length(cache, 3L)
298 # expect_true(startsWith(cache[basename(cache) == "toast"], tempcache1))
299 #
300 # })