|
| 1 | +"""Test graph command functions that display constraint information.""" |
| 2 | + |
| 3 | +from packaging.requirements import Requirement |
| 4 | +from packaging.utils import canonicalize_name |
| 5 | +from packaging.version import Version |
| 6 | + |
| 7 | +from fromager import dependency_graph |
| 8 | +from fromager.commands.graph import find_why, show_explain_duplicates |
| 9 | +from fromager.requirements_file import RequirementType |
| 10 | + |
| 11 | + |
| 12 | +def test_show_explain_duplicates_with_constraints(capsys): |
| 13 | + """Test that explain_duplicates shows constraint information.""" |
| 14 | + # Create a graph with duplicate dependencies that have constraints |
| 15 | + graph = dependency_graph.DependencyGraph() |
| 16 | + |
| 17 | + # Add top-level package |
| 18 | + graph.add_dependency( |
| 19 | + parent_name=None, |
| 20 | + parent_version=None, |
| 21 | + req_type=RequirementType.TOP_LEVEL, |
| 22 | + req=Requirement("package-a"), |
| 23 | + req_version=Version("1.0.0"), |
| 24 | + download_url="https://example.com/package-a-1.0.0.tar.gz", |
| 25 | + ) |
| 26 | + |
| 27 | + # Add package-b version 1.0.0 as dependency of package-a with constraint |
| 28 | + graph.add_dependency( |
| 29 | + parent_name=canonicalize_name("package-a"), |
| 30 | + parent_version=Version("1.0.0"), |
| 31 | + req_type=RequirementType.INSTALL, |
| 32 | + req=Requirement("package-b>=1.0"), |
| 33 | + req_version=Version("1.0.0"), |
| 34 | + download_url="https://example.com/package-b-1.0.0.tar.gz", |
| 35 | + constraint="package-b>=1.0,<2.0", |
| 36 | + ) |
| 37 | + |
| 38 | + # Add another top-level package |
| 39 | + graph.add_dependency( |
| 40 | + parent_name=None, |
| 41 | + parent_version=None, |
| 42 | + req_type=RequirementType.TOP_LEVEL, |
| 43 | + req=Requirement("package-c"), |
| 44 | + req_version=Version("1.0.0"), |
| 45 | + download_url="https://example.com/package-c-1.0.0.tar.gz", |
| 46 | + ) |
| 47 | + |
| 48 | + # Add package-b version 2.0.0 as dependency of package-c without constraint |
| 49 | + graph.add_dependency( |
| 50 | + parent_name=canonicalize_name("package-c"), |
| 51 | + parent_version=Version("1.0.0"), |
| 52 | + req_type=RequirementType.INSTALL, |
| 53 | + req=Requirement("package-b>=2.0"), |
| 54 | + req_version=Version("2.0.0"), |
| 55 | + download_url="https://example.com/package-b-2.0.0.tar.gz", |
| 56 | + constraint="", |
| 57 | + ) |
| 58 | + |
| 59 | + # Run the command |
| 60 | + show_explain_duplicates(graph) |
| 61 | + |
| 62 | + # Capture output |
| 63 | + captured = capsys.readouterr() |
| 64 | + |
| 65 | + # Verify constraint is shown at the package name level, not per-version |
| 66 | + assert "package-b (constraint: package-b>=1.0,<2.0)" in captured.out |
| 67 | + # Versions should be shown without constraint info |
| 68 | + assert " 1.0.0\n" in captured.out |
| 69 | + assert " 2.0.0\n" in captured.out |
| 70 | + # Version lines should not have constraint info |
| 71 | + assert "1.0.0 (constraint:" not in captured.out |
| 72 | + assert "2.0.0 (constraint:" not in captured.out |
| 73 | + |
| 74 | + |
| 75 | +def test_find_why_with_constraints(capsys): |
| 76 | + """Test that why command shows constraint information.""" |
| 77 | + # Create a graph with constraints |
| 78 | + graph = dependency_graph.DependencyGraph() |
| 79 | + |
| 80 | + # Add top-level package with constraint |
| 81 | + graph.add_dependency( |
| 82 | + parent_name=None, |
| 83 | + parent_version=None, |
| 84 | + req_type=RequirementType.TOP_LEVEL, |
| 85 | + req=Requirement("parent-pkg"), |
| 86 | + req_version=Version("1.0.0"), |
| 87 | + download_url="https://example.com/parent-pkg-1.0.0.tar.gz", |
| 88 | + constraint="parent-pkg==1.0.0", |
| 89 | + ) |
| 90 | + |
| 91 | + # Add child dependency with its own constraint |
| 92 | + graph.add_dependency( |
| 93 | + parent_name=canonicalize_name("parent-pkg"), |
| 94 | + parent_version=Version("1.0.0"), |
| 95 | + req_type=RequirementType.INSTALL, |
| 96 | + req=Requirement("child-pkg>=1.0"), |
| 97 | + req_version=Version("1.5.0"), |
| 98 | + download_url="https://example.com/child-pkg-1.5.0.tar.gz", |
| 99 | + constraint="child-pkg>=1.0,<2.0", |
| 100 | + ) |
| 101 | + |
| 102 | + # Find why child-pkg is included |
| 103 | + child_node = graph.nodes["child-pkg==1.5.0"] |
| 104 | + find_why(graph, child_node, 1, 0, []) |
| 105 | + |
| 106 | + # Capture output |
| 107 | + captured = capsys.readouterr() |
| 108 | + |
| 109 | + # Verify constraint is shown for the child package at depth 0 |
| 110 | + assert "child-pkg==1.5.0 (constraint: child-pkg>=1.0,<2.0)" in captured.out |
| 111 | + # Verify constraint is shown for the parent when showing the dependency relationship |
| 112 | + assert "(constraint: parent-pkg==1.0.0)" in captured.out |
| 113 | + |
| 114 | + |
| 115 | +def test_find_why_toplevel_with_constraint(capsys): |
| 116 | + """Test that why command shows constraint for top-level dependencies.""" |
| 117 | + # Create a graph with a top-level package that has a constraint |
| 118 | + graph = dependency_graph.DependencyGraph() |
| 119 | + |
| 120 | + # Add top-level package with constraint |
| 121 | + graph.add_dependency( |
| 122 | + parent_name=None, |
| 123 | + parent_version=None, |
| 124 | + req_type=RequirementType.TOP_LEVEL, |
| 125 | + req=Requirement("toplevel-pkg"), |
| 126 | + req_version=Version("2.0.0"), |
| 127 | + download_url="https://example.com/toplevel-pkg-2.0.0.tar.gz", |
| 128 | + constraint="toplevel-pkg>=2.0,<3.0", |
| 129 | + ) |
| 130 | + |
| 131 | + # Find why toplevel-pkg is included |
| 132 | + node = graph.nodes["toplevel-pkg==2.0.0"] |
| 133 | + find_why(graph, node, 0, 0, []) |
| 134 | + |
| 135 | + # Capture output |
| 136 | + captured = capsys.readouterr() |
| 137 | + |
| 138 | + # Verify constraint is shown at depth 0 |
| 139 | + assert "toplevel-pkg==2.0.0 (constraint: toplevel-pkg>=2.0,<3.0)" in captured.out |
| 140 | + # Verify constraint is shown when identifying it as a top-level dependency |
| 141 | + assert ( |
| 142 | + "toplevel-pkg==2.0.0 (constraint: toplevel-pkg>=2.0,<3.0) is a toplevel dependency" |
| 143 | + in captured.out |
| 144 | + ) |
| 145 | + |
| 146 | + |
| 147 | +def test_find_why_without_constraints(capsys): |
| 148 | + """Test that why command works when no constraints are present.""" |
| 149 | + # Create a graph without constraints |
| 150 | + graph = dependency_graph.DependencyGraph() |
| 151 | + |
| 152 | + # Add top-level package without constraint |
| 153 | + graph.add_dependency( |
| 154 | + parent_name=None, |
| 155 | + parent_version=None, |
| 156 | + req_type=RequirementType.TOP_LEVEL, |
| 157 | + req=Requirement("simple-pkg"), |
| 158 | + req_version=Version("1.0.0"), |
| 159 | + download_url="https://example.com/simple-pkg-1.0.0.tar.gz", |
| 160 | + ) |
| 161 | + |
| 162 | + # Add child dependency without constraint |
| 163 | + graph.add_dependency( |
| 164 | + parent_name=canonicalize_name("simple-pkg"), |
| 165 | + parent_version=Version("1.0.0"), |
| 166 | + req_type=RequirementType.INSTALL, |
| 167 | + req=Requirement("simple-child"), |
| 168 | + req_version=Version("2.0.0"), |
| 169 | + download_url="https://example.com/simple-child-2.0.0.tar.gz", |
| 170 | + ) |
| 171 | + |
| 172 | + # Find why simple-child is included |
| 173 | + child_node = graph.nodes["simple-child==2.0.0"] |
| 174 | + find_why(graph, child_node, 1, 0, []) |
| 175 | + |
| 176 | + # Capture output |
| 177 | + captured = capsys.readouterr() |
| 178 | + |
| 179 | + # Verify no constraint info is shown |
| 180 | + assert "(constraint:" not in captured.out |
| 181 | + assert "simple-child==2.0.0" in captured.out |
| 182 | + assert "simple-pkg==1.0.0" in captured.out |
0 commit comments