Skip to content

Commit 85c0a56

Browse files
committed
chore: improve test; fix defaults set issue in Java Repo Finder.
Signed-off-by: Ben Selwyn-Smith <benselwynsmith@googlemail.com>
1 parent 38d9537 commit 85c0a56

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/macaron/repo_finder/repo_finder_java.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module contains the JavaRepoFinder class to be used for finding Java repositories."""
@@ -116,7 +116,10 @@ def _create_urls(self, group: str, artifact: str, version: str) -> list[str]:
116116
The list of created URLs.
117117
"""
118118
repositories = defaults.get_list(
119-
"repofinder.java", "artifact_repositories", fallback=["https://repo.maven.apache.org/maven2"]
119+
"repofinder.java",
120+
"artifact_repositories",
121+
fallback=["https://repo.maven.apache.org/maven2"],
122+
duplicated_ok=True,
120123
)
121124
urls = []
122125
for repo in repositories:
@@ -160,7 +163,7 @@ def _read_pom(self, pom: str) -> list[str]:
160163
The extracted contents as a list of strings.
161164
"""
162165
# Retrieve tags
163-
tags = defaults.get_list("repofinder.java", "repo_pom_paths")
166+
tags = defaults.get_list("repofinder.java", "repo_pom_paths", duplicated_ok=True)
164167
if not any(tags):
165168
logger.debug("No POM tags found for URL discovery.")
166169
return []

tests/repo_finder/test_repo_finder.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module tests the repo finder."""
5+
import os
6+
from pathlib import Path
57

68
import pytest
7-
from defusedxml.ElementTree import fromstring
89
from packageurl import PackageURL
910

11+
from macaron.config.defaults import load_defaults
1012
from macaron.config.target_config import Configuration
11-
from macaron.repo_finder import repo_validator
1213
from macaron.repo_finder.repo_finder_java import JavaRepoFinder
1314
from macaron.slsa_analyzer.analyzer import Analyzer
1415

@@ -74,34 +75,49 @@ def test_resolve_analysis_target(
7475
assert Analyzer.to_analysis_target(config, available_domains) == expect
7576

7677

77-
def test_pom_extraction_ordering() -> None:
78+
@pytest.mark.parametrize(
79+
("user_config_input", "expected"),
80+
[
81+
(
82+
"""
83+
[repofinder.java]
84+
repo_pom_paths =
85+
scm.connection
86+
scm.url
87+
""",
88+
["scm:git:git@github.com:oracle-samples/macaron.git", "https://github.com/oracle/macaron"],
89+
),
90+
(
91+
"""
92+
[repofinder.java]
93+
repo_pom_paths =
94+
scm.url
95+
scm.connection
96+
""",
97+
["https://github.com/oracle/macaron", "scm:git:git@github.com:oracle-samples/macaron.git"],
98+
),
99+
],
100+
)
101+
def test_pom_extraction_ordering(tmp_path: Path, user_config_input: str, expected: list[str]) -> None:
78102
"""Test the ordering of elements extracted from the POM is correct and maintained."""
79103
pom_text = """
80104
<project>
81105
<url>https://example.org</url>
82106
<scm>
83107
<connection>scm:git:git@github.com:oracle-samples/macaron.git</connection>
84-
<developerConnection>scm:git:git@github.com:oracle/macaron.git</developerConnection>
85108
<url>https://github.com/oracle/macaron</url>
86109
</scm>
87110
<properties>
88111
<url>1.9.15</url>
89112
</properties>
90113
</project>
91114
"""
92-
pom = fromstring(pom_text)
115+
user_config_path = os.path.join(tmp_path, "config.ini")
116+
with open(user_config_path, "w", encoding="utf-8") as user_config_file:
117+
user_config_file.write(user_config_input)
118+
load_defaults(user_config_path)
119+
93120
repo_finder = JavaRepoFinder()
94121

95122
# Retrieve SCM from POM.
96-
connection_urls = repo_finder._find_scm(pom, ["scm.connection", "scm.url"]) # pylint: disable=W0212
97-
assert connection_urls
98-
connection_url = repo_validator.find_valid_repository_url(connection_urls)
99-
assert connection_url
100-
101-
urls = repo_finder._find_scm(pom, ["scm.url", "scm.connection"]) # pylint: disable=W0212
102-
assert urls
103-
url = repo_validator.find_valid_repository_url(urls)
104-
assert url
105-
106-
# Ensure found URLs differ.
107-
assert connection_url != url
123+
assert expected == repo_finder._read_pom(pom_text) # pylint: disable=W0212

0 commit comments

Comments
 (0)