|
| 1 | +# Mocked unit tests for java_install() |
| 2 | +# NOTE: Common mocks (java_valid_versions, rje_consent_check, java_unpack) |
| 3 | +# are defined globally in the setup-mock-globals.R file. |
| 4 | + |
| 5 | +test_that("java_install succeeds with symlink on Unix-like systems", { |
| 6 | + # This test is for non-Windows behavior |
| 7 | + skip_on_os("windows") |
| 8 | + |
| 9 | + local_proj_path <- withr::local_tempdir(pattern = "project") |
| 10 | + local_cache_path <- withr::local_tempdir(pattern = "cache") |
| 11 | + withr::local_options(rJavaEnv.cache_path = local_cache_path) |
| 12 | + |
| 13 | + fake_distrib_path <- file.path( |
| 14 | + local_cache_path, |
| 15 | + "distrib", |
| 16 | + "amazon-corretto-21-x64-linux-jdk.tar.gz" |
| 17 | + ) |
| 18 | + # The global mock will generate the unpacked path based on the distrib path |
| 19 | + fake_unpacked_path <- java_unpack(fake_distrib_path) |
| 20 | + expected_symlink_path <- file.path( |
| 21 | + local_proj_path, |
| 22 | + "rjavaenv", |
| 23 | + "linux", |
| 24 | + "x64", |
| 25 | + "21" |
| 26 | + ) |
| 27 | + |
| 28 | + env_set_calls <- 0 |
| 29 | + local_mocked_bindings( |
| 30 | + java_env_set = function(...) { |
| 31 | + env_set_calls <<- env_set_calls + 1 |
| 32 | + } |
| 33 | + ) |
| 34 | + |
| 35 | + symlink_calls <- 0 |
| 36 | + symlink_args <- list() |
| 37 | + local_mocked_bindings( |
| 38 | + file.symlink = function(from, to) { |
| 39 | + symlink_calls <<- symlink_calls + 1 |
| 40 | + symlink_args <<- list(from = from, to = to) |
| 41 | + }, |
| 42 | + .package = "base" |
| 43 | + ) |
| 44 | + |
| 45 | + return_val <- java_install( |
| 46 | + java_distrib_path = fake_distrib_path, |
| 47 | + project_path = local_proj_path, |
| 48 | + quiet = TRUE |
| 49 | + ) |
| 50 | + |
| 51 | + expect_equal(env_set_calls, 1) |
| 52 | + expect_equal(symlink_calls, 1) |
| 53 | + expect_equal(symlink_args$from, fake_unpacked_path) |
| 54 | + expect_equal(symlink_args$to, expected_symlink_path) |
| 55 | + expect_equal(return_val, fake_unpacked_path) |
| 56 | +}) |
| 57 | + |
| 58 | + |
| 59 | +test_that("java_install falls back to file.copy when symlink fails on Unix", { |
| 60 | + skip_on_os("windows") |
| 61 | + |
| 62 | + local_proj_path <- withr::local_tempdir() |
| 63 | + local_cache_path <- withr::local_tempdir() |
| 64 | + withr::local_options(rJavaEnv.cache_path = local_cache_path) |
| 65 | + |
| 66 | + fake_distrib_path <- file.path( |
| 67 | + local_cache_path, |
| 68 | + "distrib", |
| 69 | + "amazon-corretto-21-x64-linux-jdk.tar.gz" |
| 70 | + ) |
| 71 | + |
| 72 | + local_mocked_bindings(java_env_set = function(...) TRUE) |
| 73 | + |
| 74 | + local_mocked_bindings( |
| 75 | + file.symlink = function(from, to) stop("Permission denied"), |
| 76 | + .package = "base" |
| 77 | + ) |
| 78 | + |
| 79 | + copy_calls <- 0 |
| 80 | + local_mocked_bindings( |
| 81 | + file.copy = function(from, to, ...) { |
| 82 | + if (grepl(basename(local_proj_path), to, fixed = TRUE)) { |
| 83 | + copy_calls <<- copy_calls + 1 |
| 84 | + } |
| 85 | + TRUE |
| 86 | + }, |
| 87 | + .package = "base" |
| 88 | + ) |
| 89 | + |
| 90 | + java_install( |
| 91 | + java_distrib_path = fake_distrib_path, |
| 92 | + project_path = local_proj_path, |
| 93 | + quiet = TRUE |
| 94 | + ) |
| 95 | + |
| 96 | + expect_equal(copy_calls, 1) |
| 97 | +}) |
| 98 | + |
| 99 | + |
| 100 | +test_that("java_install respects autoset_java_env = FALSE", { |
| 101 | + local_proj_path <- withr::local_tempdir() |
| 102 | + local_cache_path <- withr::local_tempdir() |
| 103 | + withr::local_options(rJavaEnv.cache_path = local_cache_path) |
| 104 | + |
| 105 | + fake_distrib_path <- file.path( |
| 106 | + local_cache_path, |
| 107 | + "distrib", |
| 108 | + "amazon-corretto-21-x64-linux-jdk.tar.gz" |
| 109 | + ) |
| 110 | + |
| 111 | + # CORRECTED: Add an explicit mock for java_unpack for this test |
| 112 | + # to prevent the real function from being called and issuing a warning. |
| 113 | + local_mocked_bindings( |
| 114 | + java_unpack = function(...) { |
| 115 | + # Return a simple, predictable path string |
| 116 | + "/mock/cache/path/installed/linux/x64/21" |
| 117 | + } |
| 118 | + ) |
| 119 | + |
| 120 | + # Mock file.symlink to succeed |
| 121 | + local_mocked_bindings( |
| 122 | + file.symlink = function(...) TRUE, |
| 123 | + .package = "base" |
| 124 | + ) |
| 125 | + |
| 126 | + # Mock java_env_set to fail the test if it's ever called |
| 127 | + local_mocked_bindings( |
| 128 | + java_env_set = function(...) { |
| 129 | + stop("java_env_set should not have been called!") |
| 130 | + } |
| 131 | + ) |
| 132 | + |
| 133 | + # This test will now pass silently because java_unpack is properly mocked. |
| 134 | + expect_silent( |
| 135 | + java_install( |
| 136 | + java_distrib_path = fake_distrib_path, |
| 137 | + project_path = local_proj_path, |
| 138 | + autoset_java_env = FALSE, |
| 139 | + quiet = TRUE |
| 140 | + ) |
| 141 | + ) |
| 142 | +}) |
| 143 | + |
| 144 | + |
| 145 | +test_that("java_install succeeds with mklink junction on Windows", { |
| 146 | + skip_on_os("mac") |
| 147 | + skip_on_os("linux") |
| 148 | + skip_on_os("solaris") |
| 149 | + |
| 150 | + local_proj_path <- withr::local_tempdir() |
| 151 | + local_cache_path <- withr::local_tempdir() |
| 152 | + withr::local_options(rJavaEnv.cache_path = local_cache_path) |
| 153 | + |
| 154 | + fake_distrib_path <- file.path( |
| 155 | + local_cache_path, |
| 156 | + "distrib", |
| 157 | + "amazon-corretto-21-x64-windows-jdk.zip" |
| 158 | + ) |
| 159 | + |
| 160 | + local_mocked_bindings(java_env_set = function(...) TRUE) |
| 161 | + |
| 162 | + system2_args <- list() |
| 163 | + local_mocked_bindings( |
| 164 | + system2 = function(command, args, ...) { |
| 165 | + system2_args <<- list(command = command, args = args) |
| 166 | + "Junction created for ... " # Simulate success message |
| 167 | + }, |
| 168 | + .package = "base" |
| 169 | + ) |
| 170 | + |
| 171 | + java_install( |
| 172 | + java_distrib_path = fake_distrib_path, |
| 173 | + project_path = local_proj_path, |
| 174 | + quiet = TRUE |
| 175 | + ) |
| 176 | + |
| 177 | + expect_equal(system2_args$command, "cmd.exe") |
| 178 | + expect_true(grepl("mklink /J", system2_args$args[2])) |
| 179 | +}) |
0 commit comments