Debian Codegraph @

tests/testthat/test-index.R

raw
1
2 scope <- renv_id_generate()
3
4 counter <- (function() {
5
6 .count <- 0L
7
8 list(
9 increment = function() .count <<- .count + 1L,
10 get = function() .count,
11 reset = function() .count <<- 0L
12 )
13
14 })()
15
16 test_that("an index value is computed only once", {
17
18 counter$reset()
19 key <- renv_id_generate()
20
21 index(
22 scope = scope,
23 key = key,
24 value = counter$increment()
25 )
26
27 expect_equal(counter$get(), 1L)
28
29 index(
30 scope = scope,
31 key = key,
32 value = counter$increment()
33 )
34
35 expect_equal(counter$get(), 1L)
36
37 })
38
39 test_that("a timeout of 0 forces index to be re-computed", {
40
41 counter$reset()
42 key <- renv_id_generate()
43
44 index(
45 scope = scope,
46 key = key,
47 value = counter$increment()
48 )
49
50 expect_equal(counter$get(), 1L)
51
52 index(
53 scope = scope,
54 key = key,
55 value = counter$increment(),
56 limit = 0L
57 )
58
59 expect_equal(counter$get(), 2L)
60
61 })
62
63 test_that("other processes can use the index", {
64
65 path <- renv_scope_tempfile("renv-index-")
66 ensure_directory(path)
67 renv_scope_envvars(RENV_PATHS_INDEX = path)
68
69 key <- renv_id_generate()
70
71 index(
72 scope = scope,
73 key = key,
74 value = TRUE
75 )
76
77 script <- renv_test_code({
78 indexed <- renv:::index(scope, key, value)
79 writeLines(as.character(indexed))
80 }, list(scope = scope, key = key, value = FALSE))
81
82 output <- local({
83 renv_system_exec(
84 command = R(),
85 args = c("--vanilla", "-s", "-f", shQuote(script)),
86 action = "testing renv index",
87 quiet = FALSE
88 )
89 })
90
91 expect_equal(output, "TRUE")
92
93 })
94
95 test_that("a corrupt index is cleared and remade", {
96
97 counter$reset()
98 key <- renv_id_generate()
99
100 index(
101 scope = scope,
102 key = key,
103 value = counter$increment()
104 )
105
106 path <- renv_paths_index(scope, "index.json")
107 expect_true(file.exists(path))
108 expect_equal(counter$get(), 1L)
109
110 writeLines("BARF!", con = path)
111 contents <- readLines(path)
112 expect_true(identical(contents, "BARF!"))
113
114 index(
115 scope = scope,
116 key = key,
117 value = counter$increment()
118 )
119
120 contents <- readLines(path)
121 expect_false(identical(contents, "BARF!"))
122 expect_equal(counter$get(), 2L)
123
124 })
125
126 test_that("the available packages index is updated and cleaned", {
127
128 scope <- "available-packages"
129
130 path <- renv_scope_tempfile("renv-index-")
131 ensure_directory(path)
132 renv_scope_envvars(RENV_PATHS_INDEX = path)
133
134 renv_tests_scope()
135
136 # request available packages
137 db <- available_packages(type = "source")
138
139 # check that an index entry was created
140 idx <- index(scope)
141 expect_length(idx, 1L)
142
143 # check that we have two files in the index path
144 root <- renv_paths_index(scope)
145 files <- list.files(root)
146 expect_length(files, 2L)
147
148 })