File: test_dependency_specification.py

package info (click to toggle)
poetry 2.3.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: python: 56,035; sh: 128; makefile: 100; ansic: 49
file content (192 lines) | stat: -rw-r--r-- 6,417 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from deepdiff.diff import DeepDiff

from poetry.inspection.info import PackageInfo
from poetry.utils.dependency_specification import RequirementsParser


if TYPE_CHECKING:
    from collections.abc import Collection

    from pytest_mock import MockerFixture

    from poetry.utils.cache import ArtifactCache
    from poetry.utils.dependency_specification import DependencySpec


@pytest.mark.parametrize(
    ("requirement", "expected_variants"),
    [
        (
            "git+http://github.com/demo/demo.git",
            ({"git": "http://github.com/demo/demo.git", "name": "demo"},),
        ),
        (
            "git+https://github.com/demo/demo.git",
            ({"git": "https://github.com/demo/demo.git", "name": "demo"},),
        ),
        (
            "git+ssh://github.com/demo/demo.git",
            ({"git": "ssh://github.com/demo/demo.git", "name": "demo"},),
        ),
        (
            "git+https://github.com/demo/demo.git#main",
            (
                {
                    "git": "https://github.com/demo/demo.git",
                    "name": "demo",
                    "rev": "main",
                },
            ),
        ),
        (
            "git+https://github.com/demo/demo.git@main",
            (
                {
                    "git": "https://github.com/demo/demo.git",
                    "name": "demo",
                    "rev": "main",
                },
            ),
        ),
        (
            "git+https://github.com/demo/subdirectories.git@main#subdirectory=two",
            (
                {
                    "git": "https://github.com/demo/subdirectories.git",
                    "name": "two",
                    "rev": "main",
                    "subdirectory": "two",
                },
            ),
        ),
        ("demo", ({"name": "demo"},)),
        ("demo@1.0.0", ({"name": "demo", "version": "1.0.0"},)),
        ("demo@^1.0.0", ({"name": "demo", "version": "^1.0.0"},)),
        ("demo@==1.0.0", ({"name": "demo", "version": "==1.0.0"},)),
        ("demo@!=1.0.0", ({"name": "demo", "version": "!=1.0.0"},)),
        ("demo@~1.0.0", ({"name": "demo", "version": "~1.0.0"},)),
        (
            "demo[a,b]@1.0.0",
            ({"name": "demo", "version": "1.0.0", "extras": ["a", "b"]},),
        ),
        ("demo[a,b]", ({"name": "demo", "extras": ["a", "b"]},)),
        ("../demo", ({"name": "demo", "path": "../demo"},)),
        ("../demo/demo.whl", ({"name": "demo", "path": "../demo/demo.whl"},)),
        (
            "https://files.pythonhosted.org/distributions/demo-0.1.0.tar.gz",
            (
                {
                    "name": "demo",
                    "url": "https://files.pythonhosted.org/distributions/demo-0.1.0.tar.gz",
                },
            ),
        ),
        # PEP 508 inputs
        (
            "poetry-core (>=1.0.7,<1.1.0)",
            ({"name": "poetry-core", "version": ">=1.0.7,<1.1.0"},),
        ),
        (
            'requests [security,tests] >= 2.8.1, == 2.8.* ; python_version < "2.7"',
            (  # allow several equivalent versions to make test more robust
                {
                    "name": "requests",
                    "markers": 'python_version < "2.7"',
                    "version": ">=2.8.1,<2.9",
                    "extras": ["security", "tests"],
                },
                {
                    "name": "requests",
                    "markers": 'python_version < "2.7"',
                    "version": ">=2.8.1,<2.9.0",
                    "extras": ["security", "tests"],
                },
                {
                    "name": "requests",
                    "markers": 'python_version < "2.7"',
                    "version": ">=2.8.1,<2.9.dev0",
                    "extras": ["security", "tests"],
                },
                {
                    "name": "requests",
                    "markers": 'python_version < "2.7"',
                    "version": ">=2.8.1,<2.9.0.dev0",
                    "extras": ["security", "tests"],
                },
                {
                    "name": "requests",
                    "markers": 'python_version < "2.7"',
                    "version": ">=2.8.1,==2.8.*",
                    "extras": ["security", "tests"],
                },
            ),
        ),
        ("name (>=3,<4)", ({"name": "name", "version": ">=3,<4"},)),
        (
            "name@http://foo.com",
            ({"name": "name", "url": "http://foo.com"},),
        ),
        (
            "name [fred,bar] @ http://foo.com ; python_version=='2.7'",
            (
                {
                    "name": "name",
                    "markers": 'python_version == "2.7"',
                    "url": "http://foo.com",
                    "extras": ["fred", "bar"],
                },
            ),
        ),
        (
            (
                'cachecontrol[filecache] (>=0.12.9,<0.13.0); python_version >= "3.6"'
                ' and python_version < "4.0"'
            ),
            (
                {
                    "version": ">=0.12.9,<0.13.0",
                    "markers": 'python_version >= "3.6" and python_version < "4.0"',
                    "extras": ["filecache"],
                    "name": "cachecontrol",
                },
            ),
        ),
    ],
)
def test_parse_dependency_specification(
    requirement: str,
    expected_variants: Collection[DependencySpec],
    mocker: MockerFixture,
    artifact_cache: ArtifactCache,
) -> None:
    original = Path.exists

    # Parsing file and path dependencies reads metadata from the file or path in
    # question: for these tests we mock that out.
    def _mock(self: Path) -> bool:
        if "/" in requirement and self == Path.cwd().joinpath(requirement):
            return True
        return original(self)

    mocker.patch("pathlib.Path.exists", _mock)

    mocker.patch(
        "poetry.inspection.info.get_pep517_metadata",
        return_value=PackageInfo(name="demo", version="0.1.2"),
    )

    assert any(
        not DeepDiff(
            RequirementsParser(artifact_cache=artifact_cache).parse(requirement),
            specification,
            ignore_order=True,
        )
        for specification in expected_variants
    )