Debian Codegraph @

tests/testthat/test-migrate.R

raw
1
2 skip_if_no_packrat <- function() {
3
4 skip_on_cran()
5 skip_on_windows()
6 skip_if_not_installed("packrat")
7
8 version <- packageVersion("packrat")
9 if (renv_version_length(version) != 3)
10 skip("cannot test with development version of Packrat")
11
12 packrat <- renv_available_packages_latest(package = "packrat", type = "source")
13 if (version != packrat$Version)
14 skip("packrat is not current")
15
16 TRUE
17
18 }
19
20 test_that("a sample Packrat project can be migrated", {
21 skip_if_no_packrat()
22
23 # use dummy caches for this test
24 cache <- renv_scope_tempfile("packrat-cache-")
25 root <- renv_scope_tempfile("renv-root-")
26 renv_scope_envvars(R_PACKRAT_CACHE_DIR = cache, RENV_PATHS_ROOT = root)
27
28 # set up
29 requireNamespace("packrat")
30 renv_tests_scope("breakfast")
31
32 # initialize packrat
33 quietly(
34 expect_warning(
35 packrat::init(enter = FALSE, options = list(use.cache = TRUE))
36 )
37 )
38
39 # try to migrate
40 migrate()
41
42 # packages we expect to find
43 expected <- c("bread", "breakfast", "oatmeal", "toast", "packrat")
44
45 # check the renv cache
46 cachelist <- renv_cache_list()
47 expect_setequal(expected, basename(cachelist))
48
49 # check the renv sources directory
50 cransources <- renv_paths_source("cran")
51 expect_setequal(expected, list.files(cransources))
52
53 # check the lockfile
54 lockfile <- renv_lockfile_read("renv.lock")
55 records <- renv_lockfile_records(lockfile)
56 expect_setequal(expected, names(records))
57
58 })
59
60 test_that("a Packrat project with no library can be migrated", {
61 skip_if_no_packrat()
62
63 # TODO: skip tests if non-CRAN packrat is installed
64
65 # use dummy caches for this test
66 renv_scope_envvars(
67 R_PACKRAT_CACHE_DIR = renv_scope_tempfile("packrat-cache-"),
68 RENV_PATHS_ROOT = renv_scope_tempfile("renv-cache-")
69 )
70
71 renv_tests_scope("breakfast")
72
73 # initialize packrat
74 quietly(
75 expect_warning(
76 packrat::init(enter = FALSE, options = list(use.cache = TRUE))
77 )
78 )
79
80 # remove library
81 unlink("packrat/lib", recursive = TRUE)
82
83 # try to migrate
84 migrate()
85
86 # packages we expect to find
87 expected <- c("bread", "breakfast", "oatmeal", "toast", "packrat")
88
89 # check the renv cache
90 cachelist <- renv_cache_list()
91 expect_setequal(expected, basename(cachelist))
92
93 # check the lockfile
94 lockfile <- renv_lockfile_read("renv.lock")
95 records <- renv_lockfile_records(lockfile)
96 expect_setequal(expected, names(records))
97
98 })