Skip to content

Commit

Permalink
chore: use metadata in all tests; fix docstrings
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Selwyn-Smith <[email protected]>
  • Loading branch information
benmss committed Feb 2, 2025
1 parent 593040b commit 1c562c8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/macaron/repo_finder/repo_finder_deps_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def get_latest_version(purl: PackageURL) -> tuple[PackageURL | None, RepoFinderI

def _create_urls(self, purl: PackageURL) -> tuple[list[str], RepoFinderInfo]:
"""
Create the urls to search for the metadata relating to the passed artifact.
Create the urls to search for the metadata relating to the passed artifact, and report on that process.
If a version is not specified, remote API calls will be used to try and find one.
Expand All @@ -169,8 +169,8 @@ def _create_urls(self, purl: PackageURL) -> tuple[list[str], RepoFinderInfo]:
Returns
-------
list[str]
The list of created URLs.
tuple[list[str], RepoFinderInfo]
A tuple of: the list of created URLs, and the information on the Repo Finder outcome.
"""
outcome = None
if not purl.version:
Expand Down
2 changes: 1 addition & 1 deletion src/macaron/repo_finder/repo_finder_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _retrieve_pom(self, url: str) -> tuple[str, RepoFinderInfo]:
tuple[str, RepoFinderOutcome] :
The retrieved file data or an empty string, and the outcome to report.
"""
response = send_get_http_raw(url, always_return_response=True)
response = send_get_http_raw(url, check_response_fails=True)

if not response:
return "", RepoFinderInfo.HTTP_INVALID
Expand Down
11 changes: 7 additions & 4 deletions src/macaron/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022 - 2024, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2022 - 2025, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.

"""This module includes utilities functions for Macaron."""
Expand Down Expand Up @@ -130,7 +130,7 @@ def send_get_http_raw(
headers: dict | None = None,
timeout: int | None = None,
allow_redirects: bool = True,
always_return_response: bool = False,
check_response_fails: bool = True,
) -> Response | None:
"""Send the GET HTTP request with the given url and headers.
Expand All @@ -146,13 +146,16 @@ def send_get_http_raw(
The request timeout (optional).
allow_redirects: bool
Whether to allow redirects. Default: True.
check_response_fails: bool
When True, check if the response fails. Otherwise, return the response.
Returns
-------
Response | None
If a Response object is returned and ``allow_redirects`` is ``True`` (the default) it will have a status code of
200 (OK). If ``allow_redirects`` is ``False`` the response can instead have a status code of 302. Otherwise, the
request has failed and ``None`` will be returned.
request has failed and ``None`` will be returned. If ``check_response_fails`` is False, the response will be
returned regardless of its status code.
"""
logger.debug("GET - %s", url)
if not timeout:
Expand Down Expand Up @@ -183,7 +186,7 @@ def send_get_http_raw(
if response.status_code == 403:
check_rate_limit(response)
else:
return None if not always_return_response else response
return None if not check_response_fails else response
retry_counter = retry_counter - 1
response = requests.get(
url=url,
Expand Down
28 changes: 15 additions & 13 deletions tests/integration/cases/repo_finder_remote_calls/repo_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from macaron.repo_finder import repo_validator
from macaron.repo_finder.repo_finder import find_repo
from macaron.repo_finder.repo_finder_deps_dev import DepsDevRepoFinder
from macaron.repo_finder.repo_finder_enums import RepoFinderOutcome
from macaron.repo_finder.repo_finder_enums import RepoFinderInfo
from macaron.slsa_analyzer.git_url import clean_url

logger: logging.Logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -45,32 +45,32 @@ def test_repo_finder() -> int:

# Test Java package with SCM metadata in artifact POM.
match, outcome = find_repo(PackageURL.from_string("pkg:maven/com.fasterxml.jackson.core/[email protected]"))
if not match or outcome != RepoFinderOutcome.FOUND:
if not match or outcome != RepoFinderInfo.FOUND:
return os.EX_UNAVAILABLE

# Test Java package with SCM metadata in artifact's parent POM.
match, outcome = find_repo(PackageURL.from_string("pkg:maven/commons-cli/[email protected]"))
if not match or outcome != RepoFinderOutcome.FOUND:
if not match or outcome != RepoFinderInfo.FOUND:
return os.EX_UNAVAILABLE

# Test deps.dev API for a Python package.
match, outcome = find_repo(PackageURL.from_string("pkg:pypi/[email protected]"))
if not match or outcome != RepoFinderOutcome.FOUND:
if not match or outcome != RepoFinderInfo.FOUND:
return os.EX_UNAVAILABLE

# Test deps.dev API for a Nuget package.
match, outcome = find_repo(PackageURL.from_string("pkg:nuget/azure.core"))
if not match or outcome != RepoFinderOutcome.FOUND:
if not match or outcome != RepoFinderInfo.FOUND:
return os.EX_UNAVAILABLE

# Test deps.dev API for an NPM package.
match, outcome = find_repo(PackageURL.from_string("pkg:npm/@colors/colors"))
if not match or outcome != RepoFinderOutcome.FOUND:
if not match or outcome != RepoFinderInfo.FOUND:
return os.EX_UNAVAILABLE

# Test deps.dev API for Cargo package.
match, outcome = find_repo(PackageURL.from_string("pkg:cargo/rand_core"))
if not match or outcome != RepoFinderOutcome.FOUND:
if not match or outcome != RepoFinderInfo.FOUND:
return os.EX_UNAVAILABLE

# Test redirecting URL from Apache commons-io package.
Expand All @@ -80,18 +80,20 @@ def test_repo_finder() -> int:

# Test Java package whose SCM metadata only points to the repo in later versions than is provided here.
purl = PackageURL.from_string("pkg:maven/io.vertx/[email protected]")
repo, _ = find_repo(purl)
if repo == "https://github.com/eclipse-vertx/vertx-auth":
repo, outcome = find_repo(purl)
if outcome != RepoFinderInfo.FOUND_FROM_PARENT or repo == "https://github.com/eclipse-vertx/vertx-auth":
return os.EX_UNAVAILABLE
latest_purl, _ = DepsDevRepoFinder().get_latest_version(purl)
latest_purl, outcome = DepsDevRepoFinder().get_latest_version(purl)
assert latest_purl
repo, _ = find_repo(latest_purl)
if repo != "https://github.com/eclipse-vertx/vertx-auth":
if outcome != RepoFinderInfo.FOUND_FROM_LATEST:
return os.EX_UNAVAILABLE
repo, outcome = find_repo(latest_purl)
if outcome != RepoFinderInfo.FOUND_FROM_PARENT or repo != "https://github.com/eclipse-vertx/vertx-auth":
return os.EX_UNAVAILABLE

# Test Java package that has no version.
match, outcome = find_repo(PackageURL.from_string("pkg:maven/io.vertx/vertx-auth-common"))
if not match or outcome != RepoFinderOutcome.FOUND:
if not match or outcome != RepoFinderInfo.FOUND:
return os.EX_UNAVAILABLE

return os.EX_OK
Expand Down

0 comments on commit 1c562c8

Please sign in to comment.