summaryrefslogtreecommitdiff
path: root/tests/unit/test_target_python.py
blob: d3e27e39ae8436fcc4fcca80f4323ca891e6b4bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from typing import Any, Dict, Optional, Tuple
from unittest import mock

import pytest
from pip._vendor.packaging.tags import Tag

from pip._internal.models.target_python import TargetPython
from tests.lib import CURRENT_PY_VERSION_INFO, pyversion


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:
        """
        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:
        """
        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
        assert target_python.py_version == pyversion

    @pytest.mark.parametrize(
        "kwargs, expected",
        [
            ({}, ""),
            (dict(py_version_info=(3, 6)), "version_info='3.6'"),
            (
                dict(platforms=["darwin"], py_version_info=(3, 6)),
                "platforms=['darwin'] version_info='3.6'",
            ),
            (
                dict(
                    platforms=["darwin"],
                    py_version_info=(3, 6),
                    abis=["cp36m"],
                    implementation="cp",
                ),
                (
                    "platforms=['darwin'] version_info='3.6' abis=['cp36m'] "
                    "implementation='cp'"
                ),
            ),
        ],
    )
    def test_format_given(self, kwargs: Dict[str, Any], expected: str) -> None:
        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"),
            # Check a minor version with two digits.
            ((3, 10, 1), "310"),
            # Check that versions=None is passed to get_tags().
            (None, None),
        ],
    )
    @mock.patch("pip._internal.models.target_python.get_supported")
    def test_get_tags(
        self,
        mock_get_supported: mock.Mock,
        py_version_info: Optional[Tuple[int, ...]],
        expected_version: Optional[str],
    ) -> None:
        mock_get_supported.return_value = ["tag-1", "tag-2"]

        target_python = TargetPython(py_version_info=py_version_info)
        actual = target_python.get_tags()
        assert actual == ["tag-1", "tag-2"]

        actual = mock_get_supported.call_args[1]["version"]
        assert actual == expected_version

        # Check that the value was cached.
        assert target_python._valid_tags == ["tag-1", "tag-2"]

    def test_get_tags__uses_cached_value(self) -> None:
        """
        Test that get_tags() uses the cached value.
        """
        target_python = TargetPython(py_version_info=None)
        target_python._valid_tags = [
            Tag("py2", "none", "any"),
            Tag("py3", "none", "any"),
        ]
        actual = target_python.get_tags()
        assert actual == [Tag("py2", "none", "any"), Tag("py3", "none", "any")]