|
| 1 | +# Tests for platform_detect() and rje_consent() |
| 2 | + |
| 3 | +test_that("platform_detect returns correct structure", { |
| 4 | + res <- platform_detect(quiet = TRUE) |
| 5 | + |
| 6 | + expect_type(res, "list") |
| 7 | + expect_named(res, c("os", "arch")) |
| 8 | + expect_true(res$os %in% c("linux", "windows", "macos")) |
| 9 | + expect_true(res$arch %in% c("x64", "x86", "aarch64")) |
| 10 | +}) |
| 11 | + |
| 12 | +test_that("platform_detect returns correct OS for current platform", { |
| 13 | + res <- platform_detect(quiet = TRUE) |
| 14 | + sys_info <- Sys.info() |
| 15 | + |
| 16 | + expected_os <- switch( |
| 17 | + tolower(sys_info["sysname"]), |
| 18 | + "windows" = "windows", |
| 19 | + "linux" = "linux", |
| 20 | + "darwin" = "macos" |
| 21 | + ) |
| 22 | + |
| 23 | + expect_equal(res$os, expected_os) |
| 24 | +}) |
| 25 | + |
| 26 | +test_that("platform_detect respects R_ARCH for Windows architecture", { |
| 27 | + skip_on_os("mac") |
| 28 | + skip_on_os("linux") |
| 29 | + |
| 30 | + # Test x64 architecture via R_ARCH |
| 31 | + withr::local_envvar(R_ARCH = "/x64") |
| 32 | + res <- platform_detect(quiet = TRUE) |
| 33 | + expect_equal(res$arch, "x64") |
| 34 | + |
| 35 | + # Test x86 architecture via R_ARCH |
| 36 | + withr::local_envvar(R_ARCH = "/i386") |
| 37 | + res <- platform_detect(quiet = TRUE) |
| 38 | + expect_equal(res$arch, "x86") |
| 39 | +}) |
| 40 | + |
| 41 | +test_that("platform_detect prints messages when not quiet", { |
| 42 | + expect_message( |
| 43 | + platform_detect(quiet = FALSE), |
| 44 | + "Detected platform" |
| 45 | + ) |
| 46 | +}) |
| 47 | + |
| 48 | +test_that("rje_consent returns TRUE when option is already set", { |
| 49 | + withr::local_options(list(rJavaEnv.consent = TRUE)) |
| 50 | + |
| 51 | + expect_message( |
| 52 | + result <- rje_consent(), |
| 53 | + "already been provided" |
| 54 | + ) |
| 55 | + expect_true(result) |
| 56 | +}) |
| 57 | + |
| 58 | +test_that("rje_consent returns TRUE when cache directory exists", { |
| 59 | + cache_dir <- withr::local_tempdir() |
| 60 | + withr::local_options(list( |
| 61 | + rJavaEnv.consent = FALSE, |
| 62 | + rJavaEnv.cache_path = cache_dir |
| 63 | + )) |
| 64 | + |
| 65 | + expect_message( |
| 66 | + result <- rje_consent(), |
| 67 | + "already been provided" |
| 68 | + ) |
| 69 | + expect_true(result) |
| 70 | +}) |
| 71 | + |
| 72 | +test_that("rje_consent handles user typing 'yes'", { |
| 73 | + cache_dir <- file.path(withr::local_tempdir(), "nonexistent") |
| 74 | + withr::local_options(list( |
| 75 | + rJavaEnv.consent = FALSE, |
| 76 | + rJavaEnv.cache_path = cache_dir |
| 77 | + )) |
| 78 | + |
| 79 | + # Mock readline to return "yes" |
| 80 | + local_mocked_bindings(readline = function(...) "yes", .package = "base") |
| 81 | + |
| 82 | + expect_message( |
| 83 | + result <- rje_consent(), |
| 84 | + "Consent has been granted" |
| 85 | + ) |
| 86 | + expect_true(getOption("rJavaEnv.consent")) |
| 87 | +}) |
| 88 | + |
| 89 | +test_that("rje_consent handles user typing 'no'", { |
| 90 | + cache_dir <- file.path(withr::local_tempdir(), "nonexistent") |
| 91 | + withr::local_options(list( |
| 92 | + rJavaEnv.consent = FALSE, |
| 93 | + rJavaEnv.cache_path = cache_dir |
| 94 | + )) |
| 95 | + |
| 96 | + # Mock readline to return "no" |
| 97 | + local_mocked_bindings(readline = function(...) "no", .package = "base") |
| 98 | + |
| 99 | + expect_error( |
| 100 | + rje_consent(), |
| 101 | + "Consent was not provided" |
| 102 | + ) |
| 103 | +}) |
| 104 | + |
| 105 | +test_that("rje_consent accepts 'provided = TRUE' argument", { |
| 106 | + cache_dir <- file.path(withr::local_tempdir(), "nonexistent") |
| 107 | + withr::local_options(list( |
| 108 | + rJavaEnv.consent = FALSE, |
| 109 | + rJavaEnv.cache_path = cache_dir |
| 110 | + )) |
| 111 | + |
| 112 | + expect_message( |
| 113 | + result <- rje_consent(provided = TRUE), |
| 114 | + "Consent has been granted" |
| 115 | + ) |
| 116 | + expect_true(getOption("rJavaEnv.consent")) |
| 117 | +}) |
| 118 | + |
| 119 | +test_that("rje_consent_check returns TRUE when consent option is set", { |
| 120 | + withr::local_options(list(rJavaEnv.consent = TRUE)) |
| 121 | + |
| 122 | + expect_true(rje_consent_check()) |
| 123 | +}) |
| 124 | + |
| 125 | +test_that("rje_consent_check returns TRUE when cache directory exists", { |
| 126 | + cache_dir <- withr::local_tempdir() |
| 127 | + withr::local_options(list( |
| 128 | + rJavaEnv.consent = FALSE, |
| 129 | + rJavaEnv.cache_path = cache_dir |
| 130 | + )) |
| 131 | + |
| 132 | + expect_true(rje_consent_check()) |
| 133 | +}) |
| 134 | + |
| 135 | +test_that("rje_consent_check grants implicit consent in CI environments", { |
| 136 | + cache_dir <- file.path(withr::local_tempdir(), "nonexistent") |
| 137 | + withr::local_options(list( |
| 138 | + rJavaEnv.consent = FALSE, |
| 139 | + rJavaEnv.cache_path = cache_dir |
| 140 | + )) |
| 141 | + withr::local_envvar(CI = "true") |
| 142 | + |
| 143 | + expect_true(rje_consent_check()) |
| 144 | + expect_true(getOption("rJavaEnv.consent")) |
| 145 | +}) |
| 146 | + |
| 147 | +test_that("rje_consent_check grants implicit consent in GitHub Actions", { |
| 148 | + cache_dir <- file.path(withr::local_tempdir(), "nonexistent") |
| 149 | + withr::local_options(list( |
| 150 | + rJavaEnv.consent = FALSE, |
| 151 | + rJavaEnv.cache_path = cache_dir |
| 152 | + )) |
| 153 | + withr::local_envvar(GITHUB_ACTION = "test-action") |
| 154 | + |
| 155 | + expect_true(rje_consent_check()) |
| 156 | + expect_true(getOption("rJavaEnv.consent")) |
| 157 | +}) |
| 158 | + |
| 159 | +test_that("rje_envvar_exists correctly detects environment variables", { |
| 160 | + withr::local_envvar(TEST_VAR_EXISTS = "value") |
| 161 | + |
| 162 | + expect_true(rje_envvar_exists("TEST_VAR_EXISTS")) |
| 163 | + expect_false(rje_envvar_exists("TEST_VAR_DOES_NOT_EXIST_12345")) |
| 164 | +}) |
| 165 | + |
| 166 | +test_that("java_urls_load returns correct structure", { |
| 167 | + urls <- java_urls_load() |
| 168 | + |
| 169 | + expect_type(urls, "list") |
| 170 | + expect_true("Corretto" %in% names(urls)) |
| 171 | +}) |
| 172 | + |
| 173 | +test_that("platform_detect handles common architectures", { |
| 174 | + # This test verifies the current platform returns one of the known architectures |
| 175 | + res <- platform_detect(quiet = TRUE) |
| 176 | + |
| 177 | + # The function should not error on the current machine |
| 178 | + |
| 179 | + expect_true(length(res$arch) == 1) |
| 180 | + expect_true(nchar(res$arch) > 0) |
| 181 | +}) |
0 commit comments