pip/tests/unit/test_target_python.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
4.1 KiB
Python
Raw Permalink Normal View History

from typing import Any, Dict, Optional, Tuple
from unittest import mock
2019-06-09 22:11:16 +02:00
2021-02-10 11:38:21 +01:00
import pytest
from pip._vendor.packaging.tags import Tag
2021-02-10 11:38:21 +01:00
2019-06-09 22:11:16 +02:00
from pip._internal.models.target_python import TargetPython
2019-07-18 19:03:01 +02:00
from tests.lib import CURRENT_PY_VERSION_INFO, pyversion
2019-06-09 22:11:16 +02:00
class TestTargetPython:
@pytest.mark.parametrize(
"py_version_info, expected",
[
((), ((0, 0, 0), "0.0")),
((2,), ((2, 0, 0), "2.0")),
((3,), ((3, 0, 0), "3.0")),
((3, 7), ((3, 7, 0), "3.7")),
((3, 7, 3), ((3, 7, 3), "3.7")),
# Check a minor version with two digits.
((3, 10, 1), ((3, 10, 1), "3.10")),
],
)
def test_init__py_version_info(
self,
py_version_info: Tuple[int, ...],
expected: Tuple[Tuple[int, int, int], str],
) -> None:
2019-06-09 22:11:16 +02:00
"""
Test passing the py_version_info argument.
"""
expected_py_version_info, expected_py_version = expected
target_python = TargetPython(py_version_info=py_version_info)
# The _given_py_version_info attribute should be set as is.
assert target_python._given_py_version_info == py_version_info
assert target_python.py_version_info == expected_py_version_info
assert target_python.py_version == expected_py_version
def test_init__py_version_info_none(self) -> None:
2019-06-09 22:11:16 +02:00
"""
Test passing py_version_info=None.
"""
target_python = TargetPython(py_version_info=None)
assert target_python._given_py_version_info is None
assert target_python.py_version_info == CURRENT_PY_VERSION_INFO
2019-07-18 19:03:01 +02:00
assert target_python.py_version == pyversion
2019-06-09 22:11:16 +02:00
2019-06-23 09:51:23 +02:00
@pytest.mark.parametrize(
"kwargs, expected",
[
({}, ""),
2023-08-26 10:20:40 +02:00
({"py_version_info": (3, 6)}, "version_info='3.6'"),
2019-06-23 09:51:23 +02:00
(
2023-08-26 10:20:40 +02:00
{"platforms": ["darwin"], "py_version_info": (3, 6)},
"platforms=['darwin'] version_info='3.6'",
2019-06-23 09:51:23 +02:00
),
(
2023-08-26 10:20:40 +02:00
{
"platforms": ["darwin"],
"py_version_info": (3, 6),
"abis": ["cp36m"],
"implementation": "cp",
},
2021-08-13 15:23:45 +02:00
(
"platforms=['darwin'] version_info='3.6' abis=['cp36m'] "
2019-06-23 09:51:23 +02:00
"implementation='cp'"
2021-08-13 15:23:45 +02:00
),
2019-06-23 09:51:23 +02:00
),
],
)
def test_format_given(self, kwargs: Dict[str, Any], expected: str) -> None:
2019-06-23 09:51:23 +02:00
target_python = TargetPython(**kwargs)
actual = target_python.format_given()
assert actual == expected
@pytest.mark.parametrize(
"py_version_info, expected_version",
[
((), ""),
((2,), "2"),
((3,), "3"),
((3, 7), "37"),
((3, 7, 3), "37"),
2019-06-09 22:11:16 +02:00
# Check a minor version with two digits.
((3, 10, 1), "310"),
# Check that versions=None is passed to get_sorted_tags().
2019-06-09 22:11:16 +02:00
(None, None),
],
)
@mock.patch("pip._internal.models.target_python.get_supported")
def test_get_sorted_tags(
self,
mock_get_supported: mock.Mock,
py_version_info: Optional[Tuple[int, ...]],
expected_version: Optional[str],
) -> None:
dummy_tags = [Tag("py4", "none", "any"), Tag("py5", "none", "any")]
mock_get_supported.return_value = dummy_tags
2019-06-09 22:11:16 +02:00
target_python = TargetPython(py_version_info=py_version_info)
actual = target_python.get_sorted_tags()
assert actual == dummy_tags
2019-06-09 22:11:16 +02:00
assert mock_get_supported.call_args[1]["version"] == expected_version
2019-06-09 22:11:16 +02:00
# Check that the value was cached.
assert target_python._valid_tags == dummy_tags
2019-06-09 22:11:16 +02:00
def test_get_unsorted_tags__uses_cached_value(self) -> None:
2019-06-09 22:11:16 +02:00
"""
Test that get_unsorted_tags() uses the cached value.
2019-06-09 22:11:16 +02:00
"""
target_python = TargetPython(py_version_info=None)
target_python._valid_tags_set = {
Tag("py2", "none", "any"),
Tag("py3", "none", "any"),
}
actual = target_python.get_unsorted_tags()
assert actual == {Tag("py2", "none", "any"), Tag("py3", "none", "any")}