Debian Codegraph @

tests/testthat/test-init.R

raw
1
2 test_that("init() automatically installs referenced packages", {
3 skip_on_cran()
4
5 renv_tests_scope("bread")
6 init()
7 expect_true(renv_package_installed("bread"))
8
9 })
10
11 test_that("we can initialize a project using 'breakfast'", {
12 skip_on_cran()
13 skip_on_covr()
14 renv_tests_scope("breakfast")
15
16 init()
17 expect_true(renv_project_initialized(getwd()))
18
19 expected <- c("bread", "breakfast", "oatmeal", "toast")
20 lockfile <- snapshot(lockfile = NULL)
21
22 actual <- setdiff(names(renv_lockfile_records(lockfile)), "renv")
23 expect_setequal(actual, expected)
24
25 })
26
27 test_that("we can initialize a project using 'toast'", {
28
29 skip_on_covr()
30 renv_tests_scope("toast")
31
32 init()
33
34 expected <- c("bread", "toast")
35 lockfile <- snapshot(lockfile = NULL)
36
37 actual <- setdiff(names(renv_lockfile_records(lockfile)), "renv")
38 expect_setequal(actual, expected)
39
40 })
41
42 test_that("we cannot initialize a project using 'brunch'", {
43
44 renv_tests_scope("brunch")
45
46 # 'brunch' will fail to install
47 init()
48
49 expect_false(file.exists(renv_paths_library("brunch")))
50
51 })
52
53 # https://github.com/rstudio/renv/issues/2251
54 test_that("attempts to initialize a project with a missing package is okay", {
55
56 renv_tests_scope("missing")
57
58 # package 'missing' does not exist and so cannot be installed;
59 # this should warn but not crash with a vapply error
60 expect_snapshot(init())
61
62 expect_false(file.exists(renv_paths_library("missing")))
63
64 })
65
66 test_that("init(bare = TRUE) initializes a project without packages", {
67
68 renv_tests_scope("brunch")
69 init(bare = TRUE)
70 files <- list.files(renv_paths_library())
71 expect_length(files, 0)
72
73 })
74
75 test_that("init succeeds even if there are parse errors in project", {
76
77 renv_tests_scope()
78 writeLines("oh no", con = "analysis.R")
79
80 init()
81 expect_true(file.exists("renv.lock"))
82
83 })
84
85 test_that("init() works in path containing accented characters", {
86
87 # ensure the project path can be represented in native encoding
88 project <- enc2utf8("pr\u{00f8}ject")
89
90 roundtrip <- tryCatch(
91 enc2utf8(enc2native(project)),
92 condition = identity
93 )
94
95 if (!identical(project, roundtrip))
96 skip("project cannot be represented in native encoding")
97
98 # TODO(Kevin): Windows tests on CI had the following NOTE
99 #
100 # Found the following files/directories:
101 # 'RtmpKIUpGHprĂžject'
102 #
103 # so we should run this down.
104 project <- paste(tempdir(), enc2native(project), sep = "/")
105 renv_tests_scope(project = project)
106 defer(unlink(project, recursive = TRUE))
107
108 init()
109
110 install("toast")
111 expect_true(renv_package_installed("bread"))
112 expect_true(renv_package_installed("toast"))
113
114 snapshot(library = paths$library(), type = "all")
115 lockfile <- renv_lockfile_load(project = getwd())
116 expect_true(!is.null(lockfile$Packages$bread))
117 expect_true(!is.null(lockfile$Packages$toast))
118
119 })
120
121 test_that("we use an external library path for package projects", {
122
123 skip_on_cran()
124 renv_tests_scope()
125
126 # use custom userdir
127 userdir <- renv_scope_tempfile("renv-userdir-override")
128 ensure_directory(userdir)
129 userdir <- renv_path_normalize(userdir, mustWork = TRUE)
130 renv_scope_options(renv.userdir.override = userdir)
131
132 # sanity check
133 userdir <- renv_bootstrap_user_dir()
134 if (!identical(expect_true(renv_path_within(userdir, tempdir())), TRUE))
135 skip("userdir not in tempdir; cannot proceed")
136
137 # initialize a package project
138 writeLines("Type: Package", con = "DESCRIPTION")
139 init()
140
141 # check for external library path
142 library <- renv_libpaths_active()
143
144 expect_true(
145 object = renv_path_within(library, userdir),
146 info = sprintf("- %s\n- %s\n", library, userdir)
147 )
148
149 })
150
151 test_that("a project with unnamed repositories can be initialized", {
152
153 skip_on_cran()
154 renv_tests_scope()
155
156 repos <- c(
157 CRAN = "https://cran.rstudio.com",
158 "https://localhost:12345"
159 )
160
161 renv_scope_options(repos = repos)
162 init()
163
164 lockfile <- renv_lockfile_read("renv.lock")
165 repos <- lockfile[["R"]][["Repositories"]]
166
167 expect_equal(
168 repos,
169 list(
170 CRAN = "https://cran.rstudio.com",
171 "https://localhost:12345" = "https://localhost:12345"
172 )
173 )
174
175 })
176
177 test_that("RENV_PATHS_RENV is respected on init", {
178
179 skip_on_cran()
180
181 renv_tests_scope()
182 unlink("renv", recursive = TRUE)
183
184 renv_scope_envvars(
185 RENV_PATHS_LOCKFILE = ".renv/renv.lock",
186 RENV_PATHS_RENV = ".renv",
187 # don't execute user profile
188 R_PROFILE_USER = ""
189 )
190
191 # perform init in sub-process
192 args <- c("-s", "-e", renv_shell_quote("renv::init()"))
193 renv_system_exec(R(), args, action = "executing init()")
194
195 # check that the requisite files were created
196 expect_true(file.exists(".renv"))
197 expect_true(file.exists(".renv/renv.lock"))
198
199 script <- renv_test_code({
200 writeLines(Sys.getenv("RENV_PATHS_RENV"))
201 })
202
203 renv_scope_envvars(R_PROFILE_USER = NULL, RENV_PROJECT = NULL)
204 args <- c("-s", "-f", script)
205 renv <- renv_system_exec(R(), args, action = "reading RENV_PATHS_RENV")
206
207 expect_equal(tail(renv, n = 1L), ".renv")
208
209 })
210
211 test_that("init() uses PPM by default", {
212 skip_on_cran()
213
214 # simulate "fresh" R session with unset repositories
215 renv_scope_options(repos = c(CRAN = "@CRAN@"))
216 repos <- renv_init_repos()
217 expect_equal(repos[["CRAN"]], "https://packagemanager.posit.co/cran/latest")
218
219 })
220
221 test_that("a project can be initialized without loading it", {
222 skip_on_cran()
223
224 project <- renv_tests_scope()
225 init()
226 expect_equal(renv_project_get(), project)
227
228 oldwd <- getwd()
229 oldrepos <- getOption("repos")
230 oldlibs <- .libPaths()
231
232 other <- renv_scope_tempfile("renv-project-")
233 ensure_directory(other)
234 init(project = other, load = FALSE, restart = FALSE)
235 expect_equal(renv_project_get(), project)
236 expect_true(file.exists(file.path(other, "renv.lock")))
237
238 expect_equal(oldwd, getwd())
239 expect_equal(oldrepos, getOption("repos"))
240 expect_equal(oldlibs, .libPaths())
241 })
242
243 test_that("init() respects user-requested snapshot type", {
244 project <- renv_tests_scope()
245 writeLines("Depends: bread", con = "DESCRIPTION")
246 writeLines("library(toast)", con = "deps.R")
247 init()
248
249 expect_true(renv_package_installed("bread"))
250 expect_false(renv_package_installed("toast"))
251 expect_equal(settings$snapshot.type(), "explicit")
252 })
253
254 test_that("init() respects Remotes in a project DESCRIPTION file", {
255
256 skip_on_cran()
257 skip_if_no_github_auth()
258
259 project <- renv_tests_scope("skeleton")
260 writeLines("Depends: skeleton\nRemotes: kevinushey/skeleton", con = "DESCRIPTION")
261 init()
262 expect_true(renv_package_installed("skeleton"))
263
264 })
265
266 test_that("a project using named remotes can be initialized", {
267 project <- renv_tests_scope()
268
269 contents <- heredoc('
270 Depends:
271 toast
272 Remotes:
273 toast=toast
274 ')
275 writeLines(contents, con = "DESCRIPTION")
276
277 init(settings = list(snapshot.type = "explicit"))
278 expect_true(renv_package_installed("toast"))
279
280 })