|
| 1 | +# Mocked unit tests for java_download() |
| 2 | +test_that("java_download handles successful download and checksum", { |
| 3 | + local_cache_path <- withr::local_tempdir() |
| 4 | + |
| 5 | + # MOCK THE SLOW NETWORK CALL |
| 6 | + local_mocked_bindings( |
| 7 | + java_valid_versions = function(...) c("8", "11", "17", "21") |
| 8 | + ) |
| 9 | + |
| 10 | + local_mocked_bindings( |
| 11 | + curl_download = function(url, destfile, ...) { |
| 12 | + if (grepl("\\.md5$", destfile)) { |
| 13 | + writeLines("dummymd5checksum", destfile) |
| 14 | + } else { |
| 15 | + writeLines("dummy content", destfile) |
| 16 | + } |
| 17 | + }, |
| 18 | + .package = "curl" |
| 19 | + ) |
| 20 | + |
| 21 | + local_mocked_bindings( |
| 22 | + md5sum = function(files) setNames("dummymd5checksum", files), |
| 23 | + .package = "tools" |
| 24 | + ) |
| 25 | + |
| 26 | + expect_silent( |
| 27 | + result_path <- java_download( |
| 28 | + version = "21", |
| 29 | + cache_path = local_cache_path, |
| 30 | + quiet = TRUE |
| 31 | + ) |
| 32 | + ) |
| 33 | + expect_true(file.exists(result_path)) |
| 34 | +}) |
| 35 | + |
| 36 | +test_that("java_download aborts on MD5 checksum mismatch", { |
| 37 | + local_cache_path <- withr::local_tempdir() |
| 38 | + |
| 39 | + # MOCK THE SLOW NETWORK CALL |
| 40 | + local_mocked_bindings( |
| 41 | + java_valid_versions = function(...) c("8", "11", "17", "21") |
| 42 | + ) |
| 43 | + |
| 44 | + local_mocked_bindings( |
| 45 | + curl_download = function(url, destfile, ...) { |
| 46 | + if (grepl("\\.md5$", destfile)) { |
| 47 | + writeLines("expected_checksum", destfile) |
| 48 | + } else { |
| 49 | + writeLines("some content", destfile) |
| 50 | + } |
| 51 | + }, |
| 52 | + .package = "curl" |
| 53 | + ) |
| 54 | + |
| 55 | + local_mocked_bindings( |
| 56 | + md5sum = function(files) setNames("actual_checksum_mismatch", files), |
| 57 | + .package = "tools" |
| 58 | + ) |
| 59 | + |
| 60 | + expect_error( |
| 61 | + java_download(version = "21", cache_path = local_cache_path, quiet = TRUE), |
| 62 | + "MD5 checksum mismatch" |
| 63 | + ) |
| 64 | +}) |
| 65 | + |
| 66 | +test_that("java_download skips download if file exists and force is FALSE", { |
| 67 | + local_cache_path <- withr::local_tempdir() |
| 68 | + dest_dir <- file.path(local_cache_path, "distrib") |
| 69 | + dir.create(dest_dir, recursive = TRUE) |
| 70 | + |
| 71 | + # MOCK THE SLOW NETWORK CALL |
| 72 | + local_mocked_bindings( |
| 73 | + java_valid_versions = function(...) c("8", "11", "17", "21") |
| 74 | + ) |
| 75 | + |
| 76 | + platform <- platform_detect()$os |
| 77 | + arch <- platform_detect()$arch |
| 78 | + java_urls <- java_urls_load() |
| 79 | + url_template <- java_urls[["Corretto"]][[platform]][[arch]] |
| 80 | + url <- gsub("\\{version\\}", "21", url_template) |
| 81 | + expected_file_path <- file.path(dest_dir, basename(url)) |
| 82 | + file.create(expected_file_path) |
| 83 | + |
| 84 | + local_mocked_bindings( |
| 85 | + curl_download = function(...) { |
| 86 | + stop("curl_download should not have been called!") |
| 87 | + }, |
| 88 | + .package = "curl" |
| 89 | + ) |
| 90 | + |
| 91 | + result <- java_download( |
| 92 | + version = "21", |
| 93 | + cache_path = local_cache_path, |
| 94 | + platform = platform, |
| 95 | + arch = arch, |
| 96 | + quiet = TRUE, |
| 97 | + force = FALSE |
| 98 | + ) |
| 99 | + |
| 100 | + expect_equal(normalizePath(result), normalizePath(expected_file_path)) |
| 101 | +}) |
| 102 | + |
| 103 | + |
| 104 | +test_that("java_download overwrites if file exists and force is TRUE", { |
| 105 | + local_cache_path <- withr::local_tempdir() |
| 106 | + dest_dir <- file.path(local_cache_path, "distrib") |
| 107 | + dir.create(dest_dir, recursive = TRUE) |
| 108 | + |
| 109 | + # MOCK THE SLOW NETWORK CALL |
| 110 | + local_mocked_bindings( |
| 111 | + java_valid_versions = function(...) c("8", "11", "17", "21") |
| 112 | + ) |
| 113 | + |
| 114 | + platform <- platform_detect()$os |
| 115 | + arch <- platform_detect()$arch |
| 116 | + java_urls <- java_urls_load() |
| 117 | + url_template <- java_urls[["Corretto"]][[platform]][[arch]] |
| 118 | + url <- gsub("\\{version\\}", "21", url_template) |
| 119 | + expected_file_path <- file.path(dest_dir, basename(url)) |
| 120 | + writeLines("old content", expected_file_path) |
| 121 | + |
| 122 | + curl_call_count <- 0 |
| 123 | + local_mocked_bindings( |
| 124 | + curl_download = function(url, destfile, ...) { |
| 125 | + curl_call_count <<- curl_call_count + 1 |
| 126 | + if (grepl("\\.md5$", destfile)) { |
| 127 | + writeLines("new_checksum", destfile) |
| 128 | + } else { |
| 129 | + writeLines("new content", destfile) |
| 130 | + } |
| 131 | + }, |
| 132 | + .package = "curl" |
| 133 | + ) |
| 134 | + |
| 135 | + local_mocked_bindings( |
| 136 | + md5sum = function(files) setNames("new_checksum", files), |
| 137 | + .package = "tools" |
| 138 | + ) |
| 139 | + |
| 140 | + java_download( |
| 141 | + version = "21", |
| 142 | + cache_path = local_cache_path, |
| 143 | + platform = platform, |
| 144 | + arch = arch, |
| 145 | + quiet = TRUE, |
| 146 | + force = TRUE |
| 147 | + ) |
| 148 | + |
| 149 | + expect_equal(curl_call_count, 2) |
| 150 | + expect_equal(readLines(expected_file_path), "new content") |
| 151 | +}) |
0 commit comments