Skip to content

Commit ae49bc7

Browse files
committed
update fallback version options
1 parent 32bbf70 commit ae49bc7

File tree

4 files changed

+43
-89
lines changed

4 files changed

+43
-89
lines changed

R/internal_utilities.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ platform_detect <- function(quiet = TRUE) {
2020
"x86-64" = "x64",
2121
"x86_64" = "x64",
2222
"i386" = "x86",
23+
"i486" = "x86",
24+
"i586" = "x86",
2325
"i686" = "x86",
2426
"aarch64" = "aarch64",
2527
"arm64" = "aarch64",

R/java_valid_versions.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ java_valid_major_versions_corretto <- function(
116116
},
117117
error = function(e) {
118118
platform_arch <- paste(platform, arch, sep = "_")
119-
getOption(paste0(
120-
"rJavaEnv.fallback_valid_versions_current_platform_",
121-
platform_arch
122-
))
119+
getOption(paste0("rJavaEnv.fallback_valid_versions_", platform_arch))
123120
}
124121
)
125122

R/onLoad.R

Lines changed: 35 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
.onLoad <- function(libname, pkgname) {
2-
# Detect the current platform (OS and architecture)
3-
platform <- platform_detect(quiet = TRUE)
4-
5-
# Select the fallback valid Java versions based on the detected platform
6-
fallback_current <- switch(
7-
paste(platform$os, platform$arch, sep = "_"),
8-
"macos_aarch64" = c(
2+
# First, set all the base rJavaEnv options
3+
op <- options()
4+
op.rJavaEnv <- list(
5+
# Default folder choice (in line with renv package)
6+
rJavaEnv.cache_path = tools::R_user_dir("rJavaEnv", which = "cache"),
7+
rJavaEnv.valid_versions_cache = NULL,
8+
rJavaEnv.valid_versions_timestamp = NULL,
9+
# Fallback lists for various platforms
10+
rJavaEnv.fallback_valid_versions_macos_aarch64 = c(
911
"8",
1012
"11",
1113
"17",
@@ -17,7 +19,7 @@
1719
"23",
1820
"24"
1921
),
20-
"macos_x64" = c(
22+
rJavaEnv.fallback_valid_versions_macos_x64 = c(
2123
"8",
2224
"11",
2325
"15",
@@ -31,7 +33,7 @@
3133
"23",
3234
"24"
3335
),
34-
"linux_aarch64" = c(
36+
rJavaEnv.fallback_valid_versions_linux_aarch64 = c(
3537
"8",
3638
"11",
3739
"15",
@@ -45,7 +47,7 @@
4547
"23",
4648
"24"
4749
),
48-
"linux_x64" = c(
50+
rJavaEnv.fallback_valid_versions_linux_x64 = c(
4951
"8",
5052
"11",
5153
"15",
@@ -59,32 +61,7 @@
5961
"23",
6062
"24"
6163
),
62-
stop("Unsupported platform/architecture combination")
63-
)
64-
65-
# Get current options
66-
op <- options()
67-
68-
# Create list of rJavaEnv options including the current platform valid versions
69-
op.rJavaEnv <- list(
70-
# Default folder choice (in line with renv package)
71-
rJavaEnv.cache_path = tools::R_user_dir("rJavaEnv", which = "cache"),
72-
rJavaEnv.valid_versions_cache = NULL,
73-
rJavaEnv.valid_versions_timestamp = NULL,
74-
# Fallback lists for various platforms
75-
rJavaEnv.fallback_valid_versions_current_platform_macos_aarch64 = c(
76-
"8",
77-
"11",
78-
"17",
79-
"18",
80-
"19",
81-
"20",
82-
"21",
83-
"22",
84-
"23",
85-
"24"
86-
),
87-
rJavaEnv.fallback_valid_versions_current_platform_macos_x64 = c(
64+
rJavaEnv.fallback_valid_versions_windows_x64 = c(
8865
"8",
8966
"11",
9067
"15",
@@ -98,41 +75,33 @@
9875
"23",
9976
"24"
10077
),
101-
rJavaEnv.fallback_valid_versions_current_platform_linux_aarch64 = c(
78+
rJavaEnv.fallback_valid_versions_windows_x86 = c(
10279
"8",
103-
"11",
104-
"15",
105-
"16",
106-
"17",
107-
"18",
108-
"19",
109-
"20",
110-
"21",
111-
"22",
112-
"23",
113-
"24"
114-
),
115-
rJavaEnv.fallback_valid_versions_current_platform_linux_x64 = c(
116-
"8",
117-
"11",
118-
"15",
119-
"16",
120-
"17",
121-
"18",
122-
"19",
123-
"20",
124-
"21",
125-
"22",
126-
"23",
127-
"24"
128-
),
129-
# Current platform valid versions
130-
rJavaEnv.fallback_valid_versions_current_platform_current_platform = fallback_current
80+
"11"
81+
)
13182
)
13283

133-
# Only set the options that haven't been set yet
84+
# Only set the options that haven't been defined yet
13485
toset <- !(names(op.rJavaEnv) %in% names(op))
13586
if (any(toset)) options(op.rJavaEnv[toset])
13687

88+
# Now, detect the current platform (OS and architecture)
89+
platform <- platform_detect(quiet = TRUE)
90+
91+
# Build the option name dynamically based on platform$os and platform$arch.
92+
# For example, for macOS on x64, this results in "rJavaEnv.fallback_valid_versions_macos_x64"
93+
fallback_option_name <- paste0(
94+
"rJavaEnv.fallback_valid_versions_",
95+
platform$os,
96+
"_",
97+
platform$arch
98+
)
99+
100+
# Retrieve the corresponding fallback list using getOption()
101+
fallback_current <- getOption(fallback_option_name)
102+
103+
# Set the current platform valid versions option
104+
options(rJavaEnv.fallback_valid_versions_current_platform = fallback_current)
105+
137106
invisible()
138107
}

tests/testthat/test-java_valid_versions.R

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,14 @@ test_that("fallback is used when the API call fails", {
3838
rJavaEnv.valid_versions_timestamp = NULL
3939
)
4040

41-
fallback <- java_valid_versions(force = TRUE)
42-
43-
# Backup the original fromJSON function from the jsonlite namespace.
44-
orig_fromJSON <- get("fromJSON", envir = asNamespace("jsonlite"))
45-
46-
# Temporarily override fromJSON to simulate an API failure.
47-
unlockBinding("read_json", asNamespace("jsonlite"))
48-
assign(
49-
"read_json",
50-
function(...) stop("Simulated API failure"),
51-
envir = asNamespace("jsonlite")
52-
)
53-
on.exit(
54-
{
55-
assign("read_json", orig_fromJSON, envir = asNamespace("jsonlite"))
56-
lockBinding("read_json", asNamespace("jsonlite"))
57-
},
58-
add = TRUE
41+
local_mocked_bindings(
42+
read_json = function(...) stop("Simulated API failure"),
43+
.package = "jsonlite"
5944
)
6045

46+
fallback <- getOption("rJavaEnv.fallback_valid_versions_current_platform")
6147
versions <- java_valid_versions(force = TRUE)
6248

63-
# When the API call fails, the fallback list should be returned.
49+
## When the API call fails, the fallback list should be returned.
6450
expect_equal(versions, fallback)
6551
})

0 commit comments

Comments
 (0)