|
| 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_finder module.""" |
| 5 | + |
| 6 | +import zipfile |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from macaron.build_spec_generator.jdk_finder import get_jdk_version_from_jar, join_remote_maven_repo_url |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + ("remote_maven_url", "maven_repo_path", "expected"), |
| 16 | + [ |
| 17 | + pytest.param( |
| 18 | + "https://repo1.maven.org/maven2", |
| 19 | + "com/oracle/", |
| 20 | + "https://repo1.maven.org/maven2/com/oracle/", |
| 21 | + id="g_coordinate", |
| 22 | + ), |
| 23 | + pytest.param( |
| 24 | + "https://repo1.maven.org/maven2", |
| 25 | + "com/oracle/macaron/", |
| 26 | + "https://repo1.maven.org/maven2/com/oracle/macaron/", |
| 27 | + id="ga_coordinate", |
| 28 | + ), |
| 29 | + pytest.param( |
| 30 | + "https://repo1.maven.org/maven2", |
| 31 | + "com/oracle/macaron/0.16.0/", |
| 32 | + "https://repo1.maven.org/maven2/com/oracle/macaron/0.16.0/", |
| 33 | + id="gav_coordinate", |
| 34 | + ), |
| 35 | + pytest.param( |
| 36 | + "https://repo1.maven.org/maven2", |
| 37 | + "com/oracle/macaron/0.16.0/macaron-0.16.0.jar", |
| 38 | + "https://repo1.maven.org/maven2/com/oracle/macaron/0.16.0/macaron-0.16.0.jar", |
| 39 | + id="gav_asset_coordinate", |
| 40 | + ), |
| 41 | + pytest.param( |
| 42 | + "https://repo1.maven.org/maven2/", |
| 43 | + "com/oracle/macaron/0.16.0/", |
| 44 | + "https://repo1.maven.org/maven2/com/oracle/macaron/0.16.0/", |
| 45 | + id="handle_trailing_slash_in_remote_maven_url", |
| 46 | + ), |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_join_remote_maven_repo_url( |
| 50 | + remote_maven_url: str, |
| 51 | + maven_repo_path: str, |
| 52 | + expected: str, |
| 53 | +) -> None: |
| 54 | + """Test the join remote maven repo url function.""" |
| 55 | + assert ( |
| 56 | + join_remote_maven_repo_url( |
| 57 | + remote_maven_url=remote_maven_url, |
| 58 | + maven_repo_path=maven_repo_path, |
| 59 | + ) |
| 60 | + == expected |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + ("manifest_mf_content", "expected"), |
| 66 | + [ |
| 67 | + ("Build-Jdk: 1.8", "1.8"), |
| 68 | + ("Build-Jdk-Spec: 8", "8"), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_get_jdk_version_from_jar_succeed( |
| 72 | + tmp_path: Path, |
| 73 | + manifest_mf_content: str, |
| 74 | + expected: str, |
| 75 | +) -> None: |
| 76 | + """Test the get_jdk_version_from_jar function on valid cases.""" |
| 77 | + test_jar_file = tmp_path / "example.jar" |
| 78 | + |
| 79 | + with zipfile.ZipFile(test_jar_file, mode="w") as test_jar: |
| 80 | + test_jar.writestr("META-INF/MANIFEST.MF", manifest_mf_content) |
| 81 | + |
| 82 | + assert get_jdk_version_from_jar(str(test_jar_file)) == expected |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize( |
| 86 | + ("manifest_mf_content"), |
| 87 | + [ |
| 88 | + (""), |
| 89 | + ("Build-Jdk-Spec: "), |
| 90 | + ], |
| 91 | +) |
| 92 | +def test_get_jdk_version_from_jar_failed( |
| 93 | + tmp_path: Path, |
| 94 | + manifest_mf_content: str, |
| 95 | +) -> None: |
| 96 | + """Test the get_jdk_version_from_jar function on error cases.""" |
| 97 | + test_jar_file = tmp_path / "example.jar" |
| 98 | + |
| 99 | + with zipfile.ZipFile(test_jar_file, mode="w") as test_jar: |
| 100 | + test_jar.writestr("META-INF/MANIFEST.MF", manifest_mf_content) |
| 101 | + |
| 102 | + assert not get_jdk_version_from_jar(str(test_jar_file)) |
0 commit comments