Debian Codegraph @

tests/testthat/test-lockfile.R

raw
1
2 test_that("lockfiles can be diffed", {
3
4 lhs <- list(A = 1, B = 2, C = "a", D = list(E = 1, F = 2))
5 rhs <- list(A = 1, B = 3, C = "b", D = list(E = 1, F = 3))
6
7 diff <- renv_lockfile_diff(lhs, rhs)
8 expected <- list(
9 B = list(before = 2, after = 3),
10 C = list(before = "a", after = "b"),
11 D = list(
12 F = list(before = 2, after = 3)
13 )
14 )
15
16 expect_identical(diff, expected)
17
18 })
19
20 test_that("we can serialize lockfiles using unnamed repositories", {
21
22 # no repositories set
23 local({
24 renv_scope_options(repos = list())
25 actual <- renv_lockfile_init(project = NULL)
26 json <- renv_lockfile_write(actual, file = NULL)
27 expected <- renv_lockfile_read(text = json)
28 expect_equal(actual, expected)
29 })
30
31 # unnamed repositories set
32 local({
33 renv_scope_options(repos = c("alpha", "beta"))
34 actual <- renv_lockfile_init(project = NULL)
35 json <- renv_lockfile_write(actual, file = NULL)
36 expected <- renv_lockfile_read(text = json)
37 expect_equal(actual, expected)
38 })
39
40 })
41
42 test_that("we can create lockfiles from manifests", {
43
44 skip_on_cran()
45 lock <- renv_lockfile_from_manifest("resources/manifest.json")
46
47 expect_equal(lock$R$Version, "4.2.1")
48 expect_equal(lock$R$Repositories, list(CRAN = "https://cloud.r-project.org"))
49
50 })
51
52 test_that("we create lockfile from a manifest automatically when no lockfile found", {
53
54 skip_on_cran()
55
56 project <- tempfile()
57 dir.create(project)
58
59 path <- renv_tests_path("resources/manifest.json")
60 expected <- renv_lockfile_from_manifest(path)
61 file.copy(path, file.path(project, "manifest.json"))
62
63 # when called with `strict = TRUE` does not create manifest
64 expect_error(renv_lockfile_load(project, strict = TRUE))
65
66 # creates and reads lockfile
67 actual <- renv_lockfile_load(project)
68 expect_identical(expected, actual)
69 expect_true(file.exists(file.path(project, "renv.lock")))
70
71 unlink(project, recursive = TRUE)
72
73 })
74
75 test_that("the Requirements field is read as character", {
76
77 lockfile <- renv_lockfile_read(text = '
78 {
79 "R": {
80 "Version": "2.15.2",
81 "Repositories": []
82 },
83 "Packages": {
84 "morning": {
85 "Package": "morning",
86 "Version": "0.1.0",
87 "Requirements": [
88 "coffee",
89 "toast"
90 ]
91 }
92 }
93 }
94 ')
95
96 actual <- lockfile$Packages$morning$Requirements
97 expected <- c("coffee", "toast")
98 expect_identical(actual, expected)
99
100 })
101
102 test_that("lockfile APIs can be used", {
103 renv_tests_scope("breakfast")
104 init()
105
106 lockfile <- lockfile_read()
107 expect_s3_class(lockfile, "renv_lockfile")
108
109 # try writing some repositories
110 repos <- list(CRAN = "https://cloud.r-project.org")
111 lockfile <- lockfile_modify(lockfile, repos = repos)
112 expect_equal(repos, lockfile$R$Repositories)
113
114 # try updating a record
115 lockfile <- lockfile_modify(lockfile, remotes = list(bread = "[email protected]"))
116 bread <- lockfile$Packages$bread
117 expect_equal(bread$Version, "0.1.0")
118
119 # try writing to file
120 lockfile_write(lockfile)
121
122 # check that it's the same
123 expect_equal(lockfile, lockfile_read())
124
125 })
126
127 test_that("credentials are sanitized from lockfile repository URLs", {
128
129 lockfile <- list(
130 R = list(
131 Version = "4.4.0",
132 Repositories = list(
133 Custom = "https://user:[email protected]/r"
134 )
135 ),
136 Packages = list(
137 mypkg = list(
138 Package = "mypkg",
139 Version = "1.0.0",
140 Source = "Repository",
141 Repository = "https://[email protected]/r"
142 ),
143 otherpkg = list(
144 Package = "otherpkg",
145 Version = "2.0.0",
146 Source = "Repository",
147 Repository = "CRAN"
148 )
149 )
150 )
151
152 sanitized <- renv_lockfile_sanitize(lockfile)
153
154 # repository URLs should have credentials stripped
155 expect_equal(sanitized$R$Repositories$Custom, "https://private.repo.com/r")
156 expect_equal(sanitized$Packages$mypkg$Repository, "https://private.repo.com/r")
157
158 # plain repository names should be unchanged
159 expect_equal(sanitized$Packages$otherpkg$Repository, "CRAN")
160
161 })
162
163 test_that("credential sanitization can be disabled via settings", {
164
165 renv_tests_scope()
166 settings$lockfile.sanitize(FALSE)
167
168 url <- "https://user:[email protected]/r"
169 lockfile <- list(
170 R = list(
171 Version = getRversion(),
172 Repositories = list(Custom = url)
173 ),
174 Packages = list()
175 )
176
177 renv_lockfile_write(lockfile, file = "renv.lock")
178 result <- renv_lockfile_read("renv.lock")
179 expect_equal(result$R$Repositories$Custom, url)
180
181 })
182
183 test_that("lockfiles with UTF-8 contents can be written, read", {
184
185 author <- enc2utf8("cr\u00e8me br\u00fbl\u00e9e")
186 Encoding(author) <- "UTF-8"
187
188 lockfile <- list(
189 Packages = list(
190 example = list(
191 Package = "example",
192 Version = "1.0.0",
193 Author = author
194 )
195 )
196 )
197
198 path <- renv_scope_tempfile("renv-lockfile-")
199 renv_lockfile_write(lockfile, file = path)
200 actual <- renv_lockfile_read(path)
201 expect_identical(unclass(actual), lockfile)
202
203 })