diff --git a/src/fetchcode/cpan.py b/src/fetchcode/cpan.py index 8e446ec..1ec7e60 100644 --- a/src/fetchcode/cpan.py +++ b/src/fetchcode/cpan.py @@ -27,32 +27,27 @@ class CPAN: base_url = "https://cpan.metacpan.org/" def get_download_url(purl: str): - """ - Resolve a CPAN PURL to a verified, downloadable archive URL. - Strategy: MetaCPAN API -> verified URL; fallback to author-based path if available. - """ + """Resolve a versioned, author-scoped CPAN distribution to a verified archive URL.""" p = PackageURL.from_string(purl) - if not p.name or not p.version: + author = (p.qualifiers or {}).get("author") or p.namespace + if not author or not p.version or "::" in p.name: return None - parsed_name = urllib.parse.quote(p.name) - parsed_version = urllib.parse.quote(p.version) - api = f"https://fastapi.metacpan.org/v1/release/{parsed_name}/{parsed_version}" + author = author.upper() + author_path = urllib.parse.quote(author, safe="") + release_name = urllib.parse.quote(f"{p.name}-{p.version}", safe="") + api = f"https://fastapi.metacpan.org/v1/release/{author_path}/{release_name}" if _http_exists(api): - # Fetch release data from MetaCPAN API - # Example: https://fastapi.metacpan.org/v1/release/Some-Module/1.2.3 data = fetch_json_response(url=api) - url = data.get("download_url") or data.get("archive") - if url and _http_exists(url): - return url - - author = p.namespace - if not author: - return - auth = author.upper() - a = auth[0] - ab = auth[:2] if len(auth) >= 2 else auth + release = data.get("release", data) + if release: + url = release.get("download_url") + if url and _http_exists(url): + return url + + first = urllib.parse.quote(author[:1], safe="") + prefix = urllib.parse.quote(author[:2], safe="") for ext in (".tar.gz", ".zip"): - url = f"https://cpan.metacpan.org/authors/id/{a}/{ab}/{auth}/{p.name}-{p.version}{ext}" + url = f"{CPAN.base_url}authors/id/{first}/{prefix}/{author_path}/{release_name}{ext}" if _http_exists(url): return url diff --git a/tests/test_cpan.py b/tests/test_cpan.py index 374af41..15a2628 100644 --- a/tests/test_cpan.py +++ b/tests/test_cpan.py @@ -14,11 +14,13 @@ # CONDITIONS OF ANY KIND, either express or implied. See the License for the # specific language governing permissions and limitations under the License. +from unittest.mock import call from unittest.mock import patch import pytest from fetchcode.cpan import CPAN +from fetchcode.download_urls import download_url get_download_url = CPAN.get_download_url @@ -28,26 +30,33 @@ def valid_purl(): return "pkg:cpan/EXAMPLE/Some-Module@1.2.3" -def test_success_from_metacpan_api(valid_purl): - expected_url = "https://cpan.metacpan.org/authors/id/E/EX/EXAMPLE/Some-Module-1.2.3.tar.gz" +@pytest.mark.parametrize( + "purl", + ["pkg:cpan/GDT/URI-PackageURL@2.22", "pkg:cpan/URI-PackageURL@2.22?author=gdt"], +) +@pytest.mark.parametrize("wrapped", [False, True]) +def test_success_from_author_scoped_metacpan_api(purl, wrapped): + api_url = "https://fastapi.metacpan.org/v1/release/GDT/URI-PackageURL-2.22" + expected_url = "https://cpan.metacpan.org/authors/id/G/GD/GDT/URI-PackageURL-2.22.tar.gz" + release = {"author": "GDT", "download_url": expected_url} + payload = {"release": release, "total": 1} if wrapped else release - with patch("fetchcode.cpan.fetch_json_response") as mock_fetch, patch( - "fetchcode.cpan._http_exists" - ) as mock_exists: - mock_fetch.return_value = {"download_url": expected_url} - mock_exists.return_value = True - result = get_download_url(valid_purl) - assert result == expected_url - mock_fetch.assert_called_once() - assert mock_exists.call_count == 2 + with ( + patch("fetchcode.cpan.fetch_json_response", return_value=payload) as mock_fetch, + patch("fetchcode.cpan._http_exists", return_value=True) as mock_exists, + ): + assert download_url(purl) == expected_url + mock_fetch.assert_called_once_with(url=api_url) + assert mock_exists.call_args_list == [call(api_url), call(expected_url)] def test_fallback_to_author_path(valid_purl): expected_url = "https://cpan.metacpan.org/authors/id/E/EX/EXAMPLE/Some-Module-1.2.3.tar.gz" - with patch("fetchcode.cpan.fetch_json_response", side_effect=Exception("API error")), patch( - "fetchcode.cpan._http_exists" - ) as mock_exists: + with ( + patch("fetchcode.cpan.fetch_json_response", side_effect=Exception("API error")), + patch("fetchcode.cpan._http_exists") as mock_exists, + ): mock_exists.side_effect = lambda url: url.endswith(".tar.gz") @@ -60,9 +69,10 @@ def test_author_zip_fallback(valid_purl): tar_url = "https://cpan.metacpan.org/authors/id/E/EX/EXAMPLE/Some-Module-1.2.3.tar.gz" zip_url = "https://cpan.metacpan.org/authors/id/E/EX/EXAMPLE/Some-Module-1.2.3.zip" - with patch("fetchcode.cpan.fetch_json_response", return_value={}), patch( - "fetchcode.cpan._http_exists" - ) as mock_exists: + with ( + patch("fetchcode.cpan.fetch_json_response", return_value={}), + patch("fetchcode.cpan._http_exists") as mock_exists, + ): mock_exists.side_effect = lambda url: url == zip_url @@ -73,14 +83,36 @@ def test_author_zip_fallback(valid_purl): def test_neither_api_nor_fallback_works(valid_purl): - with patch("fetchcode.cpan.fetch_json_response", return_value={}), patch( - "fetchcode.cpan._http_exists", return_value=False - ) as mock_exists: + with ( + patch("fetchcode.cpan.fetch_json_response", return_value={}), + patch("fetchcode.cpan._http_exists", return_value=False) as mock_exists, + ): result = get_download_url(valid_purl) assert result is None assert mock_exists.call_count == 3 -def test_missing_name_or_version(): - assert get_download_url("pkg:cpan/EXAMPLE/Some-Module") is None +@pytest.mark.parametrize( + "purl", + [ + "pkg:cpan/EXAMPLE/Some-Module", + "pkg:cpan/Some-Module@1.2.3", + "pkg:cpan/EXAMPLE/Some%3A%3AModule@1.2.3", + ], +) +def test_purl_does_not_identify_an_author_scoped_release(purl): + with patch("fetchcode.cpan._http_exists", return_value=False) as mock_exists: + assert get_download_url(purl) is None + mock_exists.assert_not_called() + + +def test_missing_metacpan_release_falls_back_to_author_path(valid_purl): + api_url = "https://fastapi.metacpan.org/v1/release/EXAMPLE/Some-Module-1.2.3" + tar_url = "https://cpan.metacpan.org/authors/id/E/EX/EXAMPLE/Some-Module-1.2.3.tar.gz" + with ( + patch("fetchcode.cpan.fetch_json_response", return_value={"release": None}), + patch("fetchcode.cpan._http_exists", return_value=True) as mock_exists, + ): + assert get_download_url(valid_purl) == tar_url + assert mock_exists.call_args_list == [call(api_url), call(tar_url)]