1
1
from __future__ import annotations
2
2
3
+ from importlib .metadata import PackageNotFoundError
3
4
from itertools import chain
4
5
from typing import TYPE_CHECKING , Any , Callable , Iterator
6
+ from unittest .mock import Mock
5
7
6
8
import pytest
7
9
8
10
from pipdeptree ._models import DistPackage , PackageDAG , ReqPackage , ReversedPackageDAG
9
11
10
12
if TYPE_CHECKING :
11
- from unittest .mock import Mock
12
-
13
13
from tests .our_types import MockGraph
14
14
15
15
@@ -92,9 +92,16 @@ def sort_map_values(m: dict[str, Any]) -> dict[str, Any]:
92
92
assert all (isinstance (v , ReqPackage ) for v in chain .from_iterable (t2 .values ()))
93
93
94
94
95
- def test_package_dag_from_pkgs (mock_pkgs : Callable [[MockGraph ], Iterator [Mock ]]) -> None :
95
+ def test_package_dag_from_pkgs (
96
+ mock_pkgs : Callable [[MockGraph ], Iterator [Mock ]], monkeypatch : pytest .MonkeyPatch
97
+ ) -> None :
96
98
# when pip's _vendor.packaging.requirements.Requirement's requires() gives a lowercased package name but the actual
97
99
# package name in PyPI is mixed case, expect the mixed case version
100
+
101
+ # Since DistPackage.project_name will try to use importlib.metadata to grab the project name, let's avoid using
102
+ # the environment and use the fallback project name (since we control it here anyways).
103
+ monkeypatch .setattr ("pipdeptree._models.package.metadata" , Mock (side_effect = PackageNotFoundError ()))
104
+
98
105
graph : dict [tuple [str , str ], list [tuple [str , list [tuple [str , str ]]]]] = {
99
106
("examplePy" , "1.2.3" ): [("hellopy" , [(">=" , "2.0.0" )])],
100
107
("HelloPy" , "2.2.0" ): [],
@@ -106,8 +113,13 @@ def test_package_dag_from_pkgs(mock_pkgs: Callable[[MockGraph], Iterator[Mock]])
106
113
assert c [0 ].project_name == "HelloPy"
107
114
108
115
109
- def test_package_dag_from_pkgs_uses_pep503normalize (mock_pkgs : Callable [[MockGraph ], Iterator [Mock ]]) -> None :
116
+ def test_package_dag_from_pkgs_uses_pep503normalize (
117
+ mock_pkgs : Callable [[MockGraph ], Iterator [Mock ]], monkeypatch : pytest .MonkeyPatch
118
+ ) -> None :
110
119
# ensure that requirement gets matched with a dists even when it's key needs pep503 normalization to match
120
+
121
+ monkeypatch .setattr ("pipdeptree._models.package.metadata" , Mock (side_effect = PackageNotFoundError ()))
122
+
111
123
graph : dict [tuple [str , str ], list [tuple [str , list [tuple [str , str ]]]]] = {
112
124
("parent-package" , "1.2.3" ): [("flufl.lock" , [(">=" , "2.0.0" )])],
113
125
("flufl-lock" , "2.2.0" ): [],
0 commit comments