-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_graph_commands.py
More file actions
460 lines (393 loc) · 15.5 KB
/
Copy pathtest_graph_commands.py
File metadata and controls
460 lines (393 loc) · 15.5 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
"""Test graph command functions that display constraint information."""
from unittest.mock import Mock
import pytest
from packaging.requirements import Requirement
from packaging.utils import canonicalize_name
from packaging.version import Version
from fromager import dependency_graph
from fromager.commands.graph import (
_find_customized_dependencies_for_node,
find_why,
show_explain_duplicates,
)
from fromager.requirements_file import RequirementType
def test_show_explain_duplicates_with_constraints(
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test that explain_duplicates shows constraint information."""
# Create a graph with duplicate dependencies that have constraints
graph = dependency_graph.DependencyGraph()
# Add top-level package
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("package-a"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-a-1.0.0.tar.gz",
)
# Add package-b version 1.0.0 as dependency of package-a with constraint
graph.add_dependency(
parent_name=canonicalize_name("package-a"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-b>=1.0"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-b-1.0.0.tar.gz",
constraint=Requirement("package-b>=1.0,<2.0"),
)
# Add another top-level package
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("package-c"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-c-1.0.0.tar.gz",
)
# Add package-b version 2.0.0 as dependency of package-c without constraint
graph.add_dependency(
parent_name=canonicalize_name("package-c"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-b>=2.0"),
req_version=Version("2.0.0"),
download_url="https://example.com/package-b-2.0.0.tar.gz",
constraint=None,
)
# Run the command
show_explain_duplicates(graph)
# Capture output
captured = capsys.readouterr()
# Verify constraint is shown at the package name level, not per-version
assert "package-b (constraint: package-b<2.0,>=1.0)" in captured.out
# Versions should be shown without constraint info
assert " 1.0.0\n" in captured.out
assert " 2.0.0\n" in captured.out
# Version lines should not have constraint info
assert "1.0.0 (constraint:" not in captured.out
assert "2.0.0 (constraint:" not in captured.out
def test_find_why_with_constraints(
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test that why command shows constraint information."""
# Create a graph with constraints
graph = dependency_graph.DependencyGraph()
# Add top-level package with constraint
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("parent-pkg"),
req_version=Version("1.0.0"),
download_url="https://example.com/parent-pkg-1.0.0.tar.gz",
constraint=Requirement("parent-pkg==1.0.0"),
)
# Add child dependency with its own constraint
graph.add_dependency(
parent_name=canonicalize_name("parent-pkg"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("child-pkg>=1.0"),
req_version=Version("1.5.0"),
download_url="https://example.com/child-pkg-1.5.0.tar.gz",
constraint=Requirement("child-pkg>=1.0,<2.0"),
)
# Find why child-pkg is included
child_node = graph.nodes["child-pkg==1.5.0"]
find_why(graph, child_node, 1, 0, [])
# Capture output
captured = capsys.readouterr()
# Verify constraint is shown for the child package at depth 0
assert "child-pkg==1.5.0 (constraint: child-pkg<2.0,>=1.0)" in captured.out
# Verify constraint is shown for the parent when showing the dependency relationship
assert "(constraint: parent-pkg==1.0.0)" in captured.out
def test_find_why_toplevel_with_constraint(
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test that why command shows constraint for top-level dependencies."""
# Create a graph with a top-level package that has a constraint
graph = dependency_graph.DependencyGraph()
# Add top-level package with constraint
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("toplevel-pkg"),
req_version=Version("2.0.0"),
download_url="https://example.com/toplevel-pkg-2.0.0.tar.gz",
constraint=Requirement("toplevel-pkg<3.0,>=2.0"),
)
# Find why toplevel-pkg is included
node = graph.nodes["toplevel-pkg==2.0.0"]
find_why(graph, node, 0, 0, [])
# Capture output
captured = capsys.readouterr()
# Verify constraint is shown at depth 0
assert "toplevel-pkg==2.0.0 (constraint: toplevel-pkg<3.0,>=2.0)" in captured.out
# Verify constraint is shown when identifying it as a top-level dependency
assert (
"toplevel-pkg==2.0.0 (constraint: toplevel-pkg<3.0,>=2.0) is a toplevel dependency"
in captured.out
)
def test_find_why_without_constraints(
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test that why command works when no constraints are present."""
# Create a graph without constraints
graph = dependency_graph.DependencyGraph()
# Add top-level package without constraint
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("simple-pkg"),
req_version=Version("1.0.0"),
download_url="https://example.com/simple-pkg-1.0.0.tar.gz",
)
# Add child dependency without constraint
graph.add_dependency(
parent_name=canonicalize_name("simple-pkg"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("simple-child"),
req_version=Version("2.0.0"),
download_url="https://example.com/simple-child-2.0.0.tar.gz",
)
# Find why simple-child is included
child_node = graph.nodes["simple-child==2.0.0"]
find_why(graph, child_node, 1, 0, [])
# Capture output
captured = capsys.readouterr()
# Verify no constraint info is shown
assert "(constraint:" not in captured.out
assert "simple-child==2.0.0" in captured.out
assert "simple-pkg==1.0.0" in captured.out
def test_find_direct_customized_dependency() -> None:
"""Test finding a direct child with customizations."""
# Setup: A -> B (customized)
graph = dependency_graph.DependencyGraph()
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("package-a"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-a-1.0.0.tar.gz",
)
graph.add_dependency(
parent_name=canonicalize_name("package-a"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-b>=1.0"),
req_version=Version("2.0.0"),
download_url="https://example.com/package-b-2.0.0.tar.gz",
)
# Mock context to mark package-b as customized
mock_ctx = Mock()
mock_settings = Mock()
def mock_pbi(name: str) -> Mock:
pbi = Mock()
pbi.has_customizations = name == "package-b"
return pbi
mock_settings.package_build_info = mock_pbi
mock_ctx.settings = mock_settings
# Test
node_a = graph.nodes["package-a==1.0.0"]
result = _find_customized_dependencies_for_node(
mock_ctx, node_a, install_only=False
)
# Verify
assert len(result) == 1
assert "package-b==2.0.0" in result
assert result["package-b==2.0.0"] == "package-b>=1.0"
def test_find_transitive_customized_dependency() -> None:
"""Test finding customized dependency through non-customized intermediate."""
# Setup: A -> B (not customized) -> C (customized)
graph = dependency_graph.DependencyGraph()
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("package-a"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-a-1.0.0.tar.gz",
)
graph.add_dependency(
parent_name=canonicalize_name("package-a"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-b>=1.0"),
req_version=Version("2.0.0"),
download_url="https://example.com/package-b-2.0.0.tar.gz",
)
graph.add_dependency(
parent_name=canonicalize_name("package-b"),
parent_version=Version("2.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-c"),
req_version=Version("3.0.0"),
download_url="https://example.com/package-c-3.0.0.tar.gz",
)
# Mock context: only package-c is customized
mock_ctx = Mock()
mock_settings = Mock()
def mock_pbi(name: str) -> Mock:
pbi = Mock()
pbi.has_customizations = name == "package-c"
return pbi
mock_settings.package_build_info = mock_pbi
mock_ctx.settings = mock_settings
# Test
node_a = graph.nodes["package-a==1.0.0"]
result = _find_customized_dependencies_for_node(
mock_ctx, node_a, install_only=False
)
# Verify: Should find C (not B), with A's original requirement
assert len(result) == 1
assert "package-c==3.0.0" in result
assert result["package-c==3.0.0"] == "package-b>=1.0" # A's requirement, not B's
def test_install_only_skips_build_dependencies() -> None:
"""Test that install_only=True skips build dependencies."""
# Setup: A -> B (build dep, customized)
graph = dependency_graph.DependencyGraph()
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("package-a"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-a-1.0.0.tar.gz",
)
graph.add_dependency(
parent_name=canonicalize_name("package-a"),
parent_version=Version("1.0.0"),
req_type=RequirementType.BUILD_BACKEND, # Build dependency
req=Requirement("package-b"),
req_version=Version("2.0.0"),
download_url="https://example.com/package-b-2.0.0.tar.gz",
)
# Mock context: package-b is customized
mock_ctx = Mock()
mock_settings = Mock()
def mock_pbi(name: str) -> Mock:
pbi = Mock()
pbi.has_customizations = name == "package-b"
return pbi
mock_settings.package_build_info = mock_pbi
mock_ctx.settings = mock_settings
# Test with install_only=True
node_a = graph.nodes["package-a==1.0.0"]
result = _find_customized_dependencies_for_node(mock_ctx, node_a, install_only=True)
# Verify: Should be empty (build dep skipped)
assert len(result) == 0
def test_cycle_prevention_no_infinite_loop() -> None:
"""Test that circular dependencies don't cause infinite loops."""
# Setup: A -> B -> C -> A (cycle)
graph = dependency_graph.DependencyGraph()
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("package-a"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-a-1.0.0.tar.gz",
)
graph.add_dependency(
parent_name=canonicalize_name("package-a"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-b"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-b-1.0.0.tar.gz",
)
graph.add_dependency(
parent_name=canonicalize_name("package-b"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-c"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-c-1.0.0.tar.gz",
)
# Create cycle: C -> A
graph.add_dependency(
parent_name=canonicalize_name("package-c"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-a"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-a-1.0.0.tar.gz",
)
# Mock context: only package-c is customized
mock_ctx = Mock()
mock_settings = Mock()
def mock_pbi(name: str) -> Mock:
pbi = Mock()
pbi.has_customizations = name == "package-c"
return pbi
mock_settings.package_build_info = mock_pbi
mock_ctx.settings = mock_settings
# Test: Should not hang or raise error
node_a = graph.nodes["package-a==1.0.0"]
result = _find_customized_dependencies_for_node(
mock_ctx, node_a, install_only=False
)
# Verify: Should find C once, despite cycle
assert len(result) == 1
assert "package-c==1.0.0" in result
def test_requirement_preservation_through_chain() -> None:
"""Test that original requirement is preserved through dependency chain."""
# Setup: A requires "package-b>=1.0,<2.0"
# A -> B -> C (customized)
graph = dependency_graph.DependencyGraph()
graph.add_dependency(
parent_name=None,
parent_version=None,
req_type=RequirementType.TOP_LEVEL,
req=Requirement("package-a"),
req_version=Version("1.0.0"),
download_url="https://example.com/package-a-1.0.0.tar.gz",
)
# A's requirement of B is specific
graph.add_dependency(
parent_name=canonicalize_name("package-a"),
parent_version=Version("1.0.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-b>=1.0,<2.0"),
req_version=Version("1.5.0"),
download_url="https://example.com/package-b-1.5.0.tar.gz",
)
# B's requirement of C is different
graph.add_dependency(
parent_name=canonicalize_name("package-b"),
parent_version=Version("1.5.0"),
req_type=RequirementType.INSTALL,
req=Requirement("package-c>=3.0"),
req_version=Version("3.5.0"),
download_url="https://example.com/package-c-3.5.0.tar.gz",
)
# Mock context: only package-c is customized
mock_ctx = Mock()
mock_settings = Mock()
def mock_pbi(name: str) -> Mock:
pbi = Mock()
pbi.has_customizations = name == "package-c"
return pbi
mock_settings.package_build_info = mock_pbi
mock_ctx.settings = mock_settings
# Test
node_a = graph.nodes["package-a==1.0.0"]
result = _find_customized_dependencies_for_node(
mock_ctx, node_a, install_only=False
)
# Verify: Should preserve A's original requirement, not B's
assert len(result) == 1
assert "package-c==3.5.0" in result
# Should be A's requirement of B (the first in the chain), not B's requirement of C
# Note: The packaging library may normalize the requirement string order
requirement_str = result["package-c==3.5.0"]
assert "package-b" in requirement_str
assert ">=1.0" in requirement_str or ">= 1.0" in requirement_str
assert "<2.0" in requirement_str or "< 2.0" in requirement_str
# Should NOT be B's requirement of C
assert "package-c" not in requirement_str