Debian Codegraph @

tests/testthat/test-snapshot.R

raw
1
2 test_that("snapshot is idempotent", {
3
4 renv_tests_scope("oatmeal")
5
6 init(bare = TRUE)
7 install("oatmeal")
8 snapshot()
9 before <- renv_lockfile_read("renv.lock")
10 snapshot()
11 after <- renv_lockfile_read("renv.lock")
12 expect_equal(before, after)
13
14 })
15
16 test_that("snapshot failures are reported", {
17
18 renv_tests_scope("oatmeal")
19 init()
20
21 descpath <- system.file("DESCRIPTION", package = "oatmeal")
22 unlink(descpath)
23 expect_snapshot(snapshot())
24
25 })
26
27 test_that("broken symlinks are reported", {
28 skip_on_os("windows")
29
30 renv_scope_envvars(RENV_PATHS_ROOT = renv_scope_tempfile())
31 renv_tests_scope("oatmeal")
32 init()
33
34 oatmeal <- renv_path_normalize(system.file(package = "oatmeal"))
35 unlink(oatmeal, recursive = TRUE)
36 expect_snapshot(snapshot())
37
38 })
39
40 test_that("multiple libraries can be used when snapshotting", {
41
42 renv_scope_envvars(RENV_PATHS_ROOT = renv_scope_tempfile())
43 renv_tests_scope()
44
45 init()
46
47 lib1 <- renv_scope_tempfile("renv-lib1-")
48 lib2 <- renv_scope_tempfile("renv-lib2-")
49 ensure_directory(c(lib1, lib2))
50
51 oldlibpaths <- .libPaths()
52 .libPaths(c(lib1, lib2))
53
54 install("bread", library = lib1)
55 breadloc <- find.package("bread")
56 expect_true(renv_file_same(dirname(breadloc), lib1))
57
58 install("toast", library = lib2)
59 toastloc <- find.package("toast")
60 expect_true(renv_file_same(dirname(toastloc), lib2))
61
62 libs <- c(lib1, lib2)
63 lockfile <- snapshot(lockfile = NULL, library = libs, type = "all")
64 records <- renv_lockfile_records(lockfile)
65
66 expect_length(records, 2L)
67 expect_setequal(names(records), c("bread", "toast"))
68
69 .libPaths(oldlibpaths)
70
71 })
72
73 test_that("implicit snapshots only include packages currently used", {
74
75 renv_tests_scope("oatmeal")
76 init()
77
78 # install toast, but don't declare that we use it
79 install("toast")
80 lockfile <- snapshot(type = "implicit", lockfile = NULL)
81 records <- renv_lockfile_records(lockfile)
82 expect_length(records, 1L)
83 expect_setequal(names(records), "oatmeal")
84
85 # use toast
86 writeLines("library(toast)", con = "toast.R")
87 lockfile <- snapshot(type = "packrat", lockfile = NULL)
88 records <- renv_lockfile_records(lockfile)
89 expect_length(records, 3L)
90 expect_setequal(names(records), c("oatmeal", "bread", "toast"))
91
92 })
93
94 test_that("explicit snapshots only capture packages in DESCRIPTION", {
95
96 renv_tests_scope("breakfast")
97 init()
98
99 desc <- list(Type = "Project", Depends = "toast")
100
101 write.dcf(desc, file = "DESCRIPTION")
102 lockfile <- snapshot(type = "explicit", lockfile = NULL)
103 records <- renv_lockfile_records(lockfile)
104 expect_true(length(records) == 2L)
105 expect_true(!is.null(records[["bread"]]))
106 expect_true(!is.null(records[["toast"]]))
107
108 })
109
110 test_that("a custom snapshot filter can be used", {
111 skip_on_cran()
112 renv_tests_scope("breakfast")
113
114 settings$snapshot.type("custom")
115 filter <- function(project) c("bread", "toast")
116 renv_scope_options(renv.snapshot.filter = filter)
117
118 init()
119 lockfile <- renv_lockfile_load(project = getwd())
120 expect_setequal(names(renv_lockfile_records(lockfile)), c("bread", "toast"))
121
122 })
123
124 test_that("snapshotted packages from CRAN include the Repository field", {
125
126 renv_tests_scope("bread")
127 init()
128
129 lockfile <- renv_lockfile_read("renv.lock")
130 records <- renv_lockfile_records(lockfile)
131 expect_true(records$bread$Repository == "CRAN")
132
133 })
134
135 test_that("snapshot failures due to bad library / packages are reported", {
136 project <- renv_tests_scope("oatmeal")
137 init()
138 isolate()
139 writeLines("invalid", con = system.file("DESCRIPTION", package = "oatmeal"))
140 expect_error(snapshot())
141 })
142
143 test_that("snapshot ignores own package in package development scenarios", {
144
145 renv_tests_scope()
146 ensure_directory("bread")
147 renv_scope_wd("bread")
148
149 writeLines(c("Type: Package", "Package: bread"), con = "DESCRIPTION")
150
151 ensure_directory("R")
152 writeLines("function() { library(bread) }", con = "R/deps.R")
153
154 lockfile <- snapshot(lockfile = NULL)
155 records <- renv_lockfile_records(lockfile)
156 expect_true(is.null(records[["bread"]]))
157
158 })
159
160 test_that("snapshot warns about unsatisfied dependencies", {
161
162 renv_tests_scope("toast")
163 init(settings = list(use.cache = FALSE))
164
165 descpath <- system.file("DESCRIPTION", package = "toast")
166 toast <- renv_description_read(descpath)
167 toast$Depends <- "bread (> 1.0.0)"
168 renv_dcf_write(toast, file = descpath)
169
170 expect_snapshot(snapshot(), error = TRUE)
171
172 })
173
174 test_that("snapshot records packages discovered in cellar", {
175
176 renv_tests_scope("skeleton")
177 renv_scope_envvars(
178 RENV_PATHS_CACHE = renv_scope_tempfile(),
179 RENV_PATHS_LOCAL = renv_tests_path("local")
180 )
181
182 init(bare = TRUE)
183
184 record <- list(Package = "skeleton", Version = "1.0.1")
185 records <- install(list(record))
186
187 # validate the record in the lockfile
188 lockfile <- snapshot(lockfile = NULL)
189 records <- renv_lockfile_records(lockfile)
190 skeleton <- records[["skeleton"]]
191
192 expect_equal(skeleton$Package, "skeleton")
193 expect_equal(skeleton$Version, "1.0.1")
194 expect_equal(skeleton$Source, "Cellar")
195
196 })
197
198 test_that("snapshot prefers RemoteType to biocViews", {
199
200 desc <- list(
201 Package = "test",
202 Version = "1.0",
203 RemoteType = "github",
204 biocViews = "Biology"
205 )
206
207 descfile <- renv_scope_tempfile()
208 renv_dcf_write(desc, file = descfile)
209 record <- renv_snapshot_description(descfile)
210 expect_identical(record$Source, "GitHub")
211
212 })
213
214 test_that("parse errors cause snapshot to abort", {
215
216 renv_tests_scope()
217
218 # write invalid code to an R file
219 writeLines("parse error", con = "parse-error.R")
220
221 # init should succeed even with parse errors
222 init(bare = TRUE)
223
224 # snapshots should fail when configured to do so
225 renv_scope_options(renv.config.dependency.errors = "fatal")
226 expect_error(snapshot())
227
228 })
229
230 test_that("records for packages available on other OSes are preserved", {
231 skip_on_os("windows")
232 renv_tests_scope("unixonly")
233
234 init()
235
236 # fake a windows-only record
237 lockfile <- renv_lockfile_read("renv.lock")
238 lockfile$Packages$windowsonly <- lockfile$Packages$unixonly
239 lockfile$Packages$windowsonly$Package <- "windowsonly"
240 lockfile$Packages$windowsonly$Hash <- NULL
241 lockfile$Packages$windowsonly$OS_type <- "windows"
242 renv_lockfile_write(lockfile, "renv.lock")
243
244 # call snapshot to update lockfile
245 snapshot()
246
247 # ensure that 'windowsonly' is still preserved
248 lockfile <- renv_lockfile_read("renv.lock")
249 expect_true(!is.null(lockfile$Packages$windowsonly))
250
251 })
252
253 test_that(".renvignore works during snapshot without an explicit root", {
254
255 renv_tests_scope()
256
257 # install bread
258 install("bread")
259
260 # create sub-directory that should be ignored
261 dir.create("ignored")
262 writeLines("library(bread)", con = "ignored/script.R")
263
264 lockfile <- snapshot(project = ".", lockfile = NULL)
265 expect_false(is.null(lockfile$Packages$bread))
266
267 writeLines("*", con = "ignored/.renvignore")
268
269 lockfile <- snapshot(project = ".", lockfile = NULL)
270 expect_true(is.null(lockfile$Packages$bread))
271
272 })
273
274 test_that("snapshot(packages = ...) captures package dependencies", {
275
276 renv_tests_scope("breakfast")
277
278 # init to install required packages
279 init()
280
281 # remove old lockfile
282 unlink("renv.lock")
283
284 # create lockfile
285 snapshot(packages = "breakfast")
286
287 # check for expected records
288 lockfile <- renv_lockfile_load(project = getwd())
289 records <- renv_lockfile_records(lockfile)
290
291 expect_true(!is.null(records$breakfast))
292 expect_true(!is.null(records$bread))
293 expect_true(!is.null(records$toast))
294 expect_true(!is.null(records$oatmeal))
295
296 })
297
298 test_that("snapshot() accepts relative library paths", {
299
300 renv_tests_scope("breakfast")
301
302 # initialize project
303 init()
304
305 # remove lockfile
306 unlink("renv.lock")
307
308 # form relative path to library
309 library <- substring(.libPaths()[1], nchar(getwd()) + 2)
310
311 # try to snapshot with relative library path
312 snapshot(library = library)
313
314 # test that snapshot succeeded
315 expect_true(file.exists("renv.lock"))
316
317 })
318
319 test_that("snapshot(update = TRUE) preserves old records", {
320
321 skip_on_cran()
322 skip_if_no_github_auth()
323
324 renv_tests_scope("breakfast")
325 init()
326
327 # remove breakfast, then try to snapshot again
328 old <- renv_lockfile_read("renv.lock")
329 remove("breakfast")
330 snapshot(update = TRUE)
331 new <- renv_lockfile_read("renv.lock")
332
333 expect_identical(names(old$Packages), names(new$Packages))
334
335 # try installing a package
336 old <- renv_lockfile_read("renv.lock")
337 writeLines("library(phone)", con = "phone.R")
338 install("phone")
339 snapshot(update = TRUE)
340 new <- renv_lockfile_read("renv.lock")
341
342 # check that we have our old record names
343 expect_true(all(old$Packages %in% new$Packages))
344
345 # now try removing 'breakfast'
346 snapshot(update = FALSE)
347 new <- renv_lockfile_read("renv.lock")
348 expect_false("breakfast" %in% names(new$Packages))
349
350 })
351
352 test_that("renv reports missing packages in explicit snapshots", {
353
354 renv_tests_scope()
355 init()
356
357 writeLines("Depends: breakfast", con = "DESCRIPTION")
358 expect_snapshot(snapshot(type = "explicit"))
359
360 })
361
362 test_that("a project using explicit snapshots is marked in sync appropriately", {
363
364 skip_on_cran()
365 renv_tests_scope()
366 renv_scope_options(renv.config.snapshot.type = "explicit")
367
368 init()
369
370 writeLines("Depends: breakfast", con = "DESCRIPTION")
371 expect_false(status()$synchronized)
372
373 install("breakfast")
374 expect_false(status()$synchronized)
375
376 snapshot()
377 expect_true(status()$synchronized)
378
379 })
380
381 test_that("snapshot() warns when required package is not installed", {
382
383 renv_tests_scope("breakfast")
384 init()
385
386 remove("breakfast")
387 expect_snapshot(snapshot())
388
389 install("breakfast")
390 remove("toast")
391 expect_snapshot(snapshot(), error = TRUE)
392
393 })
394
395 test_that("packages installed from CRAN using pak are handled", {
396 skip_on_cran()
397 skip_if_not_installed("pak")
398
399 renv_tests_scope()
400 library <- renv_paths_library()
401 ensure_directory(library)
402 pak <- renv_namespace_load("pak")
403 quietly(pak$pkg_install("toast"))
404 record <- renv_snapshot_description(package = "toast")
405
406 expected <- c("Package", "Version", "Source", "Repository")
407 expect_contains(names(record), expected)
408
409 expect_identical(record$Source, "Repository")
410 expect_identical(record$Repository, "CRAN")
411 })
412
413
414 test_that("packages installed from Bioconductor using pak are handled", {
415
416 skip("pak doesn't like file:// repositories?")
417
418 skip_on_cran()
419 skip_if_not_installed("pak")
420 skip_if(devel())
421
422 renv_tests_scope()
423 library <- renv_paths_library()
424 ensure_directory(library)
425 pak <- renv_namespace_load("pak")
426 suppressMessages(pak$pkg_install("bioc::Biobase"))
427
428 record <- renv_snapshot_description(package = "Biobase")
429 expect_identical(record$Source, "Bioconductor")
430 })
431
432 test_that("snapshot always reports on R version changes", {
433 renv_scope_options(renv.verbose = TRUE)
434
435 R4.1 <- list(R = list(Version = 4.1))
436 R4.2 <- list(R = list(Version = 4.2))
437 expect_snapshot({
438 renv_snapshot_report_actions(list(), R4.1, R4.2)
439 })
440 })
441
442 test_that("user can choose to install missing packages", {
443
444 # use a temporary cache to guarantee packages are fully installed
445 # regardless of order other tests are run in
446 renv_scope_envvars(RENV_PATHS_CACHE = renv_scope_tempfile("renv-tempcache-"))
447
448 renv_tests_scope("egg")
449 renv_scope_options(renv.menu.choice = 2)
450 expect_snapshot(snapshot())
451
452 })
453
454 test_that("exclude handles uninstalled packages", {
455 project <- renv_tests_scope("bread")
456 init()
457 snapshot(exclude = "bread")
458 lockfile <- renv_lockfile_load(project)
459 expect_null(lockfile$Packages$bread)
460 })
461
462 test_that("snapshot doesn't include development dependencies", {
463
464 renv_tests_scope()
465 writeLines(c("Imports: egg", "Suggests: bread"), "DESCRIPTION")
466
467 inst <- install()
468 expect_named(inst, c("bread", "egg"), ignore.order = TRUE)
469
470 snap <- snapshot()
471 expect_named(snap$Packages, "egg")
472
473 })
474
475 test_that("automatic snapshot works as expected", {
476
477 renv_scope_binding(the, "auto_snapshot_forced", TRUE)
478 defer(the$library_info <- NULL)
479
480 project <- renv_tests_scope("oatmeal")
481 init()
482 expect_silent(renv_snapshot_task())
483
484 # bread isn't used so snapshot shouldn't change
485 install("bread")
486 expect_silent(renv_snapshot_task())
487
488 writeLines("library(egg)", "dependencies.R")
489 install("egg")
490 expect_snapshot(renv_snapshot_task())
491
492 })
493
494 # test_that("we can infer github remotes from packages installed from sources", {
495 # skip_on_cran()
496 #
497 # desc <- heredoc("
498 # Package: renv
499 # Version: 0.1.0-9000
500 # BugReports: https://github.com/rstudio/renv/issues
501 # ")
502 #
503 # descfile <- renv_scope_tempfile("description-")
504 # writeLines(desc, con = descfile)
505 #
506 # remote <- local({
507 # renv_scope_options(renv.verbose = FALSE)
508 # renv_snapshot_description(path = descfile)
509 # })
510 #
511 # expect_equal(remote$RemoteType, "github")
512 #
513 # expect_snapshot(. <- renv_snapshot_description(path = descfile))
514 #
515 # })
516
517 test_that("we report if dependency discover during snapshot() is slow", {
518
519 renv_tests_scope()
520 init()
521
522 renv_scope_options(renv.dependencies.elapsed_time_threshold = -1)
523 expect_snapshot(. <- snapshot())
524
525 })
526
527 test_that("failures in automatic snapshots disable automatic snapshots", {
528
529 renv_scope_binding(the, "auto_snapshot_forced", TRUE)
530 defer(the$library_info <- NULL)
531
532 project <- renv_tests_scope("bread")
533 init()
534
535 count <- 0L
536 renv_scope_trace(renv:::renv_snapshot_auto, function() {
537 count <<- count + 1L
538 stop("simulated failure in snapshot task")
539 })
540
541 the$library_info <- list()
542 expect_false(the$auto_snapshot_failed)
543 expect_snapshot(renv_snapshot_task())
544 expect_true(the$auto_snapshot_failed)
545
546 the$library_info <- list()
547 expect_silent(renv_snapshot_task())
548 expect_equal(count, 1L)
549
550 })
551
552 # https://github.com/rstudio/renv/issues/1607
553 test_that("snapshot() reports missing packages even if renv.verbose is FALSE", {
554 project <- renv_tests_scope()
555 init()
556
557 renv_scope_options(renv.verbose = FALSE)
558 writeLines("library(bread)", con = "deps.R")
559 expect_snapshot(. <- snapshot(force = TRUE))
560 })
561
562 test_that("packages installed from r-universe preserve remote metadata", {
563
564 text <- heredoc("
565 Package: skeleton
566 Type: Package
567 Version: 1.1.0
568 Remotes: kevinushey/skeleton
569 Repository: https://kevinushey.r-universe.dev
570 RemoteUrl: https://github.com/kevinushey/skeleton
571 RemoteSha: e4aafb92b86ba7eba3b7036d9d96fdfb6c32761a
572 ")
573
574 path <- renv_scope_tempfile()
575 writeLines(text, con = path)
576
577 record <- renv_snapshot_description(path = path)
578 expect_identical(record[["RemoteSha"]], "e4aafb92b86ba7eba3b7036d9d96fdfb6c32761a")
579
580 })
581
582 test_that("standard remotes preserve RemoteSha if it's a hash", {
583
584 text <- heredoc("
585 Package: skeleton
586 Type: Package
587 Version: 1.1.0
588 Remotes: kevinushey/skeleton
589 Repository: https://kevinushey.r-universe.dev
590 RemoteType: standard
591 RemoteUrl: https://github.com/kevinushey/skeleton
592 RemoteSha: e4aafb92b86ba7eba3b7036d9d96fdfb6c32761a
593 ")
594
595 path <- renv_scope_tempfile()
596 writeLines(text, con = path)
597
598 record <- renv_snapshot_description(path = path)
599 expect_identical(record[["RemoteSha"]], "e4aafb92b86ba7eba3b7036d9d96fdfb6c32761a")
600
601 })
602
603 test_that("standard remotes drop RemoteSha if it's a version", {
604
605 text <- heredoc("
606 Package: skeleton
607 Type: Package
608 Version: 1.1.0
609 Remotes: kevinushey/skeleton
610 Repository: https://kevinushey.r-universe.dev
611 RemoteType: standard
612 RemoteSha: 1.1.0
613 ")
614
615 path <- renv_scope_tempfile()
616 writeLines(text, con = path)
617
618 record <- renv_snapshot_description(path = path)
619 expect_null(record[["RemoteSha"]])
620
621 })
622
623 test_that("a package's hash can be re-generated from lockfile", {
624
625 project <- renv_tests_scope("breakfast")
626 init()
627
628 lockfile <- snapshot(lockfile = NULL)
629 records <- renv_lockfile_records(lockfile)
630
631 enumerate(records, function(package, record) {
632 path <- system.file("DESCRIPTION", package = package)
633 actual <- renv_hash_description(path)
634 expected <- renv_hash_record(record)
635 expect_equal(actual, expected)
636 })
637
638 })
639
640 test_that("lockfiles are stable (v1)", {
641
642 renv_scope_options(renv.lockfile.version = 1L)
643 renv_scope_envvars(RENV_PATHS_ROOT = tempfile())
644
645 project <- renv_tests_scope("breakfast")
646 init()
647
648 expect_snapshot(. <- writeLines(readLines("renv.lock")))
649
650 })
651
652 test_that("lockfiles are stable (v2)", {
653
654 renv_scope_options(renv.lockfile.version = 2L)
655 renv_scope_envvars(RENV_PATHS_ROOT = tempfile())
656
657 project <- renv_tests_scope("breakfast")
658 init()
659
660 expect_snapshot(. <- writeLines(readLines("renv.lock")))
661
662 })
663
664 # # https://github.com/rstudio/renv/issues/2073
665 test_that("empty .ipynb files are handled gracefully", {
666
667 skip_on_cran()
668 project <- renv_tests_scope("bread")
669 init()
670
671 writeLines("", con = "example.ipynb")
672 snapshot()
673
674 })
675
676 test_that("invalid lockfiles don't prevent calls to snapshot", {
677
678 skip_on_cran()
679 project <- renv_tests_scope("bread")
680 init()
681
682 writeLines("whoops!", con = "renv.lock")
683 suppressWarnings(snapshot())
684 expect_true(TRUE)
685
686 })
687
688 test_that("package records with erroneous CRAN RemoteRepos are handled", {
689
690 skip_on_cran()
691
692 contents <- heredoc('
693 Package: example
694 Version: 1.0.0
695 RemoteType: standard
696 RemotePkgRef: example
697 RemoteRef: example
698 RemoteRepos: https://packagemanager.posit.co/cran/2025-03-03
699 RemoteReposName: CRAN
700 RemotePkgPlatform: aarch64-apple-darwin20
701 RemoteSha: 1.0.0
702 ')
703
704 descfile <- renv_scope_tempfile("renv-description-")
705 writeLines(contents, con = descfile)
706 record <- renv_snapshot_description(descfile)
707
708 expect_equal(record$Source, "Repository")
709 expect_equal(record$Repository, "https://packagemanager.posit.co/cran/2025-03-03")
710
711 })
712
713 test_that("snapshot.dev setting works and snapshot() uses it as default", {
714
715 renv_tests_scope()
716 init()
717
718 # Create a project that would have dev dependencies
719 writeLines("PackageUseDevtools: Yes", con = "project.Rproj")
720
721 project <- getwd()
722
723 # Verify default setting is FALSE
724 expect_false(settings$snapshot.dev())
725
726 # Test that the setting can be set and retrieved
727 settings$snapshot.dev(TRUE)
728 expect_true(settings$snapshot.dev())
729
730 settings$snapshot.dev(FALSE)
731 expect_false(settings$snapshot.dev())
732
733 # Test that dependencies discovered with dev=FALSE don't include devtools
734 deps1 <- renv_snapshot_dependencies(project, dev = FALSE)
735 expect_false("devtools" %in% deps1)
736
737 # Test that dependencies discovered with dev=TRUE do include devtools
738 deps2 <- renv_snapshot_dependencies(project, dev = TRUE)
739 expect_true("devtools" %in% deps2)
740
741 })