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
|
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from pipdeptree._models import PackageDAG
from pipdeptree._validate import conflicting_deps, cyclic_deps, render_conflicts_text, render_cycles_text, validate
if TYPE_CHECKING:
from collections.abc import Callable, Iterator
from unittest.mock import Mock
from tests.our_types import MockGraph
@pytest.mark.parametrize(
("mpkgs", "expected_keys", "expected_output"),
[
pytest.param(
{
("a", "1.0.1"): [("b", [(">=", "2.0.0")])],
("b", "2.3.0"): [("a", [(">=", "1.0.1")])],
("c", "4.5.0"): [("d", [("==", "2.0")])],
("d", "2.0"): [],
},
[["a", "b", "a"], ["b", "a", "b"]],
["* b => a => b", "* a => b => a"],
id="depth-of-2",
),
pytest.param(
{
("a", "1.0.1"): [("b", [(">=", "2.0.0")]), ("e", [("==", "2.0")])],
("b", "2.3.0"): [("c", [(">=", "4.5.0")])],
("c", "4.5.0"): [("d", [("==", "1.0.1")])],
("d", "1.0.0"): [("a", [("==", "1.0.1")]), ("e", [("==", "2.0")])],
("e", "2.0"): [],
},
[
["b", "c", "d", "a", "b"],
["c", "d", "a", "b", "c"],
["d", "a", "b", "c", "d"],
["a", "b", "c", "d", "a"],
],
[
"* b => c => d => a => b",
"* c => d => a => b => c",
"* d => a => b => c => d",
"* a => b => c => d => a",
],
id="depth-greater-than-2",
),
pytest.param(
{("a", "1.0.1"): [("b", [(">=", "2.0.0")])], ("b", "2.0.0"): []},
[],
[],
id="no-cycle",
),
pytest.param(
{
("a", "1.0.1"): [("b", [(">=", "2.0.0")])],
},
[],
[],
id="dependency-not-installed",
),
pytest.param({("a", "1.0.1"): []}, [], [], id="no-dependencies"),
],
)
def test_cyclic_deps(
capsys: pytest.CaptureFixture[str],
mpkgs: MockGraph,
expected_keys: list[list[str]],
expected_output: list[str],
mock_pkgs: Callable[[MockGraph], Iterator[Mock]],
) -> None:
tree = PackageDAG.from_pkgs(list(mock_pkgs(mpkgs)))
result = cyclic_deps(tree)
result_keys = [[dep.key for dep in deps] for deps in result]
assert sorted(expected_keys) == sorted(result_keys)
render_cycles_text(result)
captured = capsys.readouterr()
assert "\n".join(expected_output).strip() == captured.err.strip()
@pytest.mark.parametrize(
("mpkgs", "expected_keys", "expected_output"),
[
(
{("a", "1.0.1"): [("b", [(">=", "2.3.0")])], ("b", "1.9.1"): []},
{"a": ["b"]},
[
"* a==1.0.1",
" - b [required: >=2.3.0, installed: 1.9.1]",
],
),
(
{("a", "1.0.1"): [("c", [(">=", "9.4.1")])], ("b", "2.3.0"): [("c", [(">=", "7.0")])], ("c", "8.0.1"): []},
{"a": ["c"]},
[
"* a==1.0.1",
" - c [required: >=9.4.1, installed: 8.0.1]",
],
),
(
{("a", "1.0.1"): [("c", [(">=", "9.4.1")])], ("b", "2.3.0"): [("c", [(">=", "9.4.0")])]},
{"a": ["c"], "b": ["c"]},
[
"* a==1.0.1",
" - c [required: >=9.4.1, installed: ?]",
"* b==2.3.0",
" - c [required: >=9.4.0, installed: ?]",
],
),
(
{("a", "1.0.1"): [("c", [(">=", "9.4.1")])], ("b", "2.3.0"): [("c", [(">=", "7.0")])], ("c", "9.4.1"): []},
{},
[],
),
],
)
def test_conflicting_deps(
capsys: pytest.CaptureFixture[str],
mpkgs: MockGraph,
expected_keys: dict[str, list[str]],
expected_output: list[str],
mock_pkgs: Callable[[MockGraph], Iterator[Mock]],
) -> None:
tree = PackageDAG.from_pkgs(list(mock_pkgs(mpkgs)))
result = conflicting_deps(tree)
result_keys = {k.key: [v.key for v in vs] for k, vs in result.items()}
assert expected_keys == result_keys
render_conflicts_text(result)
captured = capsys.readouterr()
assert "\n".join(expected_output).strip() == captured.err.strip()
@pytest.mark.parametrize(
("mpkgs", "expected_output"),
[
(
{("a", "1.0.1"): [("b", [(">=", "2.3.0")])], ("b", "1.9.1"): []},
[
"Warning!!! Possibly conflicting dependencies found:",
"* a==1.0.1",
" - b [required: >=2.3.0, installed: 1.9.1]",
"------------------------------------------------------------------------",
],
),
(
{
("a", "1.0.1"): [("b", [(">=", "2.0.0")])],
("b", "2.3.0"): [("a", [(">=", "1.0.1")])],
("c", "4.5.0"): [],
},
[
"Warning!!! Cyclic dependencies found:",
"* b => a => b",
"* a => b => a",
"------------------------------------------------------------------------",
],
),
],
)
def test_validate(
capsys: pytest.CaptureFixture[str],
mock_pkgs: Callable[[MockGraph], Iterator[Mock]],
mpkgs: MockGraph,
expected_output: list[str],
) -> None:
tree = PackageDAG.from_pkgs(list(mock_pkgs(mpkgs)))
validate(tree)
out, err = capsys.readouterr()
assert len(out) == 0
assert "\n".join(expected_output).strip() == err.strip()
|