-
Notifications
You must be signed in to change notification settings - Fork 241
/
test_directory_dependency.py
142 lines (113 loc) · 4.38 KB
/
test_directory_dependency.py
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
from __future__ import annotations
from pathlib import Path
from typing import cast
import pytest
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.directory_dependency import DirectoryDependency
DIST_PATH = Path(__file__).parent.parent / "fixtures" / "git" / "github.com" / "demo"
SAMPLE_PROJECT = Path(__file__).parent.parent / "fixtures" / "sample_project"
def test_directory_dependency_must_exist() -> None:
with pytest.raises(ValueError):
DirectoryDependency("demo", DIST_PATH / "invalid")
def _test_directory_dependency_pep_508(
name: str, path: Path, pep_508_input: str, pep_508_output: str | None = None
) -> None:
dep = Dependency.create_from_pep_508(
pep_508_input, relative_to=Path(__file__).parent
)
assert dep.is_directory()
dep = cast("DirectoryDependency", dep)
assert dep.name == name
assert dep.path == path
assert dep.to_pep_508() == (pep_508_output or pep_508_input)
def test_directory_dependency_pep_508_local_absolute() -> None:
path = (
Path(__file__).parent.parent
/ "fixtures"
/ "project_with_multi_constraints_dependency"
)
expected = f"demo @ {path.as_uri()}"
requirement = f"demo @ file://{path.as_posix()}"
_test_directory_dependency_pep_508("demo", path, requirement, expected)
requirement = f"demo @ {path}"
_test_directory_dependency_pep_508("demo", path, requirement, expected)
def test_directory_dependency_pep_508_localhost() -> None:
path = (
Path(__file__).parent.parent
/ "fixtures"
/ "project_with_multi_constraints_dependency"
)
requirement = f"demo @ file://localhost{path.as_posix()}"
expected = f"demo @ {path.as_uri()}"
_test_directory_dependency_pep_508("demo", path, requirement, expected)
def test_directory_dependency_pep_508_local_relative() -> None:
path = Path("..") / "fixtures" / "project_with_multi_constraints_dependency"
with pytest.raises(ValueError):
requirement = f"demo @ file://{path.as_posix()}"
_test_directory_dependency_pep_508("demo", path, requirement)
requirement = f"demo @ {path}"
expected = f"demo @ {path.as_posix()}"
_test_directory_dependency_pep_508("demo", path, requirement, expected)
def test_directory_dependency_pep_508_extras() -> None:
path = (
Path(__file__).parent.parent
/ "fixtures"
/ "project_with_multi_constraints_dependency"
)
requirement = f"demo[foo,bar] @ file://{path.as_posix()}"
expected = f"demo[bar,foo] @ {path.as_uri()}"
_test_directory_dependency_pep_508("demo", path, requirement, expected)
def test_directory_dependency_pep_508_with_marker() -> None:
path = (
Path(__file__).parent.parent
/ "fixtures"
/ "project_with_multi_constraints_dependency"
)
requirement = f'demo @ file://{path.as_posix()} ; sys_platform == "linux"'
expected = f'demo @ {path.as_uri()} ; sys_platform == "linux"'
_test_directory_dependency_pep_508("demo", path, requirement, expected)
@pytest.mark.parametrize(
"name,path,extras,constraint,expected",
[
(
"my-package",
SAMPLE_PROJECT,
None,
None,
f"my-package (*) @ {SAMPLE_PROJECT.as_uri()}",
),
(
"my-package",
SAMPLE_PROJECT,
["db"],
"1.2",
f"my-package[db] (1.2) @ {SAMPLE_PROJECT.as_uri()}",
),
],
)
def test_directory_dependency_string_representation(
name: str,
path: Path,
extras: list[str] | None,
constraint: str | None,
expected: str,
) -> None:
dependency = DirectoryDependency(name=name, path=path, extras=extras)
if constraint:
dependency.constraint = constraint # type: ignore[assignment]
assert str(dependency) == expected
@pytest.mark.parametrize(
("fixture", "name"),
[
("project_with_pep517_non_poetry", "PEP 517"),
("project_with_setup_cfg_only", "setup.cfg"),
],
)
def test_directory_dependency_non_poetry_pep517(fixture: str, name: str) -> None:
path = Path(__file__).parent.parent / "fixtures" / fixture
try:
DirectoryDependency("package", path)
except ValueError as e:
if "does not seem to be a Python package" not in str(e):
raise e from e
pytest.fail(f"A {name} project not recognized as valid directory dependency")