|
| 1 | +#' Test that java_check_version_rjava() correctly constructs the subprocess script |
| 2 | +#' without duplication issues when .libPaths() contains multiple paths. |
| 3 | + |
| 4 | +test_that("._java_version_check_rjava_impl_original handles multi-line .libPaths correctly", { |
| 5 | + # Mock a scenario where .libPaths() has multiple long paths |
| 6 | + # This previously caused duplication in paste0() because deparse() returned a vector. |
| 7 | + mock_paths <- c( |
| 8 | + "/usr/lib/R/library", |
| 9 | + "/usr/local/lib/R/site-library", |
| 10 | + "/home/user/R/x86_64-pc-linux-gnu-library/4.5" |
| 11 | + ) |
| 12 | + |
| 13 | + captured_script <- NULL |
| 14 | + |
| 15 | + # Mock get_libjvm_path to return a simple known string |
| 16 | + local_mocked_bindings( |
| 17 | + get_libjvm_path = function(...) "/mock/libjvm.so", |
| 18 | + .package = "rJavaEnv" |
| 19 | + ) |
| 20 | + |
| 21 | + local_mocked_bindings( |
| 22 | + .libPaths = function(...) mock_paths, |
| 23 | + system2 = function(command, args, ...) { |
| 24 | + # The first argument in args is the script file path |
| 25 | + if (file.exists(args[1])) { |
| 26 | + captured_script <<- readLines(args[1]) |
| 27 | + } |
| 28 | + # Return valid dummy output |
| 29 | + return(c( |
| 30 | + "rJava and other rJava/Java-based packages will use Java version: \"21\"" |
| 31 | + )) |
| 32 | + }, |
| 33 | + .package = "base" |
| 34 | + ) |
| 35 | + |
| 36 | + # Call the internal function |
| 37 | + result <- rJavaEnv:::._java_version_check_rjava_impl_original( |
| 38 | + java_home = "/mock/java" |
| 39 | + ) |
| 40 | + |
| 41 | + # Assertions on the captured script |
| 42 | + # Avoid expect_* functions that might trigger 'waldo' dependency issues. |
| 43 | + # Using simple R logic that throws an error on failure, which testthat will catch. |
| 44 | + if (is.null(captured_script)) { |
| 45 | + stop("Captured script is NULL") |
| 46 | + } |
| 47 | + |
| 48 | + # 1. Check for duplication of function definitions |
| 49 | + def_count <- sum(grepl("java_version_check <- function", captured_script)) |
| 50 | + if (!identical(as.numeric(def_count), 1)) { |
| 51 | + stop(sprintf( |
| 52 | + "The function definition should appear exactly once, but found %d", |
| 53 | + def_count |
| 54 | + )) |
| 55 | + } |
| 56 | + |
| 57 | + # 2. Check that .libPaths() contains all paths in a valid call |
| 58 | + script_text <- paste(captured_script, collapse = "\n") |
| 59 | + if (!grepl("\\.libPaths\\(c\\(", script_text)) { |
| 60 | + stop("Should use c() for multiple library paths.") |
| 61 | + } |
| 62 | + if (!grepl("/home/user/R/x86_64-pc-linux-gnu-library/4.5", script_text)) { |
| 63 | + stop("Last path should be present.") |
| 64 | + } |
| 65 | + |
| 66 | + # 3. Check for syntax errors (no stray commas at line ends from bad paste) |
| 67 | + if (grepl(", \\)", script_text)) { |
| 68 | + stop("Malformed closing parenthesis found.") |
| 69 | + } |
| 70 | + |
| 71 | + # Final verification: if we reached here, the test passed. |
| 72 | + expect_silent(NULL) |
| 73 | +}) |
0 commit comments