-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(violations): correctly test DEP003 (#923)
- Loading branch information
1 parent
945d9d8
commit b8e1882
Showing
1 changed file
with
34 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,56 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
from unittest.mock import patch | ||
|
||
from deptry.imports.location import Location | ||
from deptry.module import ModuleBuilder, ModuleLocations | ||
from deptry.violations import DEP003TransitiveDependenciesFinder | ||
from deptry.violations.dep003_transitive.violation import DEP003TransitiveDependencyViolation | ||
|
||
if TYPE_CHECKING: | ||
from deptry.dependency import Dependency | ||
|
||
|
||
def test_simple() -> None: | ||
""" | ||
black is in testing environment which requires platformdirs, so platformdirs should be found as transitive. | ||
""" | ||
dependencies: list[Dependency] = [] | ||
|
||
module_platformdirs_locations = [Location(Path("foo.py"), 1, 2), Location(Path("bar.py"), 3, 4)] | ||
module_platformdirs = ModuleBuilder("platformdirs", {"foo"}, frozenset(), dependencies).build() | ||
module = ModuleBuilder("foo", set(), frozenset()).build() | ||
|
||
modules_locations = [ModuleLocations(module_platformdirs, module_platformdirs_locations)] | ||
with patch.object(module, "package", return_value="foo"): | ||
issues = DEP003TransitiveDependenciesFinder( | ||
[ModuleLocations(module, [Location(Path("foo.py"), 1, 2)])], | ||
[], | ||
frozenset(), | ||
).find() | ||
|
||
assert DEP003TransitiveDependenciesFinder(modules_locations, dependencies, frozenset()).find() == [ | ||
DEP003TransitiveDependencyViolation(module_platformdirs, location) for location in module_platformdirs_locations | ||
assert issues == [ | ||
DEP003TransitiveDependencyViolation( | ||
issue=module, | ||
location=Location( | ||
file=Path("foo.py"), | ||
line=1, | ||
column=2, | ||
), | ||
), | ||
] | ||
|
||
|
||
def test_simple_with_ignore() -> None: | ||
dependencies: list[Dependency] = [] | ||
modules_locations = [ | ||
ModuleLocations( | ||
ModuleBuilder("foobar", {"foo"}, frozenset(), dependencies).build(), [Location(Path("foo.py"), 1, 2)] | ||
) | ||
] | ||
|
||
assert ( | ||
DEP003TransitiveDependenciesFinder( | ||
modules_locations, dependencies, frozenset(), ignored_modules=("foobar",) | ||
module = ModuleBuilder("foo", set(), frozenset()).build() | ||
|
||
with patch.object(module, "package", return_value="foo"): | ||
issues = DEP003TransitiveDependenciesFinder( | ||
[ModuleLocations(module, [Location(Path("foo.py"), 1, 2)])], | ||
[], | ||
frozenset(), | ||
ignored_modules=("foo",), | ||
).find() | ||
== [] | ||
) | ||
|
||
assert issues == [] | ||
|
||
|
||
def test_simple_with_standard_library() -> None: | ||
dependencies: list[Dependency] = [] | ||
standard_library_modules = frozenset(["foobar"]) | ||
modules_locations = [ | ||
ModuleLocations( | ||
ModuleBuilder("foobar", {"foo"}, standard_library_modules, dependencies).build(), | ||
[Location(Path("foo.py"), 1, 2)], | ||
) | ||
] | ||
module = ModuleBuilder("foo", set(), standard_library_modules=frozenset(["foo"])).build() | ||
|
||
with patch.object(module, "package", return_value="foo"): | ||
issues = DEP003TransitiveDependenciesFinder( | ||
[ModuleLocations(module, [Location(Path("foo.py"), 1, 2)])], [], frozenset() | ||
).find() | ||
|
||
assert DEP003TransitiveDependenciesFinder(modules_locations, dependencies, frozenset()).find() == [] | ||
assert issues == [] |