Debian Codegraph @

tests/testthat/test-pak.R

raw
1
2 test_that("renv::install() works in projects using pak", {
3
4 skip_on_cran()
5 skip_on_windows()
6 skip_if_not_installed("pak")
7 pak <- renv_namespace_load("pak")
8 renv_scope_options(renv.config.pak.enabled = TRUE)
9 project <- renv_tests_scope()
10
11 # try installing an older version of 'breakfast'
12 quietly(install("[email protected]"))
13 expect_true(renv_package_installed("breakfast"))
14 expect_equal(renv_package_version("breakfast"), "0.1.0")
15
16 # try using 'install()' to update all installed packages
17 quietly(install())
18 expect_true(renv_package_installed("breakfast"))
19 expect_equal(renv_package_version("breakfast"), "1.0.0")
20
21 })
22
23 test_that("install(include = ...) installs requested packages with pak (#2281)", {
24
25 skip_on_cran()
26 skip_on_windows()
27 skip_if_not_installed("pak")
28 pak <- renv_namespace_load("pak")
29 renv_scope_options(renv.config.pak.enabled = TRUE)
30 project <- renv_tests_scope()
31
32 # install(include = ...) with no positional packages should install the
33 # named packages rather than fall through to renv_pak_update().
34 quietly(install(include = "breakfast"))
35 expect_true(renv_package_installed("breakfast"))
36
37 })
38
39 test_that("install(include = ...) filters positional packages with pak (#2281)", {
40
41 skip_on_cran()
42 skip_on_windows()
43 skip_if_not_installed("pak")
44 pak <- renv_namespace_load("pak")
45 renv_scope_options(renv.config.pak.enabled = TRUE)
46 project <- renv_tests_scope()
47
48 # 'bread' and 'oatmeal' are leaf packages with no shared dependencies;
49 # include should filter the positional list down to just 'bread', matching
50 # the behavior of the non-pak install path
51 quietly(install(c("bread", "oatmeal"), include = "bread"))
52 expect_true(renv_package_installed("bread"))
53 expect_false(renv_package_installed("oatmeal"))
54
55 })
56
57 test_that("install(exclude = ...) filters positional packages with pak (#2281)", {
58
59 skip_on_cran()
60 skip_on_windows()
61 skip_if_not_installed("pak")
62 pak <- renv_namespace_load("pak")
63 renv_scope_options(renv.config.pak.enabled = TRUE)
64 project <- renv_tests_scope()
65
66 # exclude should drop the named packages from the positional list,
67 # matching the behavior of the non-pak install path
68 quietly(install(c("bread", "oatmeal"), exclude = "oatmeal"))
69 expect_true(renv_package_installed("bread"))
70 expect_false(renv_package_installed("oatmeal"))
71
72 })
73
74 test_that("install() with an explicit scope filtered to empty is a no-op with pak (#2281)", {
75
76 skip_on_cran()
77 skip_on_windows()
78 skip_if_not_installed("pak")
79 pak <- renv_namespace_load("pak")
80 renv_scope_options(
81 renv.config.pak.enabled = TRUE,
82 renv.verbose = TRUE
83 )
84 project <- renv_tests_scope()
85
86 # when the caller provides an explicit scope and filtering empties it,
87 # we should report "no packages" and return — not fall through to the
88 # project-wide pak update path
89 output <- capture.output(install("bread", exclude = "bread"))
90 expect_match(paste(output, collapse = "\n"), "no packages to install", fixed = TRUE)
91 expect_false(renv_package_installed("bread"))
92
93 })
94
95 test_that("renv::update() works in projects using pak", {
96
97 skip_on_cran()
98 skip_on_windows()
99 skip_if_not_installed("pak")
100 pak <- renv_namespace_load("pak")
101 renv_scope_options(renv.config.pak.enabled = TRUE)
102 project <- renv_tests_scope()
103
104 # try installing an older version of 'breakfast'
105 quietly(install("[email protected]"))
106 expect_true(renv_package_installed("breakfast"))
107 expect_equal(renv_package_version("breakfast"), "0.1.0")
108
109 # try using 'update()' to update all installed packages
110 quietly(update())
111 expect_true(renv_package_installed("breakfast"))
112 expect_equal(renv_package_version("breakfast"), "1.0.0")
113
114 })
115
116 # https://github.com/rstudio/renv/issues/2282
117 test_that("renv::init() routes through pak when pak.enabled = TRUE", {
118
119 renv_tests_scope("bread")
120 renv_scope_options(renv.config.pak.enabled = TRUE)
121
122 pak_install_packages <- NULL
123 local_mocked_bindings(
124 renv_pak_init = function(...) invisible(NULL),
125 renv_pak_install = function(packages, ...) {
126 pak_install_packages <<- packages
127 invisible(NULL)
128 }
129 )
130
131 quietly(init())
132
133 expect_true("bread" %in% pak_install_packages)
134
135 })
136
137 test_that("hydrate() preserves its return contract when pak handles install", {
138
139 renv_tests_scope("bread")
140 renv_scope_options(renv.config.pak.enabled = TRUE)
141
142 local_mocked_bindings(
143 renv_pak_init = function(...) invisible(NULL),
144 # simulate a pak install: return a data.frame, as pak::pkg_install() does
145 renv_pak_install = function(...) data.frame(package = "bread")
146 )
147
148 result <- quietly(hydrate(prompt = FALSE, report = FALSE))
149
150 # 'missing' should be NULL on success, not the raw pak install result
151 expect_null(result$missing)
152
153 })
154
155 test_that("hydrate() propagates pak install errors", {
156
157 renv_tests_scope("bread")
158 renv_scope_options(renv.config.pak.enabled = TRUE)
159
160 local_mocked_bindings(
161 renv_pak_init = function(...) invisible(NULL),
162 renv_pak_install = function(...) stop("pak failed")
163 )
164
165 expect_error(quietly(hydrate(prompt = FALSE, report = FALSE)), "pak failed")
166
167 })
168
169 test_that("restore(clean = TRUE) removes unused packages with pak enabled", {
170
171 skip_on_cran()
172 skip_on_windows()
173 skip_if_not_installed("pak")
174 renv_scope_options(renv.config.pak.enabled = TRUE)
175 renv_tests_scope("oatmeal")
176 init()
177
178 renv_scope_options(renv.config.auto.snapshot = FALSE)
179 install("bread")
180 expect_true(renv_package_installed("bread"))
181
182 quietly(restore(clean = TRUE))
183 expect_false(renv_package_installed("bread"))
184 expect_true(renv_package_installed("oatmeal"))
185
186 })
187
188 test_that("restore(clean = TRUE) aborts when prompt is declined on pak path", {
189
190 skip_on_cran()
191 skip_on_windows()
192 skip_if_not_installed("pak")
193 renv_scope_options(renv.config.pak.enabled = TRUE)
194 renv_tests_scope("oatmeal")
195 init()
196
197 renv_scope_options(renv.config.auto.snapshot = FALSE)
198 install("bread")
199 expect_true(renv_package_installed("bread"))
200
201 local_mocked_bindings(proceed = function(...) FALSE)
202 expect_error(quietly(restore(clean = TRUE, prompt = TRUE)))
203 expect_true(renv_package_installed("bread"))
204 expect_true(renv_package_installed("oatmeal"))
205
206 })
207
208 test_that("restore(clean = TRUE) honors `exclude` on pak path", {
209
210 skip_on_cran()
211 skip_on_windows()
212 skip_if_not_installed("pak")
213 renv_scope_options(renv.config.pak.enabled = TRUE)
214 renv_tests_scope("oatmeal")
215 init()
216
217 renv_scope_options(renv.config.auto.snapshot = FALSE)
218 install("bread")
219 expect_true(renv_package_installed("bread"))
220
221 quietly(restore(clean = TRUE, exclude = "bread"))
222 expect_true(renv_package_installed("bread"))
223 expect_true(renv_package_installed("oatmeal"))
224
225 })
226
227 test_that("restore(clean = TRUE) honors `packages` on pak path", {
228
229 skip_on_cran()
230 skip_on_windows()
231 skip_if_not_installed("pak")
232 renv_scope_options(renv.config.pak.enabled = TRUE)
233 renv_tests_scope("oatmeal")
234 init()
235
236 renv_scope_options(renv.config.auto.snapshot = FALSE)
237 install("bread")
238 expect_true(renv_package_installed("bread"))
239
240 # cleanup is scoped to the named packages; bread is unused but not named,
241 # so it should survive.
242 quietly(restore(clean = TRUE, packages = "oatmeal"))
243 expect_true(renv_package_installed("bread"))
244 expect_true(renv_package_installed("oatmeal"))
245
246 })