Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
181 changes: 181 additions & 0 deletions tests/testthat/test-internal_utilities.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Tests for platform_detect() and rje_consent()

test_that("platform_detect returns correct structure", {
res <- platform_detect(quiet = TRUE)

expect_type(res, "list")
expect_named(res, c("os", "arch"))
expect_true(res$os %in% c("linux", "windows", "macos"))
expect_true(res$arch %in% c("x64", "x86", "aarch64"))
})

test_that("platform_detect returns correct OS for current platform", {
res <- platform_detect(quiet = TRUE)
sys_info <- Sys.info()

expected_os <- switch(
tolower(sys_info["sysname"]),
"windows" = "windows",
"linux" = "linux",
"darwin" = "macos"
)

expect_equal(res$os, expected_os)
})

test_that("platform_detect respects R_ARCH for Windows architecture", {
skip_on_os("mac")
skip_on_os("linux")

# Test x64 architecture via R_ARCH
withr::local_envvar(R_ARCH = "/x64")
res <- platform_detect(quiet = TRUE)
expect_equal(res$arch, "x64")

# Test x86 architecture via R_ARCH
withr::local_envvar(R_ARCH = "/i386")
res <- platform_detect(quiet = TRUE)
expect_equal(res$arch, "x86")
})

test_that("platform_detect prints messages when not quiet", {
expect_message(
platform_detect(quiet = FALSE),
"Detected platform"
)
})

test_that("rje_consent returns TRUE when option is already set", {
withr::local_options(list(rJavaEnv.consent = TRUE))

expect_message(
result <- rje_consent(),
"already been provided"
)
expect_true(result)
})

test_that("rje_consent returns TRUE when cache directory exists", {
cache_dir <- withr::local_tempdir()
withr::local_options(list(
rJavaEnv.consent = FALSE,
rJavaEnv.cache_path = cache_dir
))

expect_message(
result <- rje_consent(),
"already been provided"
)
expect_true(result)
})

test_that("rje_consent handles user typing 'yes'", {
cache_dir <- file.path(withr::local_tempdir(), "nonexistent")
withr::local_options(list(
rJavaEnv.consent = FALSE,
rJavaEnv.cache_path = cache_dir
))

# Mock readline to return "yes"
local_mocked_bindings(readline = function(...) "yes", .package = "base")

expect_message(
result <- rje_consent(),
"Consent has been granted"
)
expect_true(getOption("rJavaEnv.consent"))
})

test_that("rje_consent handles user typing 'no'", {
cache_dir <- file.path(withr::local_tempdir(), "nonexistent")
withr::local_options(list(
rJavaEnv.consent = FALSE,
rJavaEnv.cache_path = cache_dir
))

# Mock readline to return "no"
local_mocked_bindings(readline = function(...) "no", .package = "base")

expect_error(
rje_consent(),
"Consent was not provided"
)
})

test_that("rje_consent accepts 'provided = TRUE' argument", {
cache_dir <- file.path(withr::local_tempdir(), "nonexistent")
withr::local_options(list(
rJavaEnv.consent = FALSE,
rJavaEnv.cache_path = cache_dir
))

expect_message(
result <- rje_consent(provided = TRUE),
"Consent has been granted"
)
expect_true(getOption("rJavaEnv.consent"))
})

test_that("rje_consent_check returns TRUE when consent option is set", {
withr::local_options(list(rJavaEnv.consent = TRUE))

expect_true(rje_consent_check())
})

test_that("rje_consent_check returns TRUE when cache directory exists", {
cache_dir <- withr::local_tempdir()
withr::local_options(list(
rJavaEnv.consent = FALSE,
rJavaEnv.cache_path = cache_dir
))

expect_true(rje_consent_check())
})

test_that("rje_consent_check grants implicit consent in CI environments", {
cache_dir <- file.path(withr::local_tempdir(), "nonexistent")
withr::local_options(list(
rJavaEnv.consent = FALSE,
rJavaEnv.cache_path = cache_dir
))
withr::local_envvar(CI = "true")

expect_true(rje_consent_check())
expect_true(getOption("rJavaEnv.consent"))
})

test_that("rje_consent_check grants implicit consent in GitHub Actions", {
cache_dir <- file.path(withr::local_tempdir(), "nonexistent")
withr::local_options(list(
rJavaEnv.consent = FALSE,
rJavaEnv.cache_path = cache_dir
))
withr::local_envvar(GITHUB_ACTION = "test-action")

expect_true(rje_consent_check())
expect_true(getOption("rJavaEnv.consent"))
})

test_that("rje_envvar_exists correctly detects environment variables", {
withr::local_envvar(TEST_VAR_EXISTS = "value")

expect_true(rje_envvar_exists("TEST_VAR_EXISTS"))
expect_false(rje_envvar_exists("TEST_VAR_DOES_NOT_EXIST_12345"))
})

test_that("java_urls_load returns correct structure", {
urls <- java_urls_load()

expect_type(urls, "list")
expect_true("Corretto" %in% names(urls))
})

test_that("platform_detect handles common architectures", {
# This test verifies the current platform returns one of the known architectures
res <- platform_detect(quiet = TRUE)

# The function should not error on the current machine

expect_true(length(res$arch) == 1)
expect_true(nchar(res$arch) > 0)
})
38 changes: 38 additions & 0 deletions tests/testthat/test-internal_utilities_extra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test_that("java_urls_load loads JSON correctly", {
# Real test of the JSON file included in the package
urls <- rJavaEnv:::java_urls_load()
expect_type(urls, "list")
expect_true("Corretto" %in% names(urls))
})

test_that("urls_test_all checks URLs without network", {
# Mock internal loader to return small subset
local_mocked_bindings(
java_urls_load = function() list(
TestDist = list(
linux = list(x64 = "http://example.com/jdk-{version}.tar.gz")
)
),
.package = "rJavaEnv"
)

# Mock curl to avoid network
local_mocked_bindings(
curl_fetch_memory = function(...) list(status_code = 200),
.package = "curl"
)

res <- rJavaEnv:::urls_test_all()
expect_type(res, "list")
expect_equal(res[["TestDist-linux-x64"]]$status, 200)
})

test_that("java_version_check_rscript function exists", {
# We cannot safely test this function without loading rJava, which crashes when
# Java is not configured. Testing the error path is also unsafe because mocking
# base functions like Sys.setenv or list.files interferes with testthat's own operations.
# Instead, we just verify the function exists and has correct structure.

expect_true(exists("java_version_check_rscript", where = asNamespace("rJavaEnv")))
expect_type(rJavaEnv:::java_version_check_rscript, "closure")
})
132 changes: 132 additions & 0 deletions tests/testthat/test-internal_utilities_version.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Unit tests for parsing Java version output
# These tests focus on verifying that Java version strings are parsed correctly

test_that("java_check_version_system regex extracts OpenJDK 17 version correctly", {
# We test the regex parsing logic that java_check_version_system uses
# by simulating what it does with the java version output

java_ver_string <- 'openjdk version "17.0.1" 2021-10-19'
matches <- regexec(
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
java_ver_string
)
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]

expect_equal(major_java_ver, "17")
})

test_that("java_check_version_system regex extracts Java 11 version correctly", {
java_ver_string <- 'openjdk version "11.0.13" 2021-10-19'
matches <- regexec(
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
java_ver_string
)
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]

expect_equal(major_java_ver, "11")
})

test_that("java_check_version_system regex extracts Java 8 version correctly", {
java_ver_string <- 'java version "1.8.0_292"'
matches <- regexec(
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
java_ver_string
)
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]

# Java 8 returns "1", which should be converted to "8"
expect_equal(major_java_ver, "1")

# The function then converts "1" to "8"
if (major_java_ver == "1") {
major_java_ver <- "8"
}
expect_equal(major_java_ver, "8")
})

test_that("java_check_version_system regex extracts Java 21 version correctly", {
java_ver_string <- 'openjdk version "21.0.1" 2023-10-17'
matches <- regexec(
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
java_ver_string
)
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]

expect_equal(major_java_ver, "21")
})

test_that("java_version_check_rscript output parsing for version 17", {
# Test the parsing of output from java_version_check_rscript
output <- c("rJava and other rJava/Java-based packages will use Java version: \"17.0.1\"")
cleaned_output <- cli::ansi_strip(output[1])
major_java_ver <- sub('.*version: \\"([0-9]+).*', '\\1', cleaned_output)

if (major_java_ver == "1") {
major_java_ver <- "8"
}

expect_equal(major_java_ver, "17")
})

test_that("java_version_check_rscript output parsing for version 1.8", {
output <- c("rJava and other rJava/Java-based packages will use Java version: \"1.8.0\"")
cleaned_output <- cli::ansi_strip(output[1])
major_java_ver <- sub('.*version: \\"([0-9]+).*', '\\1', cleaned_output)

# For Java 8, version string starts with "1"
expect_equal(major_java_ver, "1")

if (major_java_ver == "1") {
major_java_ver <- "8"
}
expect_equal(major_java_ver, "8")
})

test_that("java_check_version_rjava silent behavior", {
# This test verifies that when rJava is not installed, the function returns FALSE
local_mocked_bindings(
find.package = function(package, quiet = TRUE) {
# Simulate rJava not being found
character(0)
},
.package = "base"
)

result <- java_check_version_rjava(java_home = "/mock/java", quiet = TRUE)
expect_false(result)
})

test_that("java_check_version_cmd with quiet = TRUE doesn't throw", {
# When JAVA_HOME is not set and we don't provide a java_home
withr::local_envvar(c("JAVA_HOME" = NA))

result <- java_check_version_cmd(java_home = NULL, quiet = TRUE)
# Should return FALSE gracefully without errors
expect_false(result)
})

test_that("Sys.which mock works correctly", {
# Verify our mocking approach works
local_mocked_bindings(
Sys.which = function(x) {
if (x == "java") "/usr/bin/java" else ""
},
.package = "base"
)

expect_equal(Sys.which("java"), "/usr/bin/java")
expect_equal(Sys.which("nonexistent"), "")
})

test_that("system2 mock can return version output", {
# Verify our mocking approach works for system2
local_mocked_bindings(
system2 = function(cmd, args = NULL, ...) {
c('openjdk version "11.0.1"')
},
.package = "base"
)

result <- system2("java", args = "-version")
expect_equal(result[1], 'openjdk version "11.0.1"')
})
Loading
Loading