|
| 1 | +# Copyright (c) 2026 - 2026, 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 tests the Maven build specification field resolution.""" |
| 5 | + |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from packageurl import PackageURL |
| 10 | + |
| 11 | +from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpecDict |
| 12 | +from macaron.build_spec_generator.common_spec.maven_spec import MavenBuildSpec |
| 13 | + |
| 14 | +PURL_STRING = "pkg:maven/com.example/demo@1.0.0" |
| 15 | + |
| 16 | + |
| 17 | +def _build_spec_dict(language_version: list[str]) -> BaseBuildSpecDict: |
| 18 | + """Return a minimal Maven build spec dict carrying the passed language version list.""" |
| 19 | + return BaseBuildSpecDict( |
| 20 | + ecosystem="maven", |
| 21 | + purl=PURL_STRING, |
| 22 | + language="java", |
| 23 | + build_tools=["maven"], |
| 24 | + macaron_version="0.0.0", |
| 25 | + group_id="com.example", |
| 26 | + artifact_id="demo", |
| 27 | + version="1.0.0", |
| 28 | + language_version=list(language_version), |
| 29 | + build_commands=[ |
| 30 | + { |
| 31 | + "build_tool": "maven", |
| 32 | + "build_config_path": "pom.xml", |
| 33 | + "command": ["mvn", "clean", "package"], |
| 34 | + "confidence_score": 1.0, |
| 35 | + } |
| 36 | + ], |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def _resolve(language_version: list[str], jdk_from_jar: str | None) -> list[str]: |
| 41 | + """Resolve a Maven build spec with the JAR lookup pinned, and return the resulting language version.""" |
| 42 | + data = _build_spec_dict(language_version) |
| 43 | + with patch( |
| 44 | + "macaron.build_spec_generator.common_spec.maven_spec.find_jdk_version_from_central_maven_repo", |
| 45 | + return_value=jdk_from_jar, |
| 46 | + ): |
| 47 | + MavenBuildSpec(data).resolve_fields(PackageURL.from_string(PURL_STRING)) |
| 48 | + return data["language_version"] |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize( |
| 52 | + ("existing", "jdk_from_jar", "expected"), |
| 53 | + [ |
| 54 | + # The JAR manifest is the strongest evidence and wins whether or not the database |
| 55 | + # recorded a language version. The empty-list case is the ordinary one: core.py sets |
| 56 | + # "language_version" to [] whenever the database holds no recorded version. |
| 57 | + ([], "17", ["17"]), |
| 58 | + (["11"], "17", ["17"]), |
| 59 | + # No JAR evidence: fall back to what the database recorded, then to the default of 8. |
| 60 | + (["11"], None, ["11"]), |
| 61 | + ([], None, ["8"]), |
| 62 | + ], |
| 63 | +) |
| 64 | +def test_resolve_fields_selects_jdk_version(existing: list[str], jdk_from_jar: str | None, expected: list[str]) -> None: |
| 65 | + """The JDK read from the Maven Central JAR must not be discarded when the database recorded none.""" |
| 66 | + assert _resolve(existing, jdk_from_jar) == expected |
0 commit comments