Debian Codegraph @

tests/testthat/test-download.R

raw
1
2 test_that("we avoid downloading files twice", {
3 skip_on_cran()
4
5 if (!renv_download_method() %in% c("curl", "wget"))
6 skip("required downloader not available")
7
8 url <- "https://cloud.r-project.org/src/contrib/Archive/sourcetools/sourcetools_0.1.0.tar.gz"
9 destfile <- renv_scope_tempfile("renv-download-", fileext = ".tar.gz")
10
11 # download once and check file metadata
12 download(url, destfile, quiet = TRUE)
13 before <- renv_file_info(destfile)$mtime
14
15 # download again and check the file info hasn't changed
16 download(url, destfile, quiet = TRUE)
17 after <- renv_file_info(destfile)$mtime
18
19 # check that they're the same.
20 expect_identical(before, after)
21
22 })
23
24 test_that("we can successfully tweak the user agent string", {
25
26 utils <- asNamespace("utils")
27
28 ok <-
29 is.function(utils$makeUserAgent) &&
30 identical(formals(utils$makeUserAgent), pairlist(format = TRUE))
31
32 if (!ok)
33 return(NULL)
34
35 headers <- c("Key" = "Value")
36
37 before <- utils$makeUserAgent
38 expect_true(renv_download_default_agent_scope_impl(headers = headers))
39 after <- utils$makeUserAgent
40
41 expect_identical(
42 paste0(before(format = TRUE), "Key: Value\r\n"),
43 after(format = TRUE)
44 )
45
46 expect_identical(before(format = FALSE), after(format = FALSE))
47
48 })
49
50 test_that("renv_download_default_agent_scope_impl resets itself", {
51
52 before <- asNamespace("utils")$makeUserAgent
53 local(renv_download_default_agent_scope_impl(c("Key" = "Value")))
54 expect_equal(asNamespace("utils")$makeUserAgent, before)
55
56 })
57
58
59 test_that("we can successfully download files with different downloaders", {
60 skip_on_cran()
61 skip_on_os("windows")
62
63 # download a small sample file
64 url <- "https://cloud.r-project.org/src/base/THANKS"
65 destfile <- renv_scope_tempfile("r-thanks-")
66 method <- renv_download_method()
67 download.file(url, destfile = destfile, quiet = TRUE, method = method)
68 thanks <- readLines(destfile)
69
70 if (nzchar(Sys.which("curl"))) local({
71 renv_scope_envvars(RENV_DOWNLOAD_FILE_METHOD = "curl")
72 destfile <- renv_scope_tempfile("r-curl-thanks-")
73 download(url, destfile, quiet = TRUE)
74 expect_equal(readLines(destfile), thanks)
75 })
76
77 if (renv_platform_windows()) local({
78 renv_scope_envvars(RENV_DOWNLOAD_FILE_METHOD = "wininet")
79 destfile <- renv_scope_tempfile("r-wininet-thanks-")
80 download(url, destfile, quiet = TRUE)
81 expect_equal(readLines(destfile), thanks)
82 })
83
84 if (capabilities("libcurl") %||% FALSE) local({
85 renv_scope_envvars(RENV_DOWNLOAD_FILE_METHOD = "libcurl")
86 destfile <- renv_scope_tempfile("r-libcurl-thanks-")
87 download(url, destfile, quiet = TRUE)
88 expect_equal(readLines(destfile), thanks)
89 })
90
91 # TODO: fails on winbuilder
92 # if (nzchar(Sys.which("wget"))) local({
93 # renv_scope_envvars(RENV_DOWNLOAD_FILE_METHOD = "wget")
94 # destfile <- renv_scope_tempfile("r-wget-thanks-")
95 # download(url, destfile, quiet = TRUE)
96 # expect_equal(readLines(destfile), thanks)
97 # })
98
99 })
100
101 test_that("downloads work with file URIs", {
102
103 renv_tests_scope()
104
105 repos <- getOption("repos")[["CRAN"]]
106 url <- file.path(repos, "src/contrib/PACKAGES")
107
108 destfile <- renv_scope_tempfile("packages-")
109 download(url, destfile = destfile)
110
111 expect_true(file.exists(!!destfile))
112
113 })
114
115 test_that("downloads work with UNC paths on Windows", {
116 skip_on_cran()
117 skip_if_not(renv_platform_windows())
118
119 renv_tests_scope()
120
121 # get path to repos PACKAGES file
122 repos <- getOption("repos")[["CRAN"]]
123 base <- sub("^file:/*", "", repos)
124 url <- file.path(base, "src/contrib/PACKAGES")
125 norm <- renv_path_normalize(url)
126
127 # create server-style path to localhost
128 unc <- sub("^([a-zA-Z]):", "//localhost/\\1$", norm)
129 expect_true(file.exists(unc))
130
131 destfile <- renv_scope_tempfile("packages-")
132
133 urls <- c(unc, paste0("file:", unc))
134 for (url in urls) {
135 download(unc, destfile)
136 expect_true(file.exists(destfile))
137 unlink(destfile)
138 }
139
140 })
141
142 test_that("we can check that a URL is available", {
143 skip_on_cran()
144 skip_if_not_installed("webfakes")
145
146 app <- webfakes::new_app_process(webfakes::httpbin_app())
147
148 url <- paste0(app$url(), "/bytes/100")
149 expect_true(renv_download_available(url))
150
151 # also test the different methods
152 expect_true(renv_download_available_headers(url))
153 expect_true(renv_download_available_range(url))
154 expect_true(renv_download_available_fallback(url))
155 })
156
157 test_that("download failures are reported if destfile not writable", {
158 skip_on_cran()
159 skip_on_os("windows")
160
161 tdir <- tempfile("renv-forbidden-")
162 dir.create(tdir, mode = "0000")
163 expect_true(file.exists(tdir))
164
165 tfile <- tempfile("renv-download-", tmpdir = tdir)
166 expect_error(
167 download(url = "https://cran.r-project.org/", destfile = tfile, quiet = TRUE)
168 )
169
170 Sys.chmod(tdir, mode = "0755")
171 download(url = "https://cran.r-project.org/", destfile = tfile, quiet = TRUE)
172 expect_true(file.exists(tfile))
173
174 unlink(tdir, recursive = TRUE)
175
176 })
177
178 test_that("renv_curl_version returns a valid numeric_version", {
179 skip_if_not(nzchar(Sys.which("curl")))
180
181 version <- renv_curl_version()
182 expect_s3_class(version, "numeric_version")
183 expect_true(version > "0.0.0")
184 })
185
186 test_that("renv_download_parallel_method respects renv_download_method", {
187
188 # when download method is curl and version supports --parallel
189 local({
190 renv_scope_envvars(RENV_DOWNLOAD_METHOD = "curl")
191 version <- catch(renv_curl_version())
192 has_parallel <- !inherits(version, "error") && version >= "7.66.0"
193 expected <- if (has_parallel) "curl" else "sequential"
194 expect_equal(renv_download_parallel_method(), expected)
195 })
196
197 # when download method is libcurl
198 local({
199 renv_scope_envvars(RENV_DOWNLOAD_METHOD = "libcurl")
200 expected <- if (getRversion() >= "4.5.0") "libcurl" else "sequential"
201 expect_equal(renv_download_parallel_method(), expected)
202 })
203
204 # when download method is something else, always sequential
205 local({
206 renv_scope_envvars(RENV_DOWNLOAD_METHOD = "wininet")
207 expect_equal(renv_download_parallel_method(), "sequential")
208 })
209
210 })
211
212 test_that("renv_download_curl_config_text generates valid config", {
213
214 text <- renv_download_curl_config_text(
215 url = "https://example.com/pkg.tar.gz",
216 destfile = "/tmp/pkg.tar.gz",
217 type = NULL,
218 headers = c("X-Custom" = "value")
219 )
220
221 expect_true(any(grepl("location", text)))
222 expect_true(any(grepl("fail", text)))
223 expect_true(any(grepl("silent", text)))
224 expect_true(any(grepl("url", text)))
225 expect_true(any(grepl("output", text)))
226 expect_true(any(grepl("X-Custom: value", text)))
227
228 # HEAD request adds extra flags
229 head_text <- renv_download_curl_config_text(
230 url = "https://example.com/pkg.tar.gz",
231 destfile = "/tmp/pkg.tar.gz",
232 type = NULL,
233 headers = NULL,
234 request = "HEAD"
235 )
236
237 expect_true(any(grepl("head", head_text)))
238 expect_true(any(grepl("include", head_text)))
239
240 })
241
242 test_that("renv_download_parallel works with curl backend", {
243
244 skip_on_cran()
245 skip_if_not_installed("webfakes")
246 skip_if_not(nzchar(Sys.which("curl")))
247
248 version <- catch(renv_curl_version())
249 skip_if(inherits(version, "error") || version < "7.66.0",
250 "curl >= 7.66.0 required for --parallel")
251
252 app <- webfakes::new_app_process(webfakes::httpbin_app())
253
254 urls <- paste0(app$url(), "/bytes/", c(100, 200, 300))
255 names(urls) <- c("a", "b", "c")
256 destfiles <- vapply(names(urls), function(nm) {
257 tempfile(paste0("renv-parallel-curl-", nm, "-"))
258 }, character(1L))
259 types <- rep("unknown", 3L)
260
261 renv_scope_envvars(RENV_DOWNLOAD_METHOD = "curl")
262 ok <- renv_download_parallel(urls, destfiles, types)
263
264 expect_true(all(ok))
265 expect_equal(names(ok), names(urls))
266 for (i in seq_along(destfiles)) {
267 expect_true(file.exists(destfiles[[i]]))
268 expect_equal(file.size(destfiles[[i]]), c(100, 200, 300)[[i]])
269 }
270
271 unlink(destfiles)
272
273 })
274
275 test_that("renv_download_parallel works with libcurl backend", {
276
277 skip_on_cran()
278 skip_if_not_installed("webfakes")
279 skip_if_not(getRversion() >= "4.5.0", "R >= 4.5.0 required for vectorized libcurl")
280 skip_if_not(capabilities("libcurl"), "libcurl not available")
281
282 app <- webfakes::new_app_process(webfakes::httpbin_app())
283
284 urls <- paste0(app$url(), "/bytes/", c(50, 150))
285 names(urls) <- c("x", "y")
286 destfiles <- vapply(names(urls), function(nm) {
287 tempfile(paste0("renv-parallel-libcurl-", nm, "-"))
288 }, character(1L))
289 types <- rep("unknown", 2L)
290
291 renv_scope_envvars(RENV_DOWNLOAD_METHOD = "libcurl")
292 ok <- renv_download_parallel(urls, destfiles, types)
293
294 expect_true(all(ok))
295 expect_equal(names(ok), names(urls))
296 for (i in seq_along(destfiles)) {
297 expect_true(file.exists(destfiles[[i]]))
298 expect_equal(file.size(destfiles[[i]]), c(50, 150)[[i]])
299 }
300
301 unlink(destfiles)
302
303 })
304
305 test_that("renv_download_parallel_sequential works as fallback", {
306 skip_on_cran()
307 skip_if_not_installed("webfakes")
308
309 app <- webfakes::new_app_process(webfakes::httpbin_app())
310
311 urls <- paste0(app$url(), "/bytes/", c(80, 160))
312 names(urls) <- c("p", "q")
313 destfiles <- vapply(names(urls), function(nm) {
314 tempfile(paste0("renv-parallel-seq-", nm, "-"))
315 }, character(1L))
316 types <- rep("unknown", 2L)
317
318 ok <- renv_download_parallel_sequential(urls, destfiles, types)
319
320 expect_true(all(ok))
321 expect_equal(names(ok), names(urls))
322 for (df in destfiles)
323 expect_true(file.exists(df))
324
325 unlink(destfiles)
326
327 })
328
329 test_that("renv_download_parallel reports failure for bad URLs", {
330 skip_on_cran()
331 skip_if_not_installed("webfakes")
332
333 app <- webfakes::new_app_process(webfakes::httpbin_app())
334
335 urls <- c(
336 good = paste0(app$url(), "/bytes/100"),
337 bad = paste0(app$url(), "/status/404")
338 )
339 destfiles <- vapply(names(urls), function(nm) {
340 tempfile(paste0("renv-parallel-fail-", nm, "-"))
341 }, character(1L))
342 types <- rep("unknown", 2L)
343
344 ok <- renv_download_parallel_sequential(urls, destfiles, types)
345
346 expect_true(ok[["good"]])
347 expect_false(ok[["bad"]])
348
349 unlink(destfiles)
350
351 })