|
| 1 | +# Helper function to mock global dependencies for java_install tests |
| 2 | +mock_java_globals <- function(env = parent.frame()) { |
| 3 | + # We use assignInNamespace because local_mocked_bindings was failing to mock |
| 4 | + # the functions in the package namespace reliably in this context. |
| 5 | + |
| 6 | + # Mock java_valid_versions |
| 7 | + # It is internal, but assignInNamespace handles it. |
| 8 | + if (exists("java_valid_versions", envir = asNamespace("rJavaEnv"))) { |
| 9 | + original_java_valid_versions <- get( |
| 10 | + "java_valid_versions", |
| 11 | + envir = asNamespace("rJavaEnv") |
| 12 | + ) |
| 13 | + assignInNamespace( |
| 14 | + "java_valid_versions", |
| 15 | + function(...) c("8", "11", "17", "21"), |
| 16 | + ns = "rJavaEnv" |
| 17 | + ) |
| 18 | + withr::defer( |
| 19 | + assignInNamespace( |
| 20 | + "java_valid_versions", |
| 21 | + original_java_valid_versions, |
| 22 | + ns = "rJavaEnv" |
| 23 | + ), |
| 24 | + envir = env |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + # Mock rje_consent_check |
| 29 | + if (exists("rje_consent_check", envir = asNamespace("rJavaEnv"))) { |
| 30 | + original_rje_consent_check <- get( |
| 31 | + "rje_consent_check", |
| 32 | + envir = asNamespace("rJavaEnv") |
| 33 | + ) |
| 34 | + assignInNamespace("rje_consent_check", function() TRUE, ns = "rJavaEnv") |
| 35 | + withr::defer( |
| 36 | + assignInNamespace( |
| 37 | + "rje_consent_check", |
| 38 | + original_rje_consent_check, |
| 39 | + ns = "rJavaEnv" |
| 40 | + ), |
| 41 | + envir = env |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + # Mock java_unpack |
| 46 | + if (exists("java_unpack", envir = asNamespace("rJavaEnv"))) { |
| 47 | + original_java_unpack <- get("java_unpack", envir = asNamespace("rJavaEnv")) |
| 48 | + |
| 49 | + mock_unpack <- function(java_distrib_path, ...) { |
| 50 | + filename <- basename(java_distrib_path) |
| 51 | + parts <- strsplit(gsub("\\.tar\\.gz|\\.zip", "", filename), "-")[[1]] |
| 52 | + version <- parts[parts %in% c("8", "11", "17", "21")][1] |
| 53 | + arch <- parts[parts %in% c("x64", "aarch64")][1] |
| 54 | + platform <- parts[parts %in% c("linux", "windows", "macos")][1] |
| 55 | + |
| 56 | + # Use a generic, hardcoded root path instead of calling getOption(). |
| 57 | + # This makes the mock independent of the state being tested. |
| 58 | + file.path( |
| 59 | + "/mock/cache/path", |
| 60 | + "installed", |
| 61 | + platform, |
| 62 | + arch, |
| 63 | + version |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + assignInNamespace("java_unpack", mock_unpack, ns = "rJavaEnv") |
| 68 | + withr::defer( |
| 69 | + assignInNamespace("java_unpack", original_java_unpack, ns = "rJavaEnv"), |
| 70 | + envir = env |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
0 commit comments