Debian Codegraph @

tests/testthat/test-graph.R

raw
1
2 test_that("renv_graph_init resolves a package with no dependencies", {
3
4 renv_tests_scope()
5
6 descriptions <- renv_graph_init("bread")
7 expect_true("bread" %in% names(descriptions))
8 expect_equal(descriptions[["bread"]]$Package, "bread")
9 expect_equal(length(descriptions), 1L)
10
11 })
12
13 test_that("renv_graph_init resolves transitive dependencies", {
14
15 renv_tests_scope()
16
17 descriptions <- renv_graph_init("breakfast")
18 packages <- names(descriptions)
19
20 # breakfast depends on oatmeal and toast; toast depends on bread
21 expect_true("breakfast" %in% packages)
22 expect_true("oatmeal" %in% packages)
23 expect_true("toast" %in% packages)
24 expect_true("bread" %in% packages)
25
26 # suggests (egg) should not be included
27 expect_false("egg" %in% packages)
28
29 })
30
31 test_that("renv_graph_sort produces a valid topological order", {
32
33 renv_tests_scope()
34
35 descriptions <- renv_graph_init("breakfast")
36 sorted <- renv_graph_sort(descriptions)
37 order <- names(sorted)
38
39 # leaves must come before their dependents
40 expect_true(which(order == "bread") < which(order == "toast"))
41 expect_true(which(order == "oatmeal") < which(order == "breakfast"))
42 expect_true(which(order == "toast") < which(order == "breakfast"))
43
44 })
45
46 test_that("renv_graph_sort handles packages with no dependencies", {
47
48 renv_tests_scope()
49
50 descriptions <- renv_graph_init("bread")
51 sorted <- renv_graph_sort(descriptions)
52 expect_equal(names(sorted), "bread")
53
54 })
55
56 test_that("renv_graph_init handles multiple roots", {
57
58 renv_tests_scope()
59
60 descriptions <- renv_graph_init(c("bread", "egg"))
61
62 expect_equal(sort(names(descriptions)), c("bread", "egg"))
63
64 })
65
66 test_that("renv_graph_init deduplicates shared dependencies", {
67
68 renv_tests_scope()
69
70 # breakfast and brunch both depend on oatmeal and toast
71 descriptions <- renv_graph_init(c("breakfast", "brunch"))
72 packages <- names(descriptions)
73
74 # shared deps should appear only once (no duplicates by construction)
75 expect_equal(length(packages), length(unique(packages)))
76
77 # both roots and shared deps should be present
78 expect_true("breakfast" %in% packages)
79 expect_true("brunch" %in% packages)
80 expect_true("oatmeal" %in% packages)
81 expect_true("toast" %in% packages)
82 expect_true("bread" %in% packages)
83
84 })
85
86 test_that("renv_graph_waves computes correct wave structure", {
87
88 renv_tests_scope()
89
90 descriptions <- renv_graph_init("breakfast")
91 waves <- renv_graph_waves(descriptions)
92
93 # should have multiple waves
94 expect_true(length(waves) >= 2L)
95
96 # all packages should appear exactly once across waves
97 all_pkgs <- unlist(waves)
98 expect_equal(sort(all_pkgs), sort(names(descriptions)))
99 expect_equal(length(all_pkgs), length(unique(all_pkgs)))
100
101 # leaves (bread, oatmeal) must be in an earlier wave than their dependents
102 wave_of <- function(pkg) {
103 for (i in seq_along(waves))
104 if (pkg %in% waves[[i]])
105 return(i)
106 }
107
108 expect_true(wave_of("bread") < wave_of("toast"))
109 expect_true(wave_of("toast") < wave_of("breakfast"))
110 expect_true(wave_of("oatmeal") < wave_of("breakfast"))
111
112 })
113
114 test_that("renv_graph_waves returns single wave for leaf package", {
115
116 renv_tests_scope()
117
118 descriptions <- renv_graph_init("bread")
119 waves <- renv_graph_waves(descriptions)
120
121 expect_equal(length(waves), 1L)
122 expect_equal(waves[[1L]], "bread")
123
124 })
125
126 test_that("renv_graph_waves groups independent packages in same wave", {
127
128 renv_tests_scope()
129
130 # bread and egg have no dependencies on each other
131 descriptions <- renv_graph_init(c("bread", "egg"))
132 waves <- renv_graph_waves(descriptions)
133
134 # both should be in wave 1 (no deps within the set)
135 expect_equal(length(waves), 1L)
136 expect_equal(sort(waves[[1L]]), sort(c("bread", "egg")))
137
138 })
139
140 test_that("renv_graph_install installs packages end to end", {
141
142 renv_tests_scope()
143
144 descriptions <- renv_graph_init("breakfast")
145 records <- renv_graph_install(descriptions)
146
147 # all packages should be installed
148 expect_true("breakfast" %in% names(records))
149 expect_true("oatmeal" %in% names(records))
150 expect_true("toast" %in% names(records))
151 expect_true("bread" %in% names(records))
152
153 # packages should actually be loadable
154 library <- renv_libpaths_active()
155 for (pkg in names(records))
156 expect_true(renv_package_installed(pkg, lib.loc = library), info = pkg)
157
158 })
159
160 test_that("renv_graph_install installs a single leaf package", {
161
162 renv_tests_scope()
163
164 descriptions <- renv_graph_init("bread")
165 records <- renv_graph_install(descriptions)
166
167 expect_equal(names(records), "bread")
168 expect_true(renv_package_installed("bread"))
169
170 })
171
172 test_that("renv_graph_urls resolves repository package URLs", {
173
174 renv_tests_scope()
175
176 descriptions <- renv_graph_init("bread")
177 urls <- renv_graph_urls(descriptions)
178
179 expect_true("bread" %in% names(urls))
180
181 info <- urls[["bread"]]
182 expect_true(is.list(info))
183 expect_true(nzchar(info$url))
184 expect_true(nzchar(info$destfile))
185 expect_true(grepl("bread", info$url))
186
187 })
188
189 test_that("renv_graph_urls returns NULL for unsupported sources", {
190
191 desc <- list(Package = "fakepkg", Version = "1.0", Source = "bitbucket")
192 descriptions <- list(fakepkg = desc)
193
194 urls <- renv_graph_urls(descriptions)
195 expect_null(urls[["fakepkg"]])
196
197 })
198
199 test_that("renv_graph_url_github constructs correct URL", {
200
201 desc <- list(
202 Package = "mypkg",
203 Version = "1.0.0",
204 Source = "GitHub",
205 RemoteUsername = "owner",
206 RemoteRepo = "repo",
207 RemoteSha = "abc123"
208 )
209
210 info <- renv_graph_url_github(desc)
211 expect_true(is.list(info))
212 expect_true(grepl("owner/repo/tarball/abc123", info$url))
213 expect_equal(info$type, "github")
214
215 })
216
217 test_that("renv_graph_url_github returns NULL when fields are missing", {
218
219 desc <- list(Package = "mypkg", Version = "1.0.0", Source = "GitHub")
220 expect_null(renv_graph_url_github(desc))
221
222 })
223
224 test_that("renv_graph_url_gitlab constructs correct URL", {
225
226 desc <- list(
227 Package = "mypkg",
228 Version = "1.0.0",
229 Source = "GitLab",
230 RemoteUsername = "user",
231 RemoteRepo = "repo",
232 RemoteSha = "def456"
233 )
234
235 info <- renv_graph_url_gitlab(desc)
236 expect_true(is.list(info))
237 expect_true(grepl("projects/user%2Frepo/repository/archive", info$url))
238 expect_true(grepl("sha=def456", info$url))
239 expect_equal(info$type, "gitlab")
240
241 })
242
243 test_that("renv_graph_url_url resolves RemoteUrl", {
244
245 desc <- list(
246 Package = "mypkg",
247 Version = "1.0.0",
248 Source = "url",
249 RemoteUrl = "https://example.com/mypkg_1.0.0.tar.gz"
250 )
251
252 info <- renv_graph_url_url(desc)
253 expect_true(is.list(info))
254 expect_equal(info$url, "https://example.com/mypkg_1.0.0.tar.gz")
255 expect_equal(info$type, "url")
256 expect_true(nzchar(info$destfile))
257
258 })
259
260 test_that("renv_graph_url_local converts file path to file URI", {
261
262 src <- renv_scope_tempfile("renv-local-pkg-", fileext = ".tar.gz")
263 file.create(src)
264
265 desc <- list(
266 Package = "mypkg",
267 Version = "1.0.0",
268 Source = "local",
269 Path = src
270 )
271
272 info <- renv_graph_url_local(desc)
273 expect_true(is.list(info))
274 expect_true(startsWith(info$url, "file://"))
275 expect_equal(info$type, "local")
276
277 })
278
279 test_that("renv_graph_url_local returns NULL when path doesn't exist", {
280
281 desc <- list(
282 Package = "mypkg",
283 Version = "1.0.0",
284 Source = "local",
285 Path = "/nonexistent/path/mypkg_1.0.0.tar.gz"
286 )
287
288 expect_null(renv_graph_url_local(desc))
289
290 })
291
292 test_that("renv_graph_url_repository resolves from test repo", {
293
294 renv_tests_scope()
295
296 desc <- list(Package = "bread", Version = "1.0.0", Source = "Repository")
297 info <- renv_graph_url_repository(desc)
298
299 expect_true(is.list(info))
300 expect_true(grepl("bread", info$url))
301 expect_equal(info$type, "repository")
302
303 })
304
305 # adjacency graph ----
306
307 test_that("renv_graph_adjacency computes correct structure for chain", {
308
309 renv_tests_scope()
310
311 descriptions <- renv_graph_init("breakfast")
312 g <- renv_graph_adjacency(descriptions)
313
314 # breakfast -> oatmeal, toast; toast -> bread; bread, oatmeal -> nothing
315 expect_true(setequal(g$packages, c("breakfast", "oatmeal", "toast", "bread")))
316 expect_true("toast" %in% g$adj[["breakfast"]])
317 expect_true("oatmeal" %in% g$adj[["breakfast"]])
318 expect_equal(g$adj[["bread"]], character())
319 expect_equal(g$adj[["oatmeal"]], character())
320 expect_equal(g$adj[["toast"]], "bread")
321
322 # in-degree: bread=0, oatmeal=0, toast=1 (from breakfast), breakfast=0 wait no
323 # in-degree counts deps *within the set* for each package
324 expect_equal(g$indegree[["bread"]], 0L)
325 expect_equal(g$indegree[["oatmeal"]], 0L)
326 expect_equal(g$indegree[["toast"]], 1L) # depends on bread
327 expect_equal(g$indegree[["breakfast"]], 2L) # depends on oatmeal + toast
328
329 # reverse adjacency: bread is depended on by toast; toast by breakfast
330 expect_true("toast" %in% g$revadj[["bread"]])
331 expect_true("breakfast" %in% g$revadj[["toast"]])
332 expect_true("breakfast" %in% g$revadj[["oatmeal"]])
333
334 })
335
336 test_that("renv_graph_adjacency handles independent packages", {
337
338 renv_tests_scope()
339
340 descriptions <- renv_graph_init(c("bread", "egg"))
341 g <- renv_graph_adjacency(descriptions)
342
343 # no edges between independent packages
344 expect_equal(g$adj[["bread"]], character())
345 expect_equal(g$adj[["egg"]], character())
346 expect_equal(g$indegree[["bread"]], 0L)
347 expect_equal(g$indegree[["egg"]], 0L)
348
349 })
350
351 test_that("renv_graph_adjacency handles empty input", {
352
353 g <- renv_graph_adjacency(list())
354 expect_equal(length(g$packages), 0L)
355 expect_equal(length(g$adj), 0L)
356
357 })
358
359 # version requirements ----
360
361 test_that("renv_graph_requirements extracts version constraints", {
362
363 renv_tests_scope()
364
365 # breakfast depends on toast (>= 1.0.0)
366 descriptions <- renv_graph_init("breakfast")
367 requirements <- renv_graph_requirements(descriptions)
368
369 # toast should have a requirement from breakfast
370 reqs <- requirements[["toast"]]
371 expect_true(is.data.frame(reqs))
372 expect_true(nrow(reqs) >= 1L)
373 expect_true("breakfast" %in% reqs$RequiredBy)
374
375 })
376
377 test_that("renv_graph_compatible accepts satisfied constraints", {
378
379 reqs <- data.frame(
380 Package = "toast",
381 Require = ">=",
382 Version = "1.0.0",
383 RequiredBy = "breakfast",
384 stringsAsFactors = FALSE
385 )
386
387 expect_true(renv_graph_compatible("1.0.0", reqs))
388 expect_true(renv_graph_compatible("2.0.0", reqs))
389
390 })
391
392 test_that("renv_graph_compatible rejects unsatisfied constraints", {
393
394 reqs <- data.frame(
395 Package = "toast",
396 Require = ">=",
397 Version = "2.0.0",
398 RequiredBy = "breakfast",
399 stringsAsFactors = FALSE
400 )
401
402 expect_false(renv_graph_compatible("1.0.0", reqs))
403 expect_false(renv_graph_compatible("1.9.9", reqs))
404
405 })
406
407 test_that("renv_graph_compatible handles multiple constraints", {
408
409 reqs <- data.frame(
410 Package = c("toast", "toast"),
411 Require = c(">=", ">="),
412 Version = c("1.0.0", "1.5.0"),
413 RequiredBy = c("pkg1", "pkg2"),
414 stringsAsFactors = FALSE
415 )
416
417 expect_true(renv_graph_compatible("1.5.0", reqs))
418 expect_true(renv_graph_compatible("2.0.0", reqs))
419 expect_false(renv_graph_compatible("1.2.0", reqs))
420
421 })
422
423 test_that("renv_graph_compatible returns TRUE for no requirements", {
424
425 expect_true(renv_graph_compatible("1.0.0", NULL))
426 expect_true(renv_graph_compatible("1.0.0", data.frame()))
427
428 })
429
430 # needs update ----
431
432 test_that("renv_graph_needs_update preserves installed transitive deps", {
433
434 renv_tests_scope()
435
436 # install bread 1.0.0 into the test library
437 descriptions <- renv_graph_init("bread")
438 renv_graph_install(descriptions)
439 expect_true(renv_package_installed("bread"))
440
441 # set up restore state as though "breakfast" was explicitly requested;
442 # bread is a transitive dependency, not explicitly requested
443 renv_scope_restore(
444 project = getwd(),
445 library = renv_libpaths_active(),
446 packages = "breakfast"
447 )
448
449 # simulate graph resolution having resolved bread to a newer version
450 record <- list(Package = "bread", Version = "2.0.0", Source = "Repository")
451
452 # requirements that bread 1.0.0 satisfies
453 requirements <- new.env(parent = emptyenv())
454 requirements[["bread"]] <- data.frame(
455 Package = "bread",
456 Require = ">=",
457 Version = "1.0.0",
458 RequiredBy = "toast",
459 stringsAsFactors = FALSE
460 )
461
462 # installed bread 1.0.0 satisfies >= 1.0.0, so no update needed
463 expect_false(renv_graph_needs_update("bread", record, requirements))
464
465 })
466
467 test_that("renv_graph_needs_update upgrades when requirements not satisfied", {
468
469 renv_tests_scope()
470
471 # install bread 1.0.0
472 descriptions <- renv_graph_init("bread")
473 renv_graph_install(descriptions)
474
475 renv_scope_restore(
476 project = getwd(),
477 library = renv_libpaths_active(),
478 packages = "breakfast"
479 )
480
481 record <- list(Package = "bread", Version = "2.0.0", Source = "Repository")
482
483 # requirements that bread 1.0.0 does NOT satisfy
484 requirements <- new.env(parent = emptyenv())
485 requirements[["bread"]] <- data.frame(
486 Package = "bread",
487 Require = ">=",
488 Version = "2.0.0",
489 RequiredBy = "toast",
490 stringsAsFactors = FALSE
491 )
492
493 # installed bread 1.0.0 doesn't satisfy >= 2.0.0, so update is needed
494 expect_true(renv_graph_needs_update("bread", record, requirements))
495
496 })
497
498 test_that("renv_graph_needs_update always updates explicitly-requested packages", {
499
500 renv_tests_scope()
501
502 # install bread 1.0.0
503 descriptions <- renv_graph_init("bread")
504 renv_graph_install(descriptions)
505
506 # bread is explicitly requested this time
507 renv_scope_restore(
508 project = getwd(),
509 library = renv_libpaths_active(),
510 packages = "bread"
511 )
512
513 record <- list(Package = "bread", Version = "2.0.0", Source = "Repository")
514
515 # requirements that bread 1.0.0 satisfies
516 requirements <- new.env(parent = emptyenv())
517 requirements[["bread"]] <- data.frame(
518 Package = "bread",
519 Require = ">=",
520 Version = "1.0.0",
521 RequiredBy = "toast",
522 stringsAsFactors = FALSE
523 )
524
525 # even though 1.0.0 satisfies requirements, bread was explicitly
526 # requested so it should be updated
527 expect_true(renv_graph_needs_update("bread", record, requirements))
528
529 })
530
531 test_that("renv_graph_needs_update returns TRUE when package not installed", {
532
533 renv_tests_scope()
534
535 renv_scope_restore(
536 project = getwd(),
537 library = renv_libpaths_active(),
538 packages = "breakfast"
539 )
540
541 record <- list(Package = "bread", Version = "1.0.0", Source = "Repository")
542 requirements <- new.env(parent = emptyenv())
543
544 # bread is not installed, so it needs to be installed
545 expect_true(renv_graph_needs_update("bread", record, requirements))
546
547 })
548
549 # install result parsing ----
550
551 test_that("renv_graph_install_parse_result handles NULL data", {
552
553 elapsed <- as.difftime(1, units = "secs")
554 result <- renv_graph_install_parse_result(NULL, elapsed)
555
556 expect_false(result$success)
557 expect_true(grepl("unexpectedly", result$output))
558 expect_equal(result$elapsed, elapsed)
559
560 })
561
562 test_that("renv_graph_install_parse_result handles error object", {
563
564 elapsed <- as.difftime(2, units = "secs")
565 err <- simpleError("something went wrong")
566 result <- renv_graph_install_parse_result(err, elapsed)
567
568 expect_false(result$success)
569 expect_equal(result$output, "something went wrong")
570 expect_equal(result$elapsed, elapsed)
571
572 })
573
574 test_that("renv_graph_install_parse_result handles successful output", {
575
576 elapsed <- as.difftime(3, units = "secs")
577 output <- c("* installing *source* package 'bread' ...", "* DONE (bread)")
578 attr(output, "status") <- 0L
579 result <- renv_graph_install_parse_result(output, elapsed)
580
581 expect_true(result$success)
582 expect_equal(result$output, output)
583
584 })
585
586 test_that("renv_graph_install_parse_result handles failed output", {
587
588 elapsed <- as.difftime(4, units = "secs")
589 output <- c("ERROR: compilation failed")
590 attr(output, "status") <- 1L
591 result <- renv_graph_install_parse_result(output, elapsed)
592
593 expect_false(result$success)
594 expect_equal(result$output, output)
595
596 })
597
598 test_that("renv_graph_install_parse_result handles output with no status attr", {
599
600 elapsed <- as.difftime(1, units = "secs")
601 output <- c("some output")
602 result <- renv_graph_install_parse_result(output, elapsed)
603
604 # no status attribute means success (status 0)
605 expect_true(result$success)
606
607 })
608
609 # install classification ----
610
611 test_that("renv_graph_install_classify inspects archive contents", {
612
613 # the classifier must not trust the record's "type" attribute;
614 # for PPM "binary" repositories, the attribute reflects the
615 # requested type, not the real contents of the archive.
616 path <- renv_tests_path("local/skeleton/skeleton_1.0.1.tar.gz")
617
618 record <- list(Package = "skeleton", Path = path)
619 expect_equal(renv_graph_install_classify(record), "source")
620
621 # a misleading "binary" tag must not override the real type
622 attr(record, "type") <- "binary"
623 expect_equal(renv_graph_install_classify(record), "source")
624
625 })
626
627 # install needs unpack ----
628
629 test_that("renv_graph_install_needs_unpack returns TRUE for RemoteSubdir", {
630
631 record <- list(
632 Package = "mypkg",
633 Path = "/tmp/mypkg.tar.gz",
634 RemoteSubdir = "subdir"
635 )
636 expect_true(renv_graph_install_needs_unpack(record, "source"))
637
638 })
639
640 test_that("renv_graph_install_needs_unpack returns FALSE for simple tar.gz source", {
641
642 archive <- renv_scope_tempfile("renv-test-", fileext = ".tar.gz")
643 file.create(archive)
644
645 record <- list(Package = "mypkg", Path = archive)
646 renv_scope_options(renv.config.install.build = FALSE)
647 expect_false(renv_graph_install_needs_unpack(record, "source"))
648
649 })
650
651 # install error reporting ----
652
653 test_that("renv_graph_install_errors reports direct failures", {
654
655 renv_scope_options(renv.verbose = TRUE, renv.caution.verbose = TRUE)
656
657 descriptions <- list(
658 bread = list(Package = "bread", Version = "1.0.0")
659 )
660
661 errors <- list(
662 list(package = "bread", message = "compilation failed")
663 )
664
665 output <- capture.output(
666 renv_graph_install_errors(errors, "bread", descriptions)
667 )
668
669 output <- paste(output, collapse = "\n")
670 expect_true(grepl("bread", output))
671 expect_true(grepl("compilation failed", output))
672
673 })
674
675 test_that("renv_graph_install_errors reports dependency cascade failures", {
676
677 renv_scope_options(renv.verbose = TRUE, renv.caution.verbose = TRUE)
678
679 descriptions <- list(
680 bread = list(Package = "bread", Version = "1.0.0"),
681 toast = list(Package = "toast", Version = "1.0.0", Depends = "bread")
682 )
683
684 errors <- list(
685 list(package = "bread", message = "compilation failed")
686 )
687 failed <- c("bread", "toast")
688
689 output <- capture.output(
690 renv_graph_install_errors(errors, failed, descriptions)
691 )
692
693 output <- paste(output, collapse = "\n")
694 expect_true(grepl("bread", output))
695 expect_true(grepl("toast", output))
696 expect_true(grepl("dependency failed", output))
697
698 })
699
700 test_that("renv_graph_install_errors is silent with no errors", {
701
702 output <- capture.output(
703 renv_graph_install_errors(list(), character(), list())
704 )
705 expect_equal(length(output), 0L)
706
707 })
708
709 # wave cycle detection ----
710
711 test_that("renv_graph_waves warns on dependency cycle", {
712
713 # synthetic descriptions that form a cycle: A -> B -> A
714 descriptions <- list(
715 A = list(Package = "A", Version = "1.0.0", Depends = "B"),
716 B = list(Package = "B", Version = "1.0.0", Depends = "A")
717 )
718
719 expect_warning(
720 waves <- renv_graph_waves(descriptions),
721 "dependency cycle"
722 )
723
724 # all packages should still appear
725 all_pkgs <- unlist(waves)
726 expect_true(setequal(all_pkgs, c("A", "B")))
727
728 })
729
730 test_that("renv_graph_waves handles empty input", {
731
732 waves <- renv_graph_waves(list())
733 expect_equal(waves, list())
734
735 })
736
737 # install pipeline integration tests ----
738
739 test_that("renv_graph_install installs multiple independent packages", {
740
741 renv_tests_scope()
742
743 # bread and egg have no dependency relationship
744 descriptions <- renv_graph_init(c("bread", "egg"))
745 records <- renv_graph_install(descriptions)
746
747 expect_true(setequal(names(records), c("bread", "egg")))
748 expect_true(renv_package_installed("bread"))
749 expect_true(renv_package_installed("egg"))
750
751 })
752
753 test_that("renv_graph_install skips already-installed packages", {
754
755 renv_tests_scope()
756
757 # install bread first
758 descriptions <- renv_graph_init("bread")
759 renv_graph_install(descriptions)
760 expect_true(renv_package_installed("bread"))
761
762 # now install breakfast; bread should be skipped
763 descriptions <- renv_graph_init("breakfast")
764 records <- renv_graph_install(descriptions)
765
766 # breakfast and its other deps should be installed
767 expect_true(renv_package_installed("breakfast"))
768 expect_true(renv_package_installed("toast"))
769 expect_true(renv_package_installed("oatmeal"))
770
771 })
772
773 test_that("renv_graph_install returns empty list for empty input", {
774
775 renv_tests_scope()
776
777 records <- renv_graph_install(list())
778 expect_equal(records, list())
779
780 })
781
782 test_that("renv_graph_install handles deeper dependency chain", {
783
784 renv_tests_scope()
785
786 # jamie -> kevin + phone; kevin -> phone
787 # three levels: phone -> kevin -> jamie
788 descriptions <- renv_graph_init("jamie")
789 records <- renv_graph_install(descriptions)
790
791 expect_true("jamie" %in% names(records))
792 expect_true("kevin" %in% names(records))
793 expect_true("phone" %in% names(records))
794
795 for (pkg in names(records))
796 expect_true(renv_package_installed(pkg), info = pkg)
797
798 })
799
800 test_that("renv_graph_install with install.jobs = 1 uses sequential mode", {
801
802 renv_tests_scope()
803 renv_scope_options(renv.config.install.jobs = 1L)
804
805 descriptions <- renv_graph_init("breakfast")
806 records <- renv_graph_install(descriptions)
807
808 expect_true(setequal(
809 names(records),
810 c("bread", "oatmeal", "toast", "breakfast")
811 ))
812
813 for (pkg in names(records))
814 expect_true(renv_package_installed(pkg), info = pkg)
815
816 })
817
818 test_that("renv_graph_install with staged install", {
819
820 renv_tests_scope()
821
822 renv_scope_options(
823 renv.config.install.staged = TRUE,
824 renv.config.install.transactional = FALSE
825 )
826
827 descriptions <- renv_graph_init("breakfast")
828 records <- renv_graph_install(descriptions)
829
830 # all packages should be in the real library after staging
831 library <- renv_libpaths_active()
832 for (pkg in names(records))
833 expect_true(renv_package_installed(pkg, lib.loc = library), info = pkg)
834
835 })
836
837 test_that("renv_graph_install respects dependency ordering", {
838
839 renv_tests_scope(isolated = TRUE)
840
841 # disable cache so all packages go through source install
842 renv_scope_options(renv.config.cache.enabled = FALSE)
843
844 # track the order packages are finalized via a tracer;
845 # use a shared environment so the tracer (evaluated inside the
846 # renv namespace) can write to it
847 env <- new.env(parent = emptyenv())
848 env$order <- character()
849
850 renv_scope_trace(
851 what = renv:::renv_graph_install_finalize,
852 tracer = bquote({
853 .env <- .(env)
854 .env$order <- c(.env$order, record$Package)
855 })
856 )
857
858 descriptions <- renv_graph_init("breakfast")
859 records <- renv_graph_install(descriptions)
860
861 # bread must be finalized before toast, toast before breakfast
862 bread_idx <- match("bread", env$order)
863 toast_idx <- match("toast", env$order)
864 breakfast_idx <- match("breakfast", env$order)
865
866 expect_true(!is.na(bread_idx))
867 expect_true(!is.na(toast_idx))
868 expect_true(!is.na(breakfast_idx))
869 expect_true(bread_idx < toast_idx)
870 expect_true(toast_idx < breakfast_idx)
871
872 })
873
874 test_that("binary packages respect dependency ordering", {
875
876 skip_on_cran()
877 skip_if(identical(.Platform$pkgType, "source"),
878 "binary packages not supported on this platform")
879
880 renv_tests_scope(isolated = TRUE)
881
882 # use binary packages from the test repository
883 renv_scope_options(pkgType = .Platform$pkgType)
884 renv_scope_binding(the, "install_pkg_type", "binary")
885
886 # disable cache so all packages go through binary install
887 renv_scope_options(renv.config.cache.enabled = FALSE)
888
889 # track the order packages are finalized via a tracer
890 env <- new.env(parent = emptyenv())
891 env$order <- character()
892
893 renv_scope_trace(
894 what = renv:::renv_graph_install_finalize,
895 tracer = bquote({
896 .env <- .(env)
897 .env$order <- c(.env$order, record$Package)
898 })
899 )
900
901 descriptions <- renv_graph_init("breakfast")
902 records <- renv_graph_install(descriptions)
903
904 # all packages should be installed and loadable
905 for (pkg in names(records))
906 expect_true(renv_package_installed(pkg), info = pkg)
907
908 # bread must be finalized before toast, toast before breakfast
909 bread_idx <- match("bread", env$order)
910 toast_idx <- match("toast", env$order)
911 breakfast_idx <- match("breakfast", env$order)
912
913 expect_true(!is.na(bread_idx))
914 expect_true(!is.na(toast_idx))
915 expect_true(!is.na(breakfast_idx))
916 expect_true(bread_idx < toast_idx)
917 expect_true(toast_idx < breakfast_idx)
918
919 })
920
921 # graph sort edge cases ----
922
923 test_that("renv_graph_sort handles empty input", {
924
925 sorted <- renv_graph_sort(list())
926 expect_equal(length(sorted), 0L)
927
928 })
929
930 test_that("renv_graph_sort produces stable ordering for independent packages", {
931
932 renv_tests_scope()
933
934 descriptions <- renv_graph_init(c("bread", "egg", "oatmeal"))
935 sorted <- renv_graph_sort(descriptions)
936
937 # all three should appear; order among independent packages
938 # is deterministic (alphabetical from the queue)
939 expect_equal(length(sorted), 3L)
940 expect_true(setequal(names(sorted), c("bread", "egg", "oatmeal")))
941
942 })
943
944 # graph deps ----
945
946 test_that("renv_graph_deps extracts dependencies from Depends/Imports/LinkingTo", {
947
948 desc <- list(
949 Package = "mypkg",
950 Depends = "R (>= 3.5), bread, oatmeal",
951 Imports = "toast",
952 LinkingTo = "egg"
953 )
954
955 deps <- renv_graph_deps(desc)
956
957 # R should be excluded (base package)
958 expect_false("R" %in% deps)
959 expect_true(setequal(deps, c("bread", "oatmeal", "toast", "egg")))
960
961 })
962
963 test_that("renv_graph_deps respects custom fields argument", {
964
965 desc <- list(
966 Package = "mypkg",
967 Depends = "bread",
968 Imports = "toast",
969 Suggests = "egg"
970 )
971
972 # default fields don't include Suggests
973 deps_default <- renv_graph_deps(desc)
974 expect_false("egg" %in% deps_default)
975
976 # custom fields can include Suggests
977 deps_custom <- renv_graph_deps(desc, fields = c("Depends", "Imports", "Suggests"))
978 expect_true("egg" %in% deps_custom)
979
980 })
981
982 test_that("renv_graph_deps handles list-valued dependency fields from v2 lockfiles", {
983
984 # v2 lockfile records store dependency fields as lists (JSON arrays)
985 # rather than comma-separated strings; renv_graph_deps should handle both
986 desc <- list(
987 Package = "mypkg",
988 Imports = list("Rcpp", "data.table", "jsonlite"),
989 LinkingTo = list("Rcpp", "BH")
990 )
991
992 deps <- renv_graph_deps(desc)
993 expect_true(setequal(deps, c("Rcpp", "data.table", "jsonlite", "BH")))
994
995 })
996
997 test_that("renv_graph_deps returns empty for package with no deps", {
998
999 desc <- list(Package = "mypkg", Version = "1.0.0")
1000 deps <- renv_graph_deps(desc)
1001 expect_equal(deps, character())
1002
1003 })
1004
1005 # graph URLs for multiple sources ----
1006
1007 test_that("renv_graph_urls resolves URLs for full dependency tree", {
1008
1009 renv_tests_scope()
1010
1011 descriptions <- renv_graph_init("breakfast")
1012 urls <- renv_graph_urls(descriptions)
1013
1014 # every package should have a resolved URL (they're all from the test repo)
1015 for (pkg in names(descriptions)) {
1016 info <- urls[[pkg]]
1017 expect_true(is.list(info), info = pkg)
1018 expect_true(nzchar(info$url), info = pkg)
1019 }
1020
1021 })
1022
1023 test_that("renv_graph_urls gracefully handles mixed sources", {
1024
1025 renv_tests_scope()
1026
1027 descriptions <- list(
1028 bread = list(Package = "bread", Version = "1.0.0", Source = "Repository"),
1029 fake = list(Package = "fake", Version = "1.0.0", Source = "unknown_source")
1030 )
1031
1032 urls <- renv_graph_urls(descriptions)
1033
1034 # bread should resolve; fake should be NULL
1035 expect_true(is.list(urls[["bread"]]))
1036 expect_null(urls[["fake"]])
1037
1038 })
1039
1040 # https://github.com/rstudio/renv/issues/2264
1041 test_that("renv_graph_description_repository respects install_pkg_type", {
1042
1043 renv_tests_scope()
1044
1045 # track the type argument passed to renv_available_packages_entry
1046 env <- new.env(parent = emptyenv())
1047 env$types <- character()
1048
1049 renv_scope_trace(
1050 what = renv:::renv_available_packages_entry,
1051 tracer = bquote({
1052 .env <- .(env)
1053 .env$types <- c(.env$types, type)
1054 })
1055 )
1056
1057 record <- list(Package = "bread", Source = "Repository")
1058
1059 # default: install_pkg_type is NULL, should fall back to "source"
1060 renv_graph_description_repository(record)
1061 expect_true("source" %in% env$types)
1062
1063 # when install_pkg_type is set, it should be forwarded
1064 env$types <- character()
1065 renv_scope_binding(the, "install_pkg_type", "binary")
1066 catch(renv_graph_description_repository(record))
1067 expect_true("binary" %in% env$types)
1068 expect_false("source" %in% env$types)
1069
1070 })
1071
1072 # https://github.com/rstudio/renv/issues/2278
1073 test_that("repository graph prefers crandb over latest-with-overridden-version", {
1074
1075 renv_tests_scope()
1076
1077 # stub crandb lookup so we don't touch the network, and so we can verify
1078 # which path was taken: the stub returns a marker Depends field that
1079 # differs from anything in the test repository
1080 crandb_called <- FALSE
1081 renv_scope_binding(
1082 envir = asNamespace("renv"),
1083 symbol = "renv_graph_description_crandb",
1084 replacement = function(package, version) {
1085 crandb_called <<- TRUE
1086 list(Package = package, Version = version, Depends = "crandb-marker")
1087 }
1088 )
1089
1090 # ask for bread at a version that isn't in the test repository (which has
1091 # 1.0.0); step 1 (version-filtered entry) and step 2 (latest matching) will
1092 # both fail, so resolution must fall through to crandb
1093 record <- list(Package = "bread", Version = "0.1.0", Source = "Repository")
1094 desc <- renv_graph_description_repository(record)
1095
1096 expect_true(crandb_called)
1097 expect_equal(desc$Version, "0.1.0")
1098 expect_equal(desc$Depends, "crandb-marker")
1099
1100 })
1101
1102 # https://github.com/rstudio/renv/issues/2249
1103 test_that("gitlab DESCRIPTION path handles empty RemoteSubdir", {
1104
1105 # when subdir is empty or NULL, the encoded path should be just "DESCRIPTION"
1106 # (not "%2FDESCRIPTION" which results from pasting "" with "DESCRIPTION")
1107 for (subdir in list(NULL, "")) {
1108 parts <- c(if (nzchar(subdir %||% "")) subdir, "DESCRIPTION")
1109 descpath <- URLencode(paste(parts, collapse = "/"), reserved = TRUE)
1110 expect_equal(descpath, "DESCRIPTION")
1111 }
1112
1113 # when subdir is non-empty, it should appear in the path
1114 parts <- c(if (nzchar("src" %||% "")) "src", "DESCRIPTION")
1115 descpath <- URLencode(paste(parts, collapse = "/"), reserved = TRUE)
1116 expect_equal(descpath, "src%2FDESCRIPTION")
1117
1118 })