Debian Codegraph @

tests/testthat/test-record.R

raw
1
2 test_that("an existing lockfile can be updated", {
3
4 renv_tests_scope("bread")
5 init()
6
7 # change bread to 0.1.0
8 record(list(bread = "[email protected]"))
9
10 lockfile <- renv_lockfile_read("renv.lock")
11 records <- renv_lockfile_records(lockfile)
12 expect_length(records, 1L)
13 expect_equal(records$bread$Version, "0.1.0")
14
15 # add a new record
16 record(list(toast = "[email protected]"))
17
18 lockfile <- renv_lockfile_read("renv.lock")
19 records <- renv_lockfile_records(lockfile)
20 expect_length(records, 2L)
21 expect_equal(records$bread$Version, "0.1.0")
22 expect_equal(records$toast$Version, "1.0.0")
23
24 # use short-hand
25 lockfile <- record("[email protected]", lockfile = lockfile)
26 records <- renv_lockfile_records(lockfile)
27 expect_equal(records$toast$Version, "1.0.1")
28
29 lockfile <- record(list(toast = "1.0.2"), lockfile = lockfile)
30 records <- renv_lockfile_records(lockfile)
31 expect_equal(records$toast$Version, "1.0.2")
32
33 # remove a record
34 lockfile <- record(list(toast = NULL), lockfile = lockfile)
35 records <- renv_lockfile_records(lockfile)
36 expect_true(is.null(records$toast))
37
38 })
39
40 test_that("record(<package>) also records version", {
41
42 renv_tests_scope()
43
44 # create empty lockfile
45 snapshot()
46
47 # add a record
48 record("breakfast")
49
50 # check that the version of breakfast was recorded
51 lockfile <- renv_lockfile_load(project = getwd())
52 breakfast <- lockfile$Packages$breakfast
53 expect_identical(breakfast$Version, "1.0.0")
54
55 })
56
57 test_that("record() enriches resolved records with DESCRIPTION fields", {
58
59 renv_tests_scope("breakfast")
60 init()
61
62 # snapshot record (built from the installed DESCRIPTION) is the
63 # baseline for what an "enriched" record should look like
64 lockfile <- renv_lockfile_read("renv.lock")
65 snap_record <- renv_lockfile_records(lockfile)$breakfast
66
67 record("breakfast")
68
69 lockfile <- renv_lockfile_read("renv.lock")
70 rec_record <- renv_lockfile_records(lockfile)$breakfast
71
72 # record() must produce more than the minimal four fields
73 minimal <- c("Package", "Version", "Source", "Repository")
74 expect_true(length(setdiff(names(rec_record), minimal)) > 0L)
75
76 # the standard PACKAGES-derived dep / license fields should be present
77 # and agree with what snapshot() wrote
78 shared <- c("Package", "Version", "Source", "Repository",
79 "Depends", "Suggests", "License")
80 shared <- intersect(shared, names(snap_record))
81 for (field in shared)
82 expect_identical(rec_record[[field]], snap_record[[field]],
83 info = sprintf("field '%s' differs", field))
84
85 })
86
87 test_that("record(enrich = FALSE) keeps minimal records", {
88
89 renv_tests_scope("breakfast")
90 init()
91
92 record("breakfast", enrich = FALSE)
93
94 lockfile <- renv_lockfile_read("renv.lock")
95 rec_record <- renv_lockfile_records(lockfile)$breakfast
96
97 expect_setequal(
98 names(rec_record),
99 c("Package", "Version", "Source", "Repository")
100 )
101
102 })
103
104 test_that("enrich cache key varies with options('repos')", {
105
106 # the description fetcher reads from options('repos'), so the memoize
107 # key must too. version-pinned repository specs have no Repository
108 # field, so without this guard a record('[email protected]') call would return
109 # cached data fetched against a different repos config.
110 cranlike <- list(
111 Package = "foo", Version = "1.0.0", Source = "Repository"
112 )
113
114 renv_scope_options(repos = c(CRAN = "https://cran.example/a"))
115 before <- renv_record_enrich_key(cranlike)
116
117 renv_scope_options(repos = c(CRAN = "https://cran.example/b"))
118 after <- renv_record_enrich_key(cranlike)
119
120 expect_false(identical(before, after))
121
122 # github (and other non-repository sources) carry full source identity
123 # on the record itself; their key should not depend on options('repos')
124 github <- list(
125 Package = "foo", Version = "1.0.0", Source = "GitHub",
126 RemoteUsername = "user", RemoteRepo = "repo", RemoteSha = "abc"
127 )
128
129 renv_scope_options(repos = c(CRAN = "https://cran.example/a"))
130 before <- renv_record_enrich_key(github)
131
132 renv_scope_options(repos = c(CRAN = "https://cran.example/b"))
133 after <- renv_record_enrich_key(github)
134
135 expect_identical(before, after)
136
137 })
138
139 test_that("record() does not enrich caller-supplied list records", {
140
141 # a fully-resolved list record should pass through untouched: no
142 # network access, no field rewrite. this matches the documented "record
143 # this entry as provided" behavior and lets users seed unusual entries
144 # (custom fields, sources renv doesn't understand) without round-tripping
145 # through the snapshot transform.
146 renv_tests_scope()
147 snapshot()
148
149 custom <- list(
150 Package = "fictional",
151 Version = "9.9.9",
152 Source = "Repository",
153 Repository = "CRAN",
154 Custom = "preserved"
155 )
156 record(list(fictional = custom))
157
158 lockfile <- renv_lockfile_read("renv.lock")
159 fictional <- renv_lockfile_records(lockfile)$fictional
160
161 expect_identical(fictional$Version, "9.9.9")
162 expect_identical(fictional$Custom, "preserved")
163
164 })
165
166 test_that("record() stores Repository as the named form, not the URL", {
167
168 # version-pinned specs skip the 'latest' branch in
169 # renv_remotes_resolve_repository(), so the resolved record has no
170 # Repository field. enrichment must still produce the named form (e.g.
171 # 'CRAN') that snapshot() writes -- otherwise status() will report a
172 # spurious repo mismatch against an installed library.
173 renv_tests_scope("egg")
174 init()
175 record("[email protected]")
176
177 lockfile <- renv_lockfile_read("renv.lock")
178 egg <- renv_lockfile_records(lockfile)$egg
179
180 expect_identical(egg$Repository, "CRAN")
181 expect_null(egg$Name)
182
183 })