-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_bootstrap_test_mode.py
More file actions
297 lines (253 loc) · 10.8 KB
/
Copy pathtest_bootstrap_test_mode.py
File metadata and controls
297 lines (253 loc) · 10.8 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
"""Tests for --test-mode feature.
Tests the test mode functionality:
- Bootstrapper initialization with test_mode flag
- Exception handling: catch errors, log, continue
- Bootstrapper.finalize() exit codes
- JSON failure report generation
- failure_type field for categorizing failures
"""
import json
import pathlib
from unittest import mock
import pytest
from packaging.requirements import Requirement
from fromager import bootstrapper, context
class TestBootstrapperInitialization:
"""Test Bootstrapper initialization with test_mode parameter."""
def test_test_mode_enabled(self, tmp_context: context.WorkContext) -> None:
"""Test Bootstrapper with test_mode=True."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
assert bt.test_mode is True
assert isinstance(bt.failed_packages, list)
assert len(bt.failed_packages) == 0
def test_test_mode_disabled_by_default(
self, tmp_context: context.WorkContext
) -> None:
"""Test Bootstrapper with test_mode=False (default)."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context)
assert bt.test_mode is False
def test_test_mode_incompatible_with_sdist_only(
self, tmp_context: context.WorkContext
) -> None:
"""Test that test_mode and sdist_only are mutually exclusive."""
with pytest.raises(ValueError, match="--test-mode requires full wheel builds"):
bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True, sdist_only=True)
class TestFinalizeExitCodes:
"""Test finalize() returns correct exit codes."""
def test_finalize_no_failures_returns_zero(
self, tmp_context: context.WorkContext
) -> None:
"""Test finalize returns 0 when no failures in test mode."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
assert bt.finalize() == 0
def test_finalize_with_failures_returns_one(
self, tmp_context: context.WorkContext
) -> None:
"""Test finalize returns 1 when there are failures in test mode."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
bt.failed_packages.append(
{
"package": "failing-pkg",
"version": "1.0.0",
"exception_type": "RuntimeError",
"exception_message": "Build failed",
"failure_type": "bootstrap",
}
)
assert bt.finalize() == 1
def test_finalize_not_in_test_mode_returns_zero(
self, tmp_context: context.WorkContext
) -> None:
"""Test finalize returns 0 when not in test mode (regardless of failures)."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=False)
# Even if we manually add failures (shouldn't happen), it returns 0
bt.failed_packages.append(
{
"package": "some-pkg",
"version": "1.0.0",
"exception_type": "RuntimeError",
"exception_message": "Error",
"failure_type": "bootstrap",
}
)
assert bt.finalize() == 0
def test_finalize_logs_failed_packages(
self, tmp_context: context.WorkContext, caplog: pytest.LogCaptureFixture
) -> None:
"""Test finalize logs the list of failed packages."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
bt.failed_packages.extend(
[
{
"package": "pkg-a",
"version": "1.0",
"exception_type": "E",
"exception_message": "m",
"failure_type": "bootstrap",
},
{
"package": "pkg-b",
"version": "2.0",
"exception_type": "E",
"exception_message": "m",
"failure_type": "hook",
},
{
"package": "pkg-c",
"version": "3.0",
"exception_type": "E",
"exception_message": "m",
"failure_type": "dependency_extraction",
},
]
)
exit_code = bt.finalize()
assert exit_code == 1
assert "3 package(s) failed" in caplog.text
assert "pkg-a" in caplog.text
assert "pkg-b" in caplog.text
assert "pkg-c" in caplog.text
def _find_failure_report(work_dir: pathlib.Path) -> pathlib.Path | None:
"""Find the test-mode-failures-*.json file in work_dir."""
reports = list(work_dir.glob("test-mode-failures-*.json"))
return reports[0] if reports else None
class TestJsonFailureReport:
"""Test JSON failure report generation."""
def test_finalize_writes_json_report(
self, tmp_context: context.WorkContext
) -> None:
"""Test finalize writes test-mode-failures-<timestamp>.json with failure details."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
bt.failed_packages.append(
{
"package": "failing-pkg",
"version": "1.0.0",
"exception_type": "CalledProcessError",
"exception_message": "Compilation failed",
"failure_type": "bootstrap",
}
)
bt.finalize()
report_path = _find_failure_report(tmp_context.work_dir)
assert report_path is not None
assert report_path.name.startswith("test-mode-failures-")
assert report_path.name.endswith(".json")
with open(report_path) as f:
report = json.load(f)
assert "failures" in report
assert len(report["failures"]) == 1
assert report["failures"][0]["package"] == "failing-pkg"
assert report["failures"][0]["version"] == "1.0.0"
assert report["failures"][0]["exception_type"] == "CalledProcessError"
assert report["failures"][0]["exception_message"] == "Compilation failed"
assert report["failures"][0]["failure_type"] == "bootstrap"
def test_finalize_no_report_when_no_failures(
self, tmp_context: context.WorkContext
) -> None:
"""Test finalize does not write report when there are no failures."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
bt.finalize()
report_path = _find_failure_report(tmp_context.work_dir)
assert report_path is None
def test_finalize_report_with_null_version(
self, tmp_context: context.WorkContext
) -> None:
"""Test finalize handles failures where version is None (resolution failure)."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
bt.failed_packages.append(
{
"package": "failed-to-resolve",
"version": None,
"exception_type": "ResolutionError",
"exception_message": "Could not resolve version",
"failure_type": "resolution",
}
)
bt.finalize()
report_path = _find_failure_report(tmp_context.work_dir)
assert report_path is not None
with open(report_path) as f:
report = json.load(f)
assert report["failures"][0]["version"] is None
assert report["failures"][0]["failure_type"] == "resolution"
def test_finalize_report_multiple_failure_types(
self, tmp_context: context.WorkContext
) -> None:
"""Test finalize correctly reports multiple failures with different types."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
bt.failed_packages.extend(
[
{
"package": "pkg-a",
"version": "1.0.0",
"exception_type": "BuildError",
"exception_message": "Failed to compile",
"failure_type": "bootstrap",
},
{
"package": "pkg-b",
"version": "2.0.0",
"exception_type": "HookError",
"exception_message": "Validation failed",
"failure_type": "hook",
},
{
"package": "pkg-c",
"version": "3.0.0",
"exception_type": "MetadataError",
"exception_message": "Could not read metadata",
"failure_type": "dependency_extraction",
},
]
)
bt.finalize()
report_path = _find_failure_report(tmp_context.work_dir)
assert report_path is not None
with open(report_path) as f:
report = json.load(f)
assert len(report["failures"]) == 3
failure_types = [f["failure_type"] for f in report["failures"]]
assert "bootstrap" in failure_types
assert "hook" in failure_types
assert "dependency_extraction" in failure_types
class TestBootstrapExceptionHandling:
"""Test bootstrap() catches and records exceptions in test mode."""
def test_resolution_failure_recorded_in_test_mode(
self, tmp_context: context.WorkContext
) -> None:
"""Test that resolve_versions failures are recorded in test mode."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=True)
req = Requirement("nonexistent-package>=1.0")
# Mock _resolver.resolve to raise an exception (background tasks call
# _resolver.resolve directly, not resolve_versions)
with mock.patch.object(
bt._resolver,
"resolve",
side_effect=RuntimeError("Version resolution failed"),
):
# Should not raise in test mode
bt.bootstrap([req])
# Verify failure was recorded
assert len(bt.failed_packages) == 1
failure = bt.failed_packages[0]
assert failure["package"] == "nonexistent-package"
assert (
failure["version"] is None
) # No version available for resolution failures
assert failure["failure_type"] == "resolution"
assert "Version resolution failed" in failure["exception_message"]
def test_resolution_failure_raises_in_normal_mode(
self, tmp_context: context.WorkContext
) -> None:
"""Test that resolve_versions failures raise in normal mode."""
bt = bootstrapper.Bootstrapper(ctx=tmp_context, test_mode=False)
req = Requirement("nonexistent-package>=1.0")
# Mock _resolver.resolve to raise an exception (background tasks call
# _resolver.resolve directly, not resolve_versions)
with mock.patch.object(
bt._resolver,
"resolve",
side_effect=RuntimeError("Version resolution failed"),
):
with pytest.raises(RuntimeError, match="Version resolution failed"):
bt.bootstrap([req])