Skip to content

Commit 226ac79

Browse files
author
Trong Nhan Mai
committed
feat: add jdk version normalizer
Signed-off-by: Trong Nhan Mai <[email protected]>
1 parent e1f8994 commit 226ac79

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains the logic to nomarlize a JDK version string to a major version number."""
5+
6+
SUPPORTED_JAVA_VERSION = [
7+
"5",
8+
"6",
9+
"7",
10+
"8",
11+
"9",
12+
"10",
13+
"11",
14+
"12",
15+
"13",
16+
"14",
17+
"15",
18+
"16",
19+
"17",
20+
"18",
21+
"19",
22+
"20",
23+
"21",
24+
]
25+
26+
27+
def normalize_jdk_version(jdk_version_str: str) -> str | None:
28+
"""Return the major JDK version number.
29+
30+
We assume that the jdk version string is already valid (e.g not using a JDK
31+
version that is not available in the real world.
32+
33+
For 1.x versions, we returns the major version as ``x``.
34+
35+
Parameters
36+
----------
37+
jdk_version_str: str
38+
The jdk version string.
39+
40+
Returns
41+
-------
42+
str | None
43+
The major jdk version number as string or None if there is an error.
44+
45+
Examples
46+
--------
47+
>>> normalize_jdk_version("19")
48+
'19'
49+
>>> normalize_jdk_version("19-ea")
50+
'19'
51+
>>> normalize_jdk_version("11.0.1")
52+
'11'
53+
>>> normalize_jdk_version("1.8")
54+
'8'
55+
>>> normalize_jdk_version("25.0.1")
56+
"""
57+
first, _, after = jdk_version_str.partition(".")
58+
jdk_major_ver = None
59+
if first == "1":
60+
# Cases like 1.8.0_523
61+
# Or 1.8
62+
jdk_major_ver, _, _ = after.partition(".")
63+
else:
64+
# Cases like 11 or 11.0 or 11.0.1
65+
jdk_major_ver = first
66+
67+
if jdk_major_ver in SUPPORTED_JAVA_VERSION:
68+
return jdk_major_ver
69+
70+
# Handle edge cases:
71+
# pkg:maven/org.apache.druid.integration-tests/[email protected]
72+
# - "8 (Azul Systems Inc. 25.282-b08)"
73+
# pkg:maven/io.helidon.reactive.media/[email protected]
74+
# - "19-ea"
75+
for support in SUPPORTED_JAVA_VERSION:
76+
# Wouldn't work for cases like 19000 but that's not a big problem
77+
# as long as the result is a valid major version.
78+
if jdk_major_ver.startswith(support):
79+
return support
80+
81+
return None
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains the tests for the jdk version normalizer module."""
5+
6+
import pytest
7+
8+
from macaron.build_spec_generator.jdk_version_normalizer import normalize_jdk_version
9+
10+
11+
@pytest.mark.parametrize(
12+
("version_string", "expected"),
13+
[
14+
pytest.param(
15+
"1.8.0_523",
16+
"8",
17+
id="1.x_with_patch_version",
18+
),
19+
pytest.param(
20+
"1.8",
21+
"8",
22+
id="1.x_without_patch_version",
23+
),
24+
pytest.param(
25+
"11.0.1",
26+
"11",
27+
id="major_number_stands_first_with_patch_version",
28+
),
29+
pytest.param(
30+
"11.0",
31+
"11",
32+
id="major_number_stands_first_without_patch_version",
33+
),
34+
pytest.param(
35+
"11",
36+
"11",
37+
id="just_the_major_version",
38+
),
39+
pytest.param(
40+
"8 (Azul Systems Inc. 25.282-b08)",
41+
"8",
42+
id="major_follows_with_text",
43+
),
44+
pytest.param(
45+
"19-ea",
46+
"19",
47+
id="major_follows_with_text",
48+
),
49+
],
50+
)
51+
def test_jdk_version_normalizer(version_string: str, expected: str) -> None:
52+
"""Test the jdk_version_normalizer function."""
53+
assert normalize_jdk_version(version_string) == expected

0 commit comments

Comments
 (0)