forked from fpgmaas/deptry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_core.py
More file actions
218 lines (198 loc) · 6.61 KB
/
test_core.py
File metadata and controls
218 lines (198 loc) · 6.61 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from __future__ import annotations
import logging
import sys
from pathlib import Path
from unittest import mock
import pytest
from deptry.core import Core
from deptry.dependency import Dependency
from deptry.dependency_getter.base import DependenciesExtract
from deptry.imports.location import Location
from deptry.module import Module
from deptry.violations import (
DEP001MissingDependencyViolation,
DEP002UnusedDependencyViolation,
DEP003TransitiveDependencyViolation,
DEP004MisplacedDevDependencyViolation,
)
from tests.utils import create_files, run_within_dir
@pytest.mark.parametrize(
("known_first_party", "root_suffix", "experimental_namespace_package", "expected"),
[
(
(),
"",
False,
{
"local_file",
"module_with_init",
"module_without_init",
},
),
(
("module_with_init", "module_without_init"),
"",
False,
{
"local_file",
"module_with_init",
"module_without_init",
},
),
(
("module_without_init",),
"module_with_init",
False,
{
"foo",
"module_without_init",
"subdirectory",
},
),
(
(),
"",
True,
{
"local_file",
"module_using_namespace",
"module_with_init",
"module_without_init",
},
),
(
("module_with_init", "module_without_init"),
"",
True,
{
"local_file",
"module_using_namespace",
"module_with_init",
"module_without_init",
},
),
(
("module_without_init",),
"module_with_init",
True,
{
"foo",
"module_without_init",
"subdirectory",
},
),
],
)
def test__get_local_modules(
tmp_path: Path,
known_first_party: tuple[str, ...],
root_suffix: str,
experimental_namespace_package: bool,
expected: set[str],
) -> None:
with run_within_dir(tmp_path):
create_files([
Path("directory_without_python_files/foo.txt"),
Path("module_using_namespace/subdirectory_namespace/foo.py"),
Path("module_with_init/__init__.py"),
Path("module_with_init/foo.py"),
Path("module_with_init/subdirectory/__init__.py"),
Path("module_with_init/subdirectory/foo.py"),
Path("module_without_init/bar.py"),
Path("local_file.py"),
])
assert (
Core(
root=(tmp_path / root_suffix,),
config=Path("pyproject.toml"),
no_ansi=False,
per_rule_ignores={},
ignore=(),
exclude=(),
extend_exclude=(),
using_default_exclude=True,
ignore_notebooks=False,
requirements_files=(),
requirements_files_dev=(),
known_first_party=known_first_party,
json_output="",
package_module_name_map={},
pep621_dev_dependency_groups=(),
using_default_requirements_files=True,
experimental_namespace_package=experimental_namespace_package,
github_output=False,
github_warning_errors=(),
)._get_local_modules()
== expected
)
def test__get_stdlib_packages_with_stdlib_module_names() -> None:
assert Core._get_standard_library_modules() == sys.stdlib_module_names
@pytest.mark.parametrize(
"version_info",
[
(sys.version_info[0], sys.version_info[1] + 1, 0),
(sys.version_info[0], sys.version_info[1] + 1, 13),
(sys.version_info[0] + 1, sys.version_info[1], 0),
(sys.version_info[0], sys.version_info[1] + 1, 0, "beta", 1),
(sys.version_info[0], sys.version_info[1] + 1, 0, "candidate", 1),
],
)
def test__get_stdlib_packages_with_stdlib_module_names_future_version(version_info: tuple[int | str, ...]) -> None:
"""Test that future versions of Python not yet tested on the CI will also work."""
with mock.patch("sys.version_info", (sys.version_info[0], sys.version_info[1] + 1, 0)):
assert Core._get_standard_library_modules() == sys.stdlib_module_names
def test__exit_with_violations() -> None:
violations = [
DEP001MissingDependencyViolation(Module("foo"), Location(Path("foo.py"), 1, 2)),
DEP002UnusedDependencyViolation(Dependency("foo", Path("pyproject.toml")), Location(Path("pyproject.toml"))),
DEP003TransitiveDependencyViolation(Module("foo"), Location(Path("foo.py"), 1, 2)),
DEP004MisplacedDevDependencyViolation(Module("foo"), Location(Path("foo.py"), 1, 2)),
]
with pytest.raises(SystemExit) as e:
Core._exit(violations)
assert e.type is SystemExit
assert e.value.code == 1
def test__exit_without_violations() -> None:
with pytest.raises(SystemExit) as e:
Core._exit([])
assert e.type is SystemExit
assert e.value.code == 0
@pytest.mark.parametrize(
("dependencies", "dev_dependencies", "expected_logs"),
[
(
[],
[],
[],
),
(
[
Dependency("foo", Path("pyproject.toml")),
Dependency("bar", Path("pyproject.toml")),
],
[
Dependency("dev", Path("pyproject.toml")),
Dependency("another-dev", Path("pyproject.toml")),
],
[
"The project contains the following dependencies:",
"Dependency 'foo' with top-levels: {'foo'}.",
"Dependency 'bar' with top-levels: {'bar'}.",
"",
"The project contains the following dev dependencies:",
"Dependency 'dev' with top-levels: {'dev'}.",
"Dependency 'another-dev' with top-levels: {'another_dev'}.",
"",
],
),
],
)
def test__log_dependencies(
dependencies: list[Dependency],
dev_dependencies: list[Dependency],
expected_logs: list[str],
caplog: pytest.LogCaptureFixture,
) -> None:
with caplog.at_level(logging.DEBUG):
Core._log_dependencies(DependenciesExtract(dependencies, dev_dependencies))
assert caplog.messages == expected_logs