From b8e18826a00594d8ba9e70111f0666515524dce2 Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Tue, 29 Oct 2024 12:03:28 +0100 Subject: [PATCH] test(violations): correctly test DEP003 (#923) --- .../dep003_transitive/test_finder.py | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/tests/unit/violations/dep003_transitive/test_finder.py b/tests/unit/violations/dep003_transitive/test_finder.py index 3c21cec9..a99da716 100644 --- a/tests/unit/violations/dep003_transitive/test_finder.py +++ b/tests/unit/violations/dep003_transitive/test_finder.py @@ -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 == []