|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 5 | +# ScanCode is a trademark of nexB Inc. |
| 6 | +# SPDX-License-Identifier: Apache-2.0 |
| 7 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 8 | +# See https://github.com/aboutcode-org/python-inspector for support or download. |
| 9 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 10 | +# |
| 11 | + |
| 12 | +import os |
| 13 | +from unittest import mock |
| 14 | + |
| 15 | +import pytest |
| 16 | +from commoncode.testcase import FileDrivenTesting |
| 17 | +from test_cli import check_data_results |
| 18 | + |
| 19 | +from python_inspector.package_data import get_pypi_data_from_purl |
| 20 | +from python_inspector.utils_pypi import Environment |
| 21 | +from python_inspector.utils_pypi import PypiSimpleRepository |
| 22 | + |
| 23 | +test_env = FileDrivenTesting() |
| 24 | +test_env.test_data_dir = os.path.join(os.path.dirname(__file__), "data") |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.asyncio |
| 28 | +@mock.patch("python_inspector.package_data.get_sdist_download_url") |
| 29 | +@mock.patch("python_inspector.package_data.get_wheel_download_urls") |
| 30 | +@mock.patch("python_inspector.utils.get_response_async") |
| 31 | +async def test_get_pypi_data_from_purl_tries_repos_in_order( |
| 32 | + mock_get_response, mock_get_wheels, mock_get_sdist |
| 33 | +): |
| 34 | + mock_get_sdist.return_value = None |
| 35 | + mock_get_wheels.return_value = [] |
| 36 | + |
| 37 | + call_urls = [] |
| 38 | + |
| 39 | + async def track_calls(url): |
| 40 | + call_urls.append(url) |
| 41 | + return None |
| 42 | + |
| 43 | + mock_get_response.side_effect = track_calls |
| 44 | + |
| 45 | + repo1 = PypiSimpleRepository(index_url="https://repo1.example.com/simple") |
| 46 | + repo2 = PypiSimpleRepository(index_url="https://repo2.example.com/simple") |
| 47 | + env = Environment(python_version="310", operating_system="linux") |
| 48 | + |
| 49 | + await get_pypi_data_from_purl( |
| 50 | + purl="pkg:pypi/requests@2.28.0", |
| 51 | + environment=env, |
| 52 | + repos=[repo1, repo2], |
| 53 | + prefer_source=False, |
| 54 | + ) |
| 55 | + |
| 56 | + assert call_urls == [ |
| 57 | + "https://repo1.example.com/pypi/requests/2.28.0/json", |
| 58 | + "https://repo2.example.com/pypi/requests/2.28.0/json", |
| 59 | + "https://pypi.org/pypi/requests/2.28.0/json", |
| 60 | + ] |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +@mock.patch("python_inspector.package_data.get_sdist_download_url") |
| 65 | +@mock.patch("python_inspector.package_data.get_wheel_download_urls") |
| 66 | +@mock.patch("python_inspector.utils.get_response_async") |
| 67 | +async def test_get_pypi_data_from_purl_stops_on_first_success( |
| 68 | + mock_get_response, mock_get_wheels, mock_get_sdist |
| 69 | +): |
| 70 | + mock_get_sdist.return_value = None |
| 71 | + mock_get_wheels.return_value = [] |
| 72 | + |
| 73 | + call_urls = [] |
| 74 | + |
| 75 | + async def return_success_on_second(url): |
| 76 | + call_urls.append(url) |
| 77 | + if "repo2" in url: |
| 78 | + return {"info": {}, "urls": []} |
| 79 | + return None |
| 80 | + |
| 81 | + mock_get_response.side_effect = return_success_on_second |
| 82 | + |
| 83 | + repo1 = PypiSimpleRepository(index_url="https://repo1.example.com/simple") |
| 84 | + repo2 = PypiSimpleRepository(index_url="https://repo2.example.com/simple") |
| 85 | + env = Environment(python_version="310", operating_system="linux") |
| 86 | + |
| 87 | + await get_pypi_data_from_purl( |
| 88 | + purl="pkg:pypi/requests@2.28.0", |
| 89 | + environment=env, |
| 90 | + repos=[repo1, repo2], |
| 91 | + prefer_source=False, |
| 92 | + ) |
| 93 | + |
| 94 | + assert call_urls == [ |
| 95 | + "https://repo1.example.com/pypi/requests/2.28.0/json", |
| 96 | + "https://repo2.example.com/pypi/requests/2.28.0/json", |
| 97 | + ] |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +@mock.patch("python_inspector.package_data.get_sdist_download_url") |
| 102 | +@mock.patch("python_inspector.package_data.get_wheel_download_urls") |
| 103 | +@mock.patch("python_inspector.utils.get_response_async") |
| 104 | +async def test_get_pypi_data_from_purl_falls_back_to_pypi_org( |
| 105 | + mock_get_response, mock_get_wheels, mock_get_sdist |
| 106 | +): |
| 107 | + mock_get_sdist.return_value = None |
| 108 | + mock_get_wheels.return_value = [] |
| 109 | + |
| 110 | + call_urls = [] |
| 111 | + |
| 112 | + async def track_calls(url): |
| 113 | + call_urls.append(url) |
| 114 | + return None |
| 115 | + |
| 116 | + mock_get_response.side_effect = track_calls |
| 117 | + |
| 118 | + env = Environment(python_version="310", operating_system="linux") |
| 119 | + |
| 120 | + await get_pypi_data_from_purl( |
| 121 | + purl="pkg:pypi/requests@2.28.0", |
| 122 | + environment=env, |
| 123 | + repos=[], |
| 124 | + prefer_source=False, |
| 125 | + ) |
| 126 | + |
| 127 | + assert call_urls == ["https://pypi.org/pypi/requests/2.28.0/json"] |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.asyncio |
| 131 | +@mock.patch("python_inspector.package_data.get_sdist_download_url") |
| 132 | +@mock.patch("python_inspector.package_data.get_wheel_download_urls") |
| 133 | +@mock.patch("python_inspector.utils.get_response_async") |
| 134 | +async def test_get_pypi_data_from_purl_matches_by_filename( |
| 135 | + mock_get_response, mock_get_wheels, mock_get_sdist |
| 136 | +): |
| 137 | + mock_get_sdist.return_value = None |
| 138 | + mock_get_wheels.return_value = [ |
| 139 | + "https://repo.example.com/simple/../packages/ab/cd/requests-2.28.0-py3-none-any.whl" |
| 140 | + ] |
| 141 | + |
| 142 | + async def return_json_response(url): |
| 143 | + if "pypi" in url: |
| 144 | + return { |
| 145 | + "info": { |
| 146 | + "name": "requests", |
| 147 | + "version": "2.28.0", |
| 148 | + "home_page": "https://requests.readthedocs.io", |
| 149 | + "license_expression": "Apache-2.0", |
| 150 | + }, |
| 151 | + "urls": [ |
| 152 | + { |
| 153 | + "url": "../../packages/xy/zz/requests-2.28.0-py3-none-any.whl", |
| 154 | + "digests": {"sha256": "abc123def456", "md5": "789xyz"}, |
| 155 | + "size": 62500, |
| 156 | + "upload_time": "2022-06-29T15:30:00", |
| 157 | + } |
| 158 | + ], |
| 159 | + } |
| 160 | + return None |
| 161 | + |
| 162 | + mock_get_response.side_effect = return_json_response |
| 163 | + |
| 164 | + repo = PypiSimpleRepository(index_url="https://repo.example.com/simple") |
| 165 | + env = Environment(python_version="310", operating_system="linux") |
| 166 | + |
| 167 | + result = await get_pypi_data_from_purl( |
| 168 | + purl="pkg:pypi/requests@2.28.0", |
| 169 | + environment=env, |
| 170 | + repos=[repo], |
| 171 | + prefer_source=False, |
| 172 | + ) |
| 173 | + |
| 174 | + expected_file = test_env.get_test_loc( |
| 175 | + "test_get_pypi_data_from_purl_matches_by_filename-expected.json", |
| 176 | + must_exist=False, |
| 177 | + ) |
| 178 | + check_data_results(results=result.to_dict(), expected_file=expected_file) |
0 commit comments