forked from python-wheel-build/fromager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bootstrap.py
More file actions
888 lines (787 loc) · 30.3 KB
/
Copy pathtest_bootstrap.py
File metadata and controls
888 lines (787 loc) · 30.3 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
import io
import logging
import pathlib
import textwrap
from datetime import timedelta
from unittest.mock import Mock, patch
import pytest
from click.testing import CliRunner
from packaging.requirements import Requirement
from packaging.version import Version
from fromager import bootstrapper, context, dependency_graph, packagesettings
from fromager.commands import bootstrap
def test_get_requirements_single_arg() -> None:
requirements = bootstrap._get_requirements_from_args(["a"], [])
assert [Requirement("a")] == requirements
def test_get_requirements_multiple_args() -> None:
requirements = bootstrap._get_requirements_from_args(["a", "b"], [])
assert [Requirement("a"), Requirement("b")] == requirements
def test_get_requirements_args_and_file(tmp_path: pathlib.Path) -> None:
requirements_file = tmp_path / "requirements.txt"
requirements_file.write_text("c\n")
requirements = bootstrap._get_requirements_from_args(
["a", "b"], [str(requirements_file)]
)
assert [
Requirement("a"),
Requirement("b"),
Requirement("c"),
] == requirements
def test_ignore_based_on_marker() -> None:
requirements = bootstrap._get_requirements_from_args(
['foo; python_version<"3.9"'], []
)
assert [] == requirements
def test_write_constraints_file_simple() -> None:
buffer = io.StringIO()
raw_graph = {
"": {
"download_url": "",
"pre_built": False,
"version": "0",
"canonicalized_name": "",
"edges": [{"key": "a==1.0", "req_type": "install", "req": "a"}],
},
"a==1.0": {
"download_url": "url for a",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "a",
"edges": [
{"key": "b==2.0", "req_type": "install", "req": "b>=2.0"},
{"key": "c==3.0", "req_type": "install", "req": "c<4.0"},
],
},
"b==2.0": {
"download_url": "url for b",
"pre_built": False,
"version": "2.0",
"canonicalized_name": "b",
"edges": [],
},
"c==3.0": {
"download_url": "url for c",
"pre_built": False,
"version": "3.0",
"canonicalized_name": "c",
"edges": [],
},
}
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
bootstrap.write_constraints_file(graph, buffer)
expected = textwrap.dedent("""
a==1.0
b==2.0
c==3.0
""").lstrip()
assert expected == buffer.getvalue()
def test_write_constraints_file_resolvable_duplicate() -> None:
buffer = io.StringIO()
raw_graph = {
"": {
"download_url": "",
"pre_built": False,
"version": "0",
"canonicalized_name": "",
"edges": [{"key": "a==1.0", "req_type": "install", "req": "a"}],
},
"a==1.0": {
"download_url": "url for a",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "a",
"edges": [
{"key": "b==2.0", "req_type": "install", "req": "b>=2.0"},
{"key": "c==3.0", "req_type": "install", "req": "c<4.0"},
],
},
"b==2.0": {
"download_url": "url for b",
"pre_built": False,
"version": "2.0",
"canonicalized_name": "b",
"edges": [{"key": "c==3.1", "req_type": "install", "req": "c>3.0"}],
},
"c==3.0": {
"download_url": "url for c",
"pre_built": False,
"version": "3.0",
"canonicalized_name": "c",
"edges": [],
},
"c==3.1": {
"download_url": "url for c",
"pre_built": False,
"version": "3.1",
"canonicalized_name": "c",
"edges": [],
},
}
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
bootstrap.write_constraints_file(graph, buffer)
expected = textwrap.dedent("""
a==1.0
b==2.0
# NOTE: fromager selected c==3.1 from: ['3.0', '3.1']
c==3.1
""").lstrip()
assert expected == buffer.getvalue()
def test_write_constraints_file_unresolvable_duplicate() -> None:
"""Test that unresolvable duplicates cause the function to return False and not write conflicting constraints.
This test has conflicting requirements for package 'c':
- a==1.0 requires c==3.0 (exact version 3.0)
- b==2.0 requires c>3.0 (greater than 3.0)
These cannot be satisfied simultaneously, so the function should return False
and only write the packages that CAN be resolved.
"""
buffer = io.StringIO()
raw_graph = {
"": {
"download_url": "",
"pre_built": False,
"version": "0",
"canonicalized_name": "",
"edges": [{"key": "a==1.0", "req_type": "install", "req": "a"}],
},
"a==1.0": {
"download_url": "url for a",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "a",
"edges": [
{"key": "b==2.0", "req_type": "install", "req": "b>=2.0"},
{"key": "c==3.0", "req_type": "install", "req": "c==3.0"},
],
},
"b==2.0": {
"download_url": "url for b",
"pre_built": False,
"version": "2.0",
"canonicalized_name": "b",
"edges": [{"key": "c==3.1", "req_type": "install", "req": "c>3.0"}],
},
"c==3.0": {
"download_url": "url for c",
"pre_built": False,
"version": "3.0",
"canonicalized_name": "c",
"edges": [],
},
"c==3.1": {
"download_url": "url for c",
"pre_built": False,
"version": "3.1",
"canonicalized_name": "c",
"edges": [],
},
}
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
# Should return False due to impossible constraints for package 'c'
result = bootstrap.write_constraints_file(graph, buffer)
assert result is False
# Should only write packages that CAN be resolved (no conflicting 'c' versions)
output = buffer.getvalue()
assert "a==1.0" in output
assert "b==2.0" in output
assert "c==" not in output # No conflicting c versions should be written
def test_write_constraints_file_duplicates() -> None:
buffer = io.StringIO()
raw_graph = {
"": {
"download_url": "",
"pre_built": False,
"version": "0",
"canonicalized_name": "",
"edges": [
{"key": "a==1.0", "req_type": "install", "req": "a"},
{"key": "d==1.0", "req_type": "install", "req": "d"},
],
},
"a==1.0": {
"download_url": "url for a",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "a",
"edges": [
{"key": "c==3.0", "req_type": "install", "req": "c<=3.0"},
],
},
"d==1.0": {
"download_url": "url for a",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "a",
"edges": [
{"key": "c==3.1", "req_type": "install", "req": "c>=3.0"},
],
},
"c==3.0": { # transformers 4.46
"download_url": "url for c",
"pre_built": False,
"version": "3.0",
"canonicalized_name": "c",
"edges": [{"key": "b==2.0", "req_type": "install", "req": "b<2.1,>=2.0"}],
},
"c==3.1": { # transformers 4.47
"download_url": "url for c",
"pre_built": False,
"version": "3.1",
"canonicalized_name": "c",
"edges": [{"key": "b==2.1", "req_type": "install", "req": "b<2.2,>=2.1"}],
},
"b==2.0": { # tokenizer
"download_url": "url for b",
"pre_built": False,
"version": "2.0",
"canonicalized_name": "b",
"edges": [],
},
"b==2.1": { # tokenizer
"download_url": "url for b",
"pre_built": False,
"version": "2.1",
"canonicalized_name": "b",
"edges": [],
},
}
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
# This should return False because there's a genuine constraint conflict:
# c==3.0 requires b<2.1,>=2.0 (only b==2.0 satisfies this)
# c==3.1 requires b<2.2,>=2.1 (only b==2.1 satisfies this)
# No single version of b can satisfy both constraints
result = bootstrap.write_constraints_file(graph, buffer)
assert result is False
# When there are conflicts, no constraints should be written to the output
output_content = buffer.getvalue()
# Should contain resolved packages that don't have conflicts
assert "a==1.0" in output_content
assert "d==1.0" in output_content
# c gets resolved to 3.0 before the conflict with b is detected
assert "c==3.0" in output_content
# Should NOT contain conflicted packages
assert "b==" not in output_content
def test_write_constraints_file_multiples() -> None:
buffer = io.StringIO()
raw_graph = {
"": {
"download_url": "",
"pre_built": False,
"version": "0",
"canonicalized_name": "",
"edges": [
{
"key": "a==2.7.0",
"req_type": "toplevel",
"req": "a==2.7.0",
},
{
"key": "b==0.26.1",
"req_type": "toplevel",
"req": "b==0.26.1",
},
],
},
"b==0.26.1": {
"download_url": "",
"pre_built": False,
"version": "0.26.1",
"canonicalized_name": "b",
"edges": [],
},
"b==0.26.2": {
"download_url": "",
"pre_built": False,
"version": "0.26.2",
"canonicalized_name": "b",
"edges": [],
},
"a==2.7.0": {
"download_url": "",
"pre_built": False,
"version": "2.7.0",
"canonicalized_name": "a",
"edges": [
{
"key": "b==0.26.2",
"req_type": "install",
"req": "b<0.27.0,>=0.26.1",
},
],
},
}
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
assert bootstrap.write_constraints_file(graph, buffer)
expected = textwrap.dedent("""
a==2.7.0
# NOTE: fromager selected b==0.26.2 from: ['0.26.1', '0.26.2']
b==0.26.2
""").lstrip()
assert expected == buffer.getvalue()
def test_write_constraints_file_prevents_false_resolution() -> None:
"""Test that packages marked as unresolvable in early iterations stay unresolvable.
This test validates the fix for the bug where a package could appear unresolvable
in iteration 1 (due to constraints from multiple versions), but then appear
resolvable in iteration 2+ when user counts change after other packages resolve.
Without the fix, 'conflicted' would be incorrectly resolved in a later iteration.
With the fix, it should remain unresolvable throughout all iterations.
"""
buffer = io.StringIO()
raw_graph = {
"": {
"download_url": "",
"pre_built": False,
"version": "0",
"canonicalized_name": "",
"edges": [
{"key": "easy==1.0", "req_type": "toplevel", "req": "easy==1.0"},
{"key": "pkg-a==1.0", "req_type": "toplevel", "req": "pkg-a==1.0"},
{"key": "pkg-b==1.0", "req_type": "toplevel", "req": "pkg-b==1.0"},
],
},
"easy==1.0": {
"download_url": "url",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "easy",
"edges": [], # No dependencies, resolves immediately
},
"pkg-a==1.0": {
"download_url": "url",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "pkg-a",
"edges": [
{
"key": "conflicted==1.0",
"req_type": "install",
"req": "conflicted<1.5",
},
{
"key": "intermediate==1.0",
"req_type": "install",
"req": "intermediate",
},
],
},
"pkg-b==1.0": {
"download_url": "url",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "pkg-b",
"edges": [
{
"key": "conflicted==2.0",
"req_type": "install",
"req": "conflicted>=2.0",
},
],
},
"intermediate==1.0": {
"download_url": "url",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "intermediate",
"edges": [
{
"key": "conflicted==1.0",
"req_type": "install",
"req": "conflicted<1.5",
},
],
},
"conflicted==1.0": {
"download_url": "url",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "conflicted",
"edges": [],
},
"conflicted==2.0": {
"download_url": "url",
"pre_built": False,
"version": "2.0",
"canonicalized_name": "conflicted",
"edges": [],
},
}
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
result = bootstrap.write_constraints_file(graph, buffer)
# Should return False because 'conflicted' has impossible constraints:
# - pkg-a and intermediate need conflicted<1.5 (only 1.0 satisfies)
# - pkg-b needs conflicted>=2.0 (only 2.0 satisfies)
# - No version satisfies both <1.5 AND >=2.0
assert result is False
# Verify that packages that CAN be resolved are written
output_content = buffer.getvalue()
assert "easy==1.0" in output_content
assert "pkg-a==1.0" in output_content
assert "pkg-b==1.0" in output_content
assert "intermediate==1.0" in output_content
# Verify that the conflicted package is NOT written
assert "conflicted==" not in output_content
def test_to_constraints_command_no_file_on_failure(tmp_path: pathlib.Path) -> None:
"""Test that the to-constraints logic doesn't create output files when there are constraint conflicts.
This is a regression test for the bug where constraint resolution would write partial
output even when it ultimately failed.
"""
import io
from fromager import dependency_graph
# Create a graph with conflicts (same as test_write_constraints_file_unresolvable_duplicate)
raw_graph = {
"": {
"download_url": "",
"pre_built": False,
"version": "0",
"canonicalized_name": "",
"edges": [{"key": "a==1.0", "req_type": "install", "req": "a"}],
},
"a==1.0": {
"download_url": "url for a",
"pre_built": False,
"version": "1.0",
"canonicalized_name": "a",
"edges": [
{"key": "b==2.0", "req_type": "install", "req": "b>=2.0"},
{"key": "c==3.0", "req_type": "install", "req": "c==3.0"},
],
},
"b==2.0": {
"download_url": "url for b",
"pre_built": False,
"version": "2.0",
"canonicalized_name": "b",
"edges": [{"key": "c==3.1", "req_type": "install", "req": "c>3.0"}],
},
"c==3.0": {
"download_url": "url for c",
"pre_built": False,
"version": "3.0",
"canonicalized_name": "c",
"edges": [],
},
"c==3.1": {
"download_url": "url for c",
"pre_built": False,
"version": "3.1",
"canonicalized_name": "c",
"edges": [],
},
}
graph = dependency_graph.DependencyGraph.from_dict(raw_graph)
# Test the new to-constraints logic: use buffer first, only create file on success
output_file = tmp_path / "constraints.txt"
# Simulate the fixed to-constraints behavior
buffer = io.StringIO()
result = bootstrap.write_constraints_file(graph, buffer)
# Should return False due to conflicts
assert result is False
# Because result is False, the output file should NOT be created
# (In the old buggy behavior, the file would have been created with partial content)
# Since result is False, we shouldn't write to the actual file
# This simulates the fix where we check the result before creating the output file
if result:
with open(output_file, "w") as f:
f.write(buffer.getvalue())
# Verify the output file was NOT created
assert not output_file.exists(), (
f"Output file {output_file} should not have been created when constraint resolution failed"
)
def test_skip_constraints_cli_option() -> None:
"""Test that the --skip-constraints option is available in the CLI"""
runner = CliRunner()
result = runner.invoke(bootstrap.bootstrap, ["--help"])
# Check that the help text includes our new option
assert "--skip-constraints" in result.output
assert "Skip generating constraints.txt file" in result.output
@patch("fromager.commands.bootstrap.bootstrapper.Bootstrapper")
@patch("fromager.commands.bootstrap.server.start_wheel_server")
@patch("fromager.commands.bootstrap.progress.progress_context")
@patch("fromager.commands.bootstrap.metrics.summarize")
def test_multiple_versions_auto_disables_constraints(
mock_metrics: Mock,
mock_progress: Mock,
mock_server: Mock,
mock_bootstrapper: Mock,
tmp_context: context.WorkContext,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that --multiple-versions alone auto-disables constraints and logs message"""
# Setup mocks
mock_progress.return_value.__enter__.return_value = Mock()
mock_progress.return_value.__exit__.return_value = None
mock_bt_instance = Mock()
mock_bt_instance.finalize.return_value = 0
mock_bootstrapper.return_value = mock_bt_instance
runner = CliRunner()
with runner.isolated_filesystem():
# Create a temporary requirements file
pathlib.Path("req.txt").write_text("setuptools>=60\n")
# Invoke with --multiple-versions but NOT --skip-constraints
with caplog.at_level(logging.INFO):
result = runner.invoke(
bootstrap.bootstrap,
[
"-r",
"req.txt",
"--multiple-versions",
"--max-release-age",
"45",
],
obj=tmp_context,
)
# Should succeed
assert result.exit_code == 0
# Should log that constraints are auto-disabled
assert "automatically disabling constraints generation" in caplog.text
assert "incompatible with --multiple-versions" in caplog.text
@patch("fromager.commands.bootstrap.bootstrapper.Bootstrapper")
@patch("fromager.commands.bootstrap.server.start_wheel_server")
@patch("fromager.commands.bootstrap.progress.progress_context")
@patch("fromager.commands.bootstrap.metrics.summarize")
def test_multiple_versions_with_skip_constraints_no_duplicate_log(
mock_metrics: Mock,
mock_progress: Mock,
mock_server: Mock,
mock_bootstrapper: Mock,
tmp_context: context.WorkContext,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that --multiple-versions --skip-constraints together doesn't log auto-disable message"""
# Setup mocks
mock_progress.return_value.__enter__.return_value = Mock()
mock_progress.return_value.__exit__.return_value = None
mock_bt_instance = Mock()
mock_bt_instance.finalize.return_value = 0
mock_bootstrapper.return_value = mock_bt_instance
runner = CliRunner()
with runner.isolated_filesystem():
# Create a temporary requirements file
pathlib.Path("req.txt").write_text("setuptools>=60\n")
# Invoke with BOTH --multiple-versions AND --skip-constraints
with caplog.at_level(logging.INFO):
result = runner.invoke(
bootstrap.bootstrap,
[
"-r",
"req.txt",
"--multiple-versions",
"--skip-constraints",
"--max-release-age",
"45",
],
obj=tmp_context,
)
# Should succeed
assert result.exit_code == 0
# Should NOT log the auto-disable message (already disabled by user)
assert "automatically disabling constraints generation" not in caplog.text
@patch("fromager.commands.bootstrap.bootstrapper.Bootstrapper")
@patch("fromager.commands.bootstrap.server.start_wheel_server")
@patch("fromager.commands.bootstrap.progress.progress_context")
@patch("fromager.commands.bootstrap.metrics.summarize")
@patch("fromager.commands.bootstrap.write_constraints_file")
def test_without_multiple_versions_constraints_not_disabled(
mock_write_constraints: Mock,
mock_metrics: Mock,
mock_progress: Mock,
mock_server: Mock,
mock_bootstrapper: Mock,
tmp_context: context.WorkContext,
) -> None:
"""Test that without --multiple-versions, constraints are not auto-disabled"""
# Setup mocks
mock_progress.return_value.__enter__.return_value = Mock()
mock_progress.return_value.__exit__.return_value = None
mock_bt_instance = Mock()
mock_bt_instance.finalize.return_value = 0
mock_bootstrapper.return_value = mock_bt_instance
mock_write_constraints.return_value = True
runner = CliRunner()
with runner.isolated_filesystem():
# Create a temporary requirements file
pathlib.Path("req.txt").write_text("setuptools>=60\n")
# Invoke WITHOUT --multiple-versions
result = runner.invoke(
bootstrap.bootstrap,
[
"-r",
"req.txt",
],
obj=tmp_context,
)
# Should succeed
assert result.exit_code == 0
# write_constraints_file should have been called (constraints NOT disabled)
assert mock_write_constraints.called
def test_max_release_age_rejects_zero(
tmp_context: context.WorkContext,
) -> None:
"""Test that --max-release-age 0 is rejected by Click's IntRange(min=1)."""
runner = CliRunner()
with runner.isolated_filesystem():
pathlib.Path("req.txt").write_text("setuptools>=60\n")
result = runner.invoke(
bootstrap.bootstrap,
[
"-r",
"req.txt",
"--max-release-age",
"0",
],
obj=tmp_context,
)
assert result.exit_code == 2
@patch("fromager.commands.bootstrap.bootstrapper.Bootstrapper")
@patch("fromager.commands.bootstrap.server.start_wheel_server")
@patch("fromager.commands.bootstrap.progress.progress_context")
@patch("fromager.commands.bootstrap.metrics.summarize")
def test_max_release_age_sets_context(
mock_metrics: Mock,
mock_progress: Mock,
mock_server: Mock,
mock_bootstrapper: Mock,
tmp_context: context.WorkContext,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that --max-release-age stores the value on WorkContext."""
mock_progress.return_value.__enter__.return_value = Mock()
mock_progress.return_value.__exit__.return_value = None
mock_bt_instance = Mock()
mock_bt_instance.finalize.return_value = 0
mock_bootstrapper.return_value = mock_bt_instance
runner = CliRunner()
with runner.isolated_filesystem():
pathlib.Path("req.txt").write_text("setuptools>=60\n")
with caplog.at_level(logging.INFO):
result = runner.invoke(
bootstrap.bootstrap,
[
"-r",
"req.txt",
"--multiple-versions",
"--max-release-age",
"45",
],
obj=tmp_context,
)
assert result.exit_code == 0
assert tmp_context.max_release_age == timedelta(days=45)
assert "rejecting versions older than 45 days" in caplog.text
@patch("fromager.gitutils.git_clone")
def test_resolve_version_from_git_url_with_submodules_enabled(
mock_git_clone: Mock,
tmp_context: context.WorkContext,
) -> None:
"""Test that git_clone is called with submodules=True when configured."""
req = Requirement("test-pkg @ git+https://github.com/example/repo.git")
mock_git_options = packagesettings.GitOptions(submodules=True)
with patch.object(tmp_context, "package_build_info") as mock_pbi:
mock_pbi_instance = Mock()
mock_pbi_instance.git_options = mock_git_options
mock_pbi.return_value = mock_pbi_instance
with patch(
"fromager.bootstrapper.Bootstrapper._get_version_from_package_metadata"
) as mock_get_version:
with patch("shutil.move"):
with patch("pathlib.Path.mkdir"):
mock_get_version.return_value = Version("1.0.0")
# Execute
bs = bootstrapper.Bootstrapper(tmp_context)
try:
bs._resolve_version_from_git_url(req)
except AssertionError:
# Expected since we're mocking everything
pass
mock_git_clone.assert_called_once()
call_args = mock_git_clone.call_args
assert call_args.kwargs["submodules"] is True
assert call_args.kwargs["repo_url"] == "https://github.com/example/repo.git"
assert call_args.kwargs["ref"] is None
@patch("fromager.gitutils.git_clone")
def test_resolve_version_from_git_url_with_specific_submodule_paths(
mock_git_clone: Mock,
tmp_context: context.WorkContext,
) -> None:
"""Test that git_clone is called with specific submodule paths when configured."""
req = Requirement("test-pkg @ git+https://github.com/example/repo.git")
mock_git_options = packagesettings.GitOptions(
submodule_paths=["vendor/lib1", "vendor/lib2"]
)
with patch.object(tmp_context, "package_build_info") as mock_pbi:
mock_pbi_instance = Mock()
mock_pbi_instance.git_options = mock_git_options
mock_pbi.return_value = mock_pbi_instance
with patch(
"fromager.bootstrapper.Bootstrapper._get_version_from_package_metadata"
) as mock_get_version:
with patch("shutil.move"):
with patch("pathlib.Path.mkdir"):
mock_get_version.return_value = Version("1.0.0")
bs = bootstrapper.Bootstrapper(tmp_context)
try:
bs._resolve_version_from_git_url(req)
except AssertionError:
# Expected since we're mocking everything
pass
mock_git_clone.assert_called_once()
call_args = mock_git_clone.call_args
assert call_args.kwargs["submodules"] == ["vendor/lib1", "vendor/lib2"]
@patch("fromager.gitutils.git_clone")
def test_resolve_version_from_git_url_with_submodules_disabled(
mock_git_clone: Mock,
tmp_context: context.WorkContext,
) -> None:
"""Test that git_clone is called with submodules=False by default."""
req = Requirement("test-pkg @ git+https://github.com/example/repo.git")
with patch(
"fromager.bootstrapper.Bootstrapper._get_version_from_package_metadata"
) as mock_get_version:
with patch("shutil.move"):
with patch("pathlib.Path.mkdir"):
mock_get_version.return_value = Version("1.0.0")
bs = bootstrapper.Bootstrapper(tmp_context)
try:
bs._resolve_version_from_git_url(req)
except AssertionError:
# Expected since we're mocking everything
pass
mock_git_clone.assert_called_once()
call_args = mock_git_clone.call_args
assert call_args.kwargs["submodules"] is False
@patch("fromager.gitutils.git_clone")
def test_resolve_version_from_git_url_with_git_ref(
mock_git_clone: Mock,
tmp_context: context.WorkContext,
) -> None:
"""Test that git_clone is called with the correct ref when URL includes @ref."""
req = Requirement("test-pkg @ git+https://github.com/example/repo.git@v1.2.3")
mock_git_options = packagesettings.GitOptions(submodules=True)
with patch.object(tmp_context, "package_build_info") as mock_pbi:
mock_pbi_instance = Mock()
mock_pbi_instance.git_options = mock_git_options
mock_pbi.return_value = mock_pbi_instance
with patch(
"fromager.bootstrapper.Bootstrapper._get_version_from_package_metadata"
) as mock_get_version:
with patch("shutil.move"):
with patch("pathlib.Path.mkdir"):
mock_get_version.return_value = Version("1.2.3")
bs = bootstrapper.Bootstrapper(tmp_context)
try:
bs._resolve_version_from_git_url(req)
except AssertionError:
# Expected since we're mocking everything
pass
mock_git_clone.assert_called_once()
call_args = mock_git_clone.call_args
assert call_args.kwargs["submodules"] is True
assert call_args.kwargs["repo_url"] == "https://github.com/example/repo.git"
assert call_args.kwargs["ref"] == "v1.2.3"
def test_resolve_version_from_git_url_invalid_scheme(
tmp_context: context.WorkContext,
) -> None:
"""Test that non-git URLs raise ValueError."""
req = Requirement("test-pkg @ https://github.com/example/repo.git")
bs = bootstrapper.Bootstrapper(tmp_context)
with pytest.raises(ValueError, match="unable to handle URL scheme"):
bs._resolve_version_from_git_url(req)