Debian Codegraph @

tests/testthat/test-lockfile-validate.R

raw
1 test_that("a known-good full lockfile passes validation", {
2 skip_on_cran()
3 skip_if_not_installed("jsonvalidate")
4
5 lockfile <- '
6 {
7 "R": {
8 "Version": "4.4.3",
9 "Repositories": [
10 { "Name": "CRAN", "URL": "https://cloud.r-project.org" }
11 ]
12 },
13 "Python": {
14 "Name": ".venv",
15 "Type": "virtualenv",
16 "Version": "3.10.12"
17 },
18 "Bioconductor": {
19 "Version": "3.18"
20 },
21 "Packages": {
22 "fullpkg": {
23 "Package": "fullpkg",
24 "Version": "1.2.3",
25 "Source": "Repository",
26 "Repository": "CRAN",
27 "Hash": "0123456789abcdef0123456789abcdef",
28 "biocViews": "Genomics",
29 "RemoteType": "github",
30 "RemoteHost": "api.github.com",
31 "RemoteUsername": "rstudio",
32 "RemoteRepo": "fullpkg",
33 "RemoteRepos": "https://cloud.r-project.org",
34 "RemoteRef": "main",
35 "RemotePkgRef": "fullpkg",
36 "RemotePkgPlatform": "x86_64-pc-linux-gnu",
37 "RemoteSha": "abcdefabcdefabcdefabcdefabcdefab",
38 "Title": "A Full Featured Package",
39 "Authors@R": "person(\\"Jane\\", \\"Doe\\", role = c(\\"aut\\", \\"cre\\"), email = \\"[email protected]\\")",
40 "Description": "This is a comprehensive DESCRIPTION-like entry with every field populated.",
41 "License": "MIT + file LICENSE",
42 "URL": "https://github.com/rstudio/fullpkg",
43 "BugReports": "https://github.com/rstudio/fullpkg/issues",
44 "Author": "Jane Doe [aut, cre]",
45 "Maintainer": "Jane Doe <[email protected]>",
46 "Depends": ["R (>= 4.0)"],
47 "Imports": ["cli", "rlang"],
48 "LinkingTo": ["cpp11"],
49 "Suggests": ["testthat", "knitr"],
50 "Enhances": ["shiny"],
51 "SystemRequirements": "libxml2 (>= 2.9.0)",
52 "VignetteBuilder": "knitr",
53 "Type": "Package",
54 "Date": "2025-04-01",
55 "ByteCompile": "true",
56 "Biarch": "TRUE",
57 "Encoding": "UTF-8",
58 "Language": "en-US",
59 "RoxygenNote": "7.4.0",
60 "NeedsCompilation": "yes",
61 "Copyright": "© 2025 Jane Doe",
62 "Config/Needs/website": "pkgdown",
63 "Config/testthat/edition": "3",
64 "Config/testthat/parallel": "true",
65 "Config/testthat/start-first": "install,restore",
66 "Config/autostyle/scope": "line_breaks",
67 "Config/autostyle/strict": "true",
68 "Config/autostyle/rmd": "false"
69 }
70 }
71 }
72 '
73 expect_no_error(lockfile_validate(lockfile = lockfile))
74 expect_true(lockfile_validate(lockfile = lockfile))
75
76 })
77
78
79 test_that("a known-good lockfile with extra fields passes validation", {
80
81 skip_on_cran()
82 skip_if_not_installed("jsonvalidate")
83
84 # Lockfile adds a R$Nickname field not present in the schema
85 lockfile <- '
86 {
87 "R": {
88 "Version": "4.2.3",
89 "Nickname": "Shortstop Beagle",
90 "Repositories": [
91 {
92 "Name": "CRAN",
93 "URL": "https://cloud.r-project.org"
94 },
95 {
96 "Name": "BioCsoft",
97 "URL": "https://bioconductor.org/packages/3.8/bioc"
98 }
99 ]
100 },
101 "Python": {
102 "Version": "3.10.12",
103 "Type": "virtualenv",
104 "Name": "./renv/python/virtualenvs/renv-python-3.10"
105 },
106 "Bioconductor": {
107 "Version": "3.8"
108 },
109 "Packages": {
110 "markdown": {
111 "Package": "markdown",
112 "Version": "1.0",
113 "Source": "Repository",
114 "Repository": "CRAN",
115 "Hash": "4584a57f565dd7987d59dda3a02cfb41"
116 },
117 "mime": {
118 "Package": "mime",
119 "Version": "0.12.1",
120 "Source": "GitHub",
121 "RemoteType": "github",
122 "RemoteHost": "api.github.com",
123 "RemoteUsername": "yihui",
124 "RemoteRepo": "mime",
125 "RemoteRef": "main",
126 "RemoteSha": "1763e0dcb72fb58d97bab97bb834fc71f1e012bc",
127 "Requirements": [
128 "tools"
129 ],
130 "Hash": "c2772b6269924dad6784aaa1d99dbb86"
131 }
132 }
133 }
134 '
135 expect_no_error(lockfile_validate(lockfile = lockfile))
136 expect_true(lockfile_validate(lockfile = lockfile))
137
138 })
139
140 test_that("a custom schema file can be used for successful validation", {
141
142 skip_on_cran()
143 skip_if_not_installed("jsonvalidate")
144
145 # Custom schema adds a required R$Nickname field present in the lockfile
146 lockfile <- '
147 {
148 "R": {
149 "Version": "4.2.3",
150 "Nickname": "Shortstop Beagle",
151 "Repositories": [
152 {
153 "Name": "CRAN",
154 "URL": "https://cloud.r-project.org"
155 }
156 ]
157 },
158 "Packages": {
159 "markdown": {
160 "Package": "markdown",
161 "Version": "1.0",
162 "Source": "Repository",
163 "Repository": "CRAN",
164 "Hash": "4584a57f565dd7987d59dda3a02cfb41"
165 }
166 }
167 }
168 '
169
170 schema <- '
171 {
172 "$schema": "http://json-schema.org/draft-07/schema#",
173 "type": "object",
174 "properties": {
175 "R": {
176 "type": "object",
177 "properties": {
178 "Version": {
179 "type": "string"
180 },
181 "Nickname": {
182 "type": "string"
183 }
184 },
185 "required": ["Version", "Nickname"]
186 }
187 }
188 }
189 '
190 expect_no_error(lockfile_validate(lockfile = lockfile, schema = schema))
191 expect_true(lockfile_validate(lockfile = lockfile, schema = schema))
192
193 })
194
195 test_that("a custom schema file can be used for failed validation", {
196
197 skip_on_cran()
198 skip_if_not_installed("jsonvalidate")
199
200 # Custom schema adds a required R$Nickname field not present in the lockfile
201 lockfile <- '
202 {
203 "R": {
204 "Version": "4.2.3",
205 "Repositories": [
206 {
207 "Name": "CRAN",
208 "URL": "https://cloud.r-project.org"
209 }
210 ]
211 },
212 "Packages": {
213 "markdown": {
214 "Package": "markdown",
215 "Version": "1.0",
216 "Source": "Repository",
217 "Repository": "CRAN",
218 "Hash": "4584a57f565dd7987d59dda3a02cfb41"
219 }
220 }
221 }
222 '
223
224 schema <- '
225 {
226 "$schema": "http://json-schema.org/draft-07/schema#",
227 "type": "object",
228 "properties": {
229 "R": {
230 "type": "object",
231 "properties": {
232 "Version": {
233 "type": "string"
234 },
235 "Nickname": {
236 "type": "string"
237 }
238 },
239 "required": ["Version", "Nickname"]
240 }
241 }
242 }
243 '
244
245 expect_false(lockfile_validate(lockfile = lockfile, schema = schema))
246
247 })
248
249 test_that("an incorrect Packages$Hash field fails validation", {
250
251 skip_on_cran()
252 skip_if_not_installed("jsonvalidate")
253
254 lockfile <- '
255 {
256 "R": {
257 "Version": "4.2.3",
258 "Repositories": [
259 {
260 "Name": "CRAN",
261 "URL": "https://cloud.r-project.org"
262 }
263 ]
264 },
265 "Packages": {
266 "markdown": {
267 "Package": "markdown",
268 "Version": "1.0",
269 "Source": "Repository",
270 "Repository": "CRAN",
271 "Hash": "4584a57f565dd"
272 }
273 }
274 }
275 '
276
277 expect_false(lockfile_validate(lockfile = lockfile))
278
279 })
280
281 test_that("invalid JSON fails validation", {
282
283 skip_on_cran()
284 skip_if_not_installed("jsonvalidate")
285
286 # Packages uses [] which is not valid JSON
287 lockfile <- '
288 {
289 "R": {
290 "Version": "4.2.3",
291 "Repositories": [
292 {
293 "Name": "CRAN",
294 "URL": "https://cloud.r-project.org"
295 }
296 ]
297 },
298 "Packages": [
299 "markdown": {
300 "Package": "markdown",
301 "Version": "1.0",
302 "Source": "Repository",
303 "Repository": "CRAN",
304 "Hash": "4584a57f565dd7987d59dda3a02cfb41"
305 }
306 ]
307 }
308 '
309 expect_error(lockfile_validate(lockfile = lockfile, error = TRUE))
310
311 })
312
313 test_that("strict mode catches unknown keyword in provided schema", {
314
315 skip_on_cran()
316 skip_if_not_installed("jsonvalidate")
317
318 # Custom schema provides "Version" with "type": "UNKNOWN"
319 lockfile <- '
320 {
321 "R": {
322 "Version": "4.2.3",
323 "Repositories": [
324 {
325 "Name": "CRAN",
326 "URL": "https://cloud.r-project.org"
327 }
328 ]
329 },
330 "Packages": {
331 "markdown": {
332 "Package": "markdown",
333 "Version": "1.0",
334 "Source": "Repository",
335 "Repository": "CRAN",
336 "Hash": "4584a57f565dd7987d59dda3a02cfb41"
337 }
338 }
339 }
340 '
341
342 schema <- '
343 {
344 "$schema": "http://json-schema.org/draft-07/schema#",
345 "type": "object",
346 "properties": {
347 "R": {
348 "type": "object",
349 "properties": {
350 "Version": {
351 "UNKNOWN": "string"
352 }
353 }
354 }
355 }
356 }
357 '
358 expect_true(lockfile_validate(lockfile = lockfile, schema = schema))
359 expect_error(
360 lockfile_validate(lockfile = lockfile, schema = schema, strict = TRUE)
361 )
362
363 })