Skip to content

Commit 8a745e4

Browse files
committed
add new tests
1 parent 2fa6f53 commit 8a745e4

11 files changed

+1225
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ Config/testthat/edition: 3
6363
Encoding: UTF-8
6464
Language: en-US
6565
Roxygen: list(markdown = TRUE)
66-
RoxygenNote: 7.3.2
66+
RoxygenNote: 7.3.3
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
test_that("java_urls_load loads JSON correctly", {
2+
# Real test of the JSON file included in the package
3+
urls <- rJavaEnv:::java_urls_load()
4+
expect_type(urls, "list")
5+
expect_true("Corretto" %in% names(urls))
6+
})
7+
8+
test_that("urls_test_all checks URLs without network", {
9+
# Mock internal loader to return small subset
10+
local_mocked_bindings(
11+
java_urls_load = function() list(
12+
TestDist = list(
13+
linux = list(x64 = "http://example.com/jdk-{version}.tar.gz")
14+
)
15+
),
16+
.package = "rJavaEnv"
17+
)
18+
19+
# Mock curl to avoid network
20+
local_mocked_bindings(
21+
curl_fetch_memory = function(...) list(status_code = 200),
22+
.package = "curl"
23+
)
24+
25+
res <- rJavaEnv:::urls_test_all()
26+
expect_type(res, "list")
27+
expect_equal(res[["TestDist-linux-x64"]]$status, 200)
28+
})
29+
30+
test_that("java_version_check_rscript function exists", {
31+
# We cannot safely test this function without loading rJava, which crashes when
32+
# Java is not configured. Testing the error path is also unsafe because mocking
33+
# base functions like Sys.setenv or list.files interferes with testthat's own operations.
34+
# Instead, we just verify the function exists and has correct structure.
35+
36+
expect_true(exists("java_version_check_rscript", where = asNamespace("rJavaEnv")))
37+
expect_type(rJavaEnv:::java_version_check_rscript, "closure")
38+
})
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Unit tests for parsing Java version output
2+
# These tests focus on verifying that Java version strings are parsed correctly
3+
4+
test_that("java_check_version_system regex extracts OpenJDK 17 version correctly", {
5+
# We test the regex parsing logic that java_check_version_system uses
6+
# by simulating what it does with the java version output
7+
8+
java_ver_string <- 'openjdk version "17.0.1" 2021-10-19'
9+
matches <- regexec(
10+
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
11+
java_ver_string
12+
)
13+
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]
14+
15+
expect_equal(major_java_ver, "17")
16+
})
17+
18+
test_that("java_check_version_system regex extracts Java 11 version correctly", {
19+
java_ver_string <- 'openjdk version "11.0.13" 2021-10-19'
20+
matches <- regexec(
21+
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
22+
java_ver_string
23+
)
24+
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]
25+
26+
expect_equal(major_java_ver, "11")
27+
})
28+
29+
test_that("java_check_version_system regex extracts Java 8 version correctly", {
30+
java_ver_string <- 'java version "1.8.0_292"'
31+
matches <- regexec(
32+
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
33+
java_ver_string
34+
)
35+
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]
36+
37+
# Java 8 returns "1", which should be converted to "8"
38+
expect_equal(major_java_ver, "1")
39+
40+
# The function then converts "1" to "8"
41+
if (major_java_ver == "1") {
42+
major_java_ver <- "8"
43+
}
44+
expect_equal(major_java_ver, "8")
45+
})
46+
47+
test_that("java_check_version_system regex extracts Java 21 version correctly", {
48+
java_ver_string <- 'openjdk version "21.0.1" 2023-10-17'
49+
matches <- regexec(
50+
'(openjdk|java) (version )?(\\\")?([0-9]{1,2})',
51+
java_ver_string
52+
)
53+
major_java_ver <- regmatches(java_ver_string, matches)[[1]][5]
54+
55+
expect_equal(major_java_ver, "21")
56+
})
57+
58+
test_that("java_version_check_rscript output parsing for version 17", {
59+
# Test the parsing of output from java_version_check_rscript
60+
output <- c("rJava and other rJava/Java-based packages will use Java version: \"17.0.1\"")
61+
cleaned_output <- cli::ansi_strip(output[1])
62+
major_java_ver <- sub('.*version: \\"([0-9]+).*', '\\1', cleaned_output)
63+
64+
if (major_java_ver == "1") {
65+
major_java_ver <- "8"
66+
}
67+
68+
expect_equal(major_java_ver, "17")
69+
})
70+
71+
test_that("java_version_check_rscript output parsing for version 1.8", {
72+
output <- c("rJava and other rJava/Java-based packages will use Java version: \"1.8.0\"")
73+
cleaned_output <- cli::ansi_strip(output[1])
74+
major_java_ver <- sub('.*version: \\"([0-9]+).*', '\\1', cleaned_output)
75+
76+
# For Java 8, version string starts with "1"
77+
expect_equal(major_java_ver, "1")
78+
79+
if (major_java_ver == "1") {
80+
major_java_ver <- "8"
81+
}
82+
expect_equal(major_java_ver, "8")
83+
})
84+
85+
test_that("java_check_version_rjava silent behavior", {
86+
# This test verifies that when rJava is not installed, the function returns FALSE
87+
local_mocked_bindings(
88+
find.package = function(package, quiet = TRUE) {
89+
# Simulate rJava not being found
90+
character(0)
91+
},
92+
.package = "base"
93+
)
94+
95+
result <- java_check_version_rjava(java_home = "/mock/java", quiet = TRUE)
96+
expect_false(result)
97+
})
98+
99+
test_that("java_check_version_cmd with quiet = TRUE doesn't throw", {
100+
# When JAVA_HOME is not set and we don't provide a java_home
101+
withr::local_envvar(c("JAVA_HOME" = NA))
102+
103+
result <- java_check_version_cmd(java_home = NULL, quiet = TRUE)
104+
# Should return FALSE gracefully without errors
105+
expect_false(result)
106+
})
107+
108+
test_that("Sys.which mock works correctly", {
109+
# Verify our mocking approach works
110+
local_mocked_bindings(
111+
Sys.which = function(x) {
112+
if (x == "java") "/usr/bin/java" else ""
113+
},
114+
.package = "base"
115+
)
116+
117+
expect_equal(Sys.which("java"), "/usr/bin/java")
118+
expect_equal(Sys.which("nonexistent"), "")
119+
})
120+
121+
test_that("system2 mock can return version output", {
122+
# Verify our mocking approach works for system2
123+
local_mocked_bindings(
124+
system2 = function(cmd, args = NULL, ...) {
125+
c('openjdk version "11.0.1"')
126+
},
127+
.package = "base"
128+
)
129+
130+
result <- system2("java", args = "-version")
131+
expect_equal(result[1], 'openjdk version "11.0.1"')
132+
})

0 commit comments

Comments
 (0)