test-base 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. test_base/__init__.py +20 -0
  2. test_base/adapters/__init__.py +354 -0
  3. test_base/adapters/base.py +263 -0
  4. test_base/adapters/canonical_json.py +1088 -0
  5. test_base/adapters/junit_xml.py +1272 -0
  6. test_base/api/__init__.py +0 -0
  7. test_base/api/app.py +314 -0
  8. test_base/api/deps.py +178 -0
  9. test_base/api/routes/__init__.py +0 -0
  10. test_base/api/routes/failures.py +104 -0
  11. test_base/api/routes/health.py +35 -0
  12. test_base/api/routes/imports.py +143 -0
  13. test_base/api/routes/queries.py +295 -0
  14. test_base/api/routes/retention.py +130 -0
  15. test_base/api/routes/runs.py +147 -0
  16. test_base/api/routes/server.py +65 -0
  17. test_base/api/routes/stream.py +117 -0
  18. test_base/api/schemas.py +390 -0
  19. test_base/api/security.py +183 -0
  20. test_base/cli/__init__.py +125 -0
  21. test_base/cli/commands/__init__.py +0 -0
  22. test_base/cli/commands/db.py +76 -0
  23. test_base/cli/commands/demo.py +103 -0
  24. test_base/cli/commands/formats.py +41 -0
  25. test_base/cli/commands/ingest.py +119 -0
  26. test_base/cli/commands/init.py +738 -0
  27. test_base/cli/commands/prune.py +146 -0
  28. test_base/cli/commands/publish.py +133 -0
  29. test_base/cli/commands/serve.py +118 -0
  30. test_base/cli/commands/token.py +97 -0
  31. test_base/cli/options.py +189 -0
  32. test_base/cli/render.py +223 -0
  33. test_base/config.py +519 -0
  34. test_base/demo/__init__.py +0 -0
  35. test_base/demo/evidence.py +447 -0
  36. test_base/demo/seed.py +628 -0
  37. test_base/domain/__init__.py +61 -0
  38. test_base/domain/events.py +422 -0
  39. test_base/domain/failures.py +327 -0
  40. test_base/domain/identity.py +205 -0
  41. test_base/domain/metrics.py +176 -0
  42. test_base/domain/models.py +947 -0
  43. test_base/domain/status.py +246 -0
  44. test_base/errors.py +136 -0
  45. test_base/logging.py +174 -0
  46. test_base/py.typed +0 -0
  47. test_base/sdk/__init__.py +28 -0
  48. test_base/sdk/client.py +607 -0
  49. test_base/service/__init__.py +0 -0
  50. test_base/service/bus.py +331 -0
  51. test_base/service/coalesce.py +133 -0
  52. test_base/service/imports.py +552 -0
  53. test_base/service/retention.py +640 -0
  54. test_base/service/runs.py +394 -0
  55. test_base/service/runtime.py +123 -0
  56. test_base/storage/__init__.py +41 -0
  57. test_base/storage/artifacts.py +347 -0
  58. test_base/storage/base.py +350 -0
  59. test_base/storage/db.py +595 -0
  60. test_base/storage/failures.py +321 -0
  61. test_base/storage/locks.py +104 -0
  62. test_base/storage/migrations/env.py +108 -0
  63. test_base/storage/migrations/script.py.mako +26 -0
  64. test_base/storage/migrations/versions/0001_initial.py +289 -0
  65. test_base/storage/migrations/versions/0002_retention_indexes.py +38 -0
  66. test_base/storage/migrations/versions/0003_postgresql_bigint.py +74 -0
  67. test_base/storage/migrations/versions/0004_run_shards.py +37 -0
  68. test_base/storage/migrations/versions/0005_failure_groups.py +206 -0
  69. test_base/storage/models.py +560 -0
  70. test_base/storage/repositories.py +2481 -0
  71. test_base/storage/uow.py +138 -0
  72. test_base/version.py +17 -0
  73. test_base/web/assets/css/app.css +3567 -0
  74. test_base/web/assets/favicon.svg +5 -0
  75. test_base/web/assets/js/00-util.js +829 -0
  76. test_base/web/assets/js/05-windows.js +551 -0
  77. test_base/web/assets/js/10-provider.js +396 -0
  78. test_base/web/assets/js/12-live-provider.js +804 -0
  79. test_base/web/assets/js/20-charts.js +715 -0
  80. test_base/web/assets/js/30-store.js +793 -0
  81. test_base/web/assets/js/40-components.js +2141 -0
  82. test_base/web/assets/js/42-timeline.js +629 -0
  83. test_base/web/assets/js/50-view-overview.js +747 -0
  84. test_base/web/assets/js/52-view-analytics.js +842 -0
  85. test_base/web/assets/js/53-view-suites.js +508 -0
  86. test_base/web/assets/js/54-view-metrics.js +438 -0
  87. test_base/web/assets/js/55-view-runs.js +244 -0
  88. test_base/web/assets/js/56-view-evidence.js +703 -0
  89. test_base/web/assets/js/57-view-failures.js +596 -0
  90. test_base/web/assets/js/60-view-run.js +788 -0
  91. test_base/web/assets/js/65-view-test.js +402 -0
  92. test_base/web/assets/js/70-view-settings.js +524 -0
  93. test_base/web/assets/js/75-view-help.js +554 -0
  94. test_base/web/assets/js/80-settings.js +99 -0
  95. test_base/web/assets/js/85-activity.js +727 -0
  96. test_base/web/assets/js/90-app.js +595 -0
  97. test_base/web/assets/manifest.json +70 -0
  98. test_base/web/index.html +210 -0
  99. test_base-0.1.1.dist-info/METADATA +285 -0
  100. test_base-0.1.1.dist-info/RECORD +103 -0
  101. test_base-0.1.1.dist-info/WHEEL +4 -0
  102. test_base-0.1.1.dist-info/entry_points.txt +6 -0
  103. test_base-0.1.1.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,1272 @@
1
+ """JUnit / xUnit XML — the format almost every test runner can already write.
2
+
3
+ "JUnit XML" is not a specification. It is a shape that Ant emitted in 2002 and
4
+ that thirty frameworks have been extending ever since, so this adapter is
5
+ deliberately forgiving about structure and deliberately strict about two things:
6
+ safety and truthfulness about what it could not represent.
7
+
8
+ Safety
9
+ ------
10
+
11
+ Parsing is done exclusively through :mod:`defusedxml`, with DTDs, entity
12
+ declarations and external references all refused. A JUnit file is an *untrusted
13
+ upload*: the two classic attacks — a billion-laughs entity bomb that turns
14
+ 200 bytes into 3 GB of RAM, and an XXE that makes the server read
15
+ ``/etc/passwd`` and put it in a stack trace on a dashboard — are both entity
16
+ tricks, and both are refused at the parser rather than mitigated afterwards.
17
+ ``tests/fixtures/junit/billion-laughs.xml`` and ``xxe.xml`` exist to keep that
18
+ true.
19
+
20
+ Memory
21
+ ------
22
+
23
+ ``iterparse`` with ``elem.clear()``, and each finished ``<testcase>`` is removed
24
+ from its parent as soon as it has been turned into events. A 200 MB Surefire
25
+ aggregate is streamed, never held: the resident set is one suite's worth of
26
+ elements plus the event currently being yielded.
27
+
28
+ What it handles
29
+ ---------------
30
+
31
+ * ``<testsuites>`` roots and bare ``<testsuite>`` roots.
32
+ * ``<testsuite>`` nested inside ``<testsuite>`` — the child's ``parent_id``
33
+ chains to the parent and its path is ``parent/child``.
34
+ * ``<testcase name classname time file line assertions status>``.
35
+ * ``<failure message type>``, ``<error>``, ``<skipped message>``, with the text
36
+ body or a ``<stackTrace>`` child as the trace.
37
+ * ``<system-out>`` / ``<system-err>`` at case level (onto the attempt) and at
38
+ suite level (see "Known compromises").
39
+ * ``<properties><property name value/></properties>`` at suite level, kept as
40
+ suite metadata, with the well-known keys — branch, commit, environment, ci,
41
+ url and friends — lifted onto the run's :class:`RunContext`.
42
+ * ``time`` in seconds as a float, with a comma decimal separator, with thousands
43
+ separators, or missing entirely.
44
+ * An empty ``<testcase/>`` — that means passed.
45
+
46
+ Retries, and why they matter most
47
+ ---------------------------------
48
+
49
+ Maven Surefire records a retried test as *one* ``<testcase>`` with the earlier
50
+ attempts nested inside it::
51
+
52
+ <testcase name="chargesCard" classname="CheckoutIT" time="4.1">
53
+ <flakyFailure message="timeout" type="AssertionError">stack...</flakyFailure>
54
+ <flakyFailure message="timeout" type="AssertionError">stack...</flakyFailure>
55
+ </testcase>
56
+
57
+ ``flakyFailure``/``flakyError`` mean "this failed, then a rerun passed"; the
58
+ ``<testcase>``'s own (absent) outcome is the pass. ``rerunFailure``/
59
+ ``rerunError`` mean "this failed, the rerun failed too"; the ``<testcase>``'s own
60
+ ``<failure>`` is the final word. Either way the nested elements are the
61
+ **earlier** attempts and the ``<testcase>``'s own outcome is the **last** one,
62
+ so the example above becomes three attempts — failed, failed, passed — and the
63
+ test is flaky. Collapsing that into a single "passed" is how a flaky suite stays
64
+ invisible for a year, which is why this is the one behaviour in this module
65
+ worth reading twice.
66
+
67
+ Counting
68
+ --------
69
+
70
+ The ``tests``/``failures``/``errors``/``skipped`` attributes a publisher writes
71
+ are advisory and frequently wrong — a partially-uploaded shard, a framework that
72
+ counts retries, a hand-edited file. Counts are folded from the cases that were
73
+ actually parsed, and a ``counts_mismatch`` warning records the disagreement
74
+ rather than hiding it. Likewise, any status word that
75
+ :func:`~test_base.domain.status.normalize_status` cannot map becomes ``unknown``
76
+ and raises an ``unknown_status`` warning naming the word and where it was found.
77
+
78
+ Known compromises
79
+ -----------------
80
+
81
+ * JUnit has no per-test wall-clock times, only durations. Attempts therefore
82
+ carry ``duration_ms`` and no ``started_at``/``finished_at``; the run's own
83
+ window is taken from the earliest ``timestamp`` attribute plus the reported
84
+ total, and a ``synthesized_timestamps`` info warning says so when there was no
85
+ ``timestamp`` at all.
86
+ * Suite-level ``<system-out>``/``<system-err>`` arrive *after* ``suite.started``
87
+ has been emitted, and ``SuiteFinishedData`` has nowhere to carry text, so that
88
+ output is attached to ``run.finished``'s metadata with a
89
+ ``suite_output_lifted`` info warning.
90
+ """
91
+
92
+ from __future__ import annotations
93
+
94
+ import contextlib
95
+ import re
96
+ import uuid
97
+ from collections.abc import Iterator
98
+ from datetime import UTC, datetime, timedelta
99
+ from typing import IO, Any
100
+
101
+ from defusedxml.ElementTree import iterparse as defused_iterparse
102
+ from pydantic import ValidationError as PydanticValidationError
103
+
104
+ from test_base.adapters.base import SNIFF_BYTES, BaseAdapter, ParseContext, WarningCollector
105
+ from test_base.domain.events import (
106
+ DomainEvent,
107
+ EventType,
108
+ RunFinishedData,
109
+ RunQueuedData,
110
+ RunStartedData,
111
+ SuiteFinishedData,
112
+ SuiteStartedData,
113
+ TestFinishedData,
114
+ make_event,
115
+ )
116
+ from test_base.domain.identity import TEST_BASE_NAMESPACE, test_identity
117
+ from test_base.domain.identity import suite_id as suite_identity
118
+ from test_base.domain.models import FailureDetail, RunContext, utcnow
119
+ from test_base.domain.status import TestStatus, normalize_status, worst_status
120
+ from test_base.errors import AdapterError
121
+ from test_base.logging import get_logger
122
+
123
+ __all__ = ["JUnitXmlAdapter"]
124
+
125
+ logger = get_logger(__name__)
126
+
127
+ #: ``LongText`` / ``ShortText`` in :mod:`test_base.domain.models`.
128
+ _LONG_TEXT_LIMIT = 64_000
129
+ _SHORT_TEXT_LIMIT = 512
130
+ #: Suite-level console output is metadata, not a payload; keep it small.
131
+ _SUITE_OUTPUT_LIMIT = 8192
132
+
133
+ _ROOT_TAGS = frozenset({"testsuites", "testsuite"})
134
+
135
+ #: Nested elements that describe an *earlier* attempt of the same test.
136
+ _RETRY_TAGS: dict[str, TestStatus] = {
137
+ "flakyfailure": TestStatus.FAILED,
138
+ "flakyerror": TestStatus.ERROR,
139
+ "rerunfailure": TestStatus.FAILED,
140
+ "rerunerror": TestStatus.ERROR,
141
+ }
142
+
143
+ #: Outcome children of a ``<testcase>``, in the order the XML allows them.
144
+ _OUTCOME_TAGS: dict[str, TestStatus] = {
145
+ "failure": TestStatus.FAILED,
146
+ "error": TestStatus.ERROR,
147
+ "skipped": TestStatus.SKIPPED,
148
+ }
149
+
150
+ #: Ant and its descendants write ``status="run"`` on *every* executed test — it
151
+ #: describes execution, not outcome. Treating it as a status word would raise an
152
+ #: ``unknown_status`` warning on every single passing test in the file, so these
153
+ #: words mean "look at the children instead" and nothing else.
154
+ _EXECUTION_MARKERS = frozenset({"run", "completed", "complete", "executed", "finished"})
155
+
156
+ #: property name (lowercased, ``-`` folded to ``_``) -> ``RunContext`` field.
157
+ _RUN_PROPERTIES: dict[str, str] = {
158
+ "branch": "branch",
159
+ "git.branch": "branch",
160
+ "git_branch": "branch",
161
+ "build_branch": "branch",
162
+ "ci_commit_branch": "branch",
163
+ "commit": "commit_sha",
164
+ "commit_sha": "commit_sha",
165
+ "git.commit": "commit_sha",
166
+ "git_commit": "commit_sha",
167
+ "revision": "commit_sha",
168
+ "sha": "commit_sha",
169
+ "commit_message": "commit_message",
170
+ "environment": "environment",
171
+ "env": "environment",
172
+ "test_environment": "environment",
173
+ "ci": "ci_provider",
174
+ "ci_provider": "ci_provider",
175
+ "ci_platform": "ci_provider",
176
+ "url": "pipeline_url",
177
+ "build_url": "pipeline_url",
178
+ "job_url": "pipeline_url",
179
+ "pipeline_url": "pipeline_url",
180
+ "pipeline_id": "pipeline_id",
181
+ "build_id": "pipeline_id",
182
+ "build_number": "pipeline_id",
183
+ "hostname": "hostname",
184
+ "host": "hostname",
185
+ "os": "os",
186
+ "platform": "os",
187
+ "framework": "framework",
188
+ "framework_version": "framework_version",
189
+ "language": "language",
190
+ "language_version": "language_version",
191
+ "executor": "executor",
192
+ }
193
+
194
+ _SLUG_RE = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9._\- ]*$")
195
+ _XML_NAME_RE = re.compile(rb"([A-Za-z_][A-Za-z0-9_.:-]*)")
196
+
197
+
198
+ # --------------------------------------------------------------- utilities
199
+
200
+
201
+ def _localname(tag: object) -> str:
202
+ """The tag without its namespace, lowercased. ``{ns}testSuite`` -> ``testsuite``."""
203
+ if not isinstance(tag, str):
204
+ return ""
205
+ return tag.rsplit("}", 1)[-1].lower()
206
+
207
+
208
+ def _clip(value: object, limit: int) -> tuple[str | None, bool]:
209
+ """Coerce to text and cut to ``limit`` characters, reporting whether it cut."""
210
+ if value is None:
211
+ return None, False
212
+ text = value if isinstance(value, str) else str(value)
213
+ if len(text) <= limit:
214
+ return text or None, False
215
+ return text[:limit], True
216
+
217
+
218
+ def _event_id(source: str, run_id: uuid.UUID, sequence: int, event_type: EventType) -> uuid.UUID:
219
+ """Deterministic event id, so re-importing the same file applies nothing new."""
220
+ return uuid.uuid5(TEST_BASE_NAMESPACE, f"{source}\x1f{run_id}\x1f{sequence}\x1f{event_type}")
221
+
222
+
223
+ def _root_element_name(head: bytes) -> str | None:
224
+ """The name of the first real element in ``head``, skipping prologue noise.
225
+
226
+ Hand-rolled rather than regex-driven because a DOCTYPE with an internal
227
+ subset (``<!DOCTYPE x [ ... ]>``) contains ``>`` characters, and every
228
+ "strip the doctype" regex gets that wrong — which matters here, since a
229
+ DOCTYPE is exactly what an entity bomb arrives wrapped in.
230
+ """
231
+ index = 0
232
+ length = len(head)
233
+ while index < length:
234
+ start = head.find(b"<", index)
235
+ if start < 0:
236
+ return None
237
+ if head.startswith(b"<?", start):
238
+ end = head.find(b"?>", start)
239
+ if end < 0:
240
+ return None
241
+ index = end + 2
242
+ elif head.startswith(b"<!--", start):
243
+ end = head.find(b"-->", start)
244
+ if end < 0:
245
+ return None
246
+ index = end + 3
247
+ elif head.startswith(b"<!", start):
248
+ cursor = start + 2
249
+ depth = 0
250
+ while cursor < length:
251
+ char = head[cursor : cursor + 1]
252
+ if char == b"[":
253
+ depth += 1
254
+ elif char == b"]":
255
+ depth -= 1
256
+ elif char == b">" and depth <= 0:
257
+ break
258
+ cursor += 1
259
+ if cursor >= length:
260
+ return None
261
+ index = cursor + 1
262
+ else:
263
+ match = _XML_NAME_RE.match(head, start + 1)
264
+ if match is None:
265
+ return None
266
+ return match.group(1).decode("ascii", "replace")
267
+ return None
268
+
269
+
270
+ # ------------------------------------------------------------------ adapter
271
+
272
+
273
+ class JUnitXmlAdapter(BaseAdapter):
274
+ """Streams JUnit/xUnit XML into Test Base events."""
275
+
276
+ name = "junit-xml"
277
+ display_name = "JUnit XML"
278
+ extensions = (".xml", ".junit", ".junit.xml")
279
+ content_types = ("application/xml", "text/xml", "application/x-junit+xml")
280
+ priority = 50
281
+ description = "JUnit/xUnit XML from pytest, Surefire, Gradle, Jest, Playwright and friends."
282
+
283
+ def detect(self, stream: IO[bytes], filename: str | None, content_type: str | None) -> float:
284
+ try:
285
+ head = self.sniff(stream, SNIFF_BYTES)
286
+ except Exception: # pragma: no cover - unreadable stream
287
+ return self.hint_score(filename, content_type)
288
+ root = _root_element_name(head)
289
+ if root is None:
290
+ # Not recognisably XML in the first 8 KiB: fall back to the name.
291
+ return self.hint_score(filename, content_type)
292
+ if root.rsplit(":", 1)[-1].lower() in _ROOT_TAGS:
293
+ return 0.9
294
+ return 0.0
295
+
296
+ def parse(self, stream: IO[bytes], context: ParseContext) -> Iterator[DomainEvent]:
297
+ return _JUnitParser(context).run(stream)
298
+
299
+
300
+ # ------------------------------------------------------------------- frames
301
+
302
+
303
+ class _SuiteFrame:
304
+ """One open ``<testsuite>`` element and everything folded into it so far."""
305
+
306
+ __slots__ = (
307
+ "declared",
308
+ "descendant_counts",
309
+ "element",
310
+ "identity",
311
+ "metadata",
312
+ "name",
313
+ "own_counts",
314
+ "parent_id",
315
+ "path",
316
+ "seconds",
317
+ "started_at",
318
+ )
319
+
320
+ def __init__(
321
+ self,
322
+ *,
323
+ element: Any,
324
+ name: str,
325
+ path: str,
326
+ identity: uuid.UUID,
327
+ parent_id: uuid.UUID | None,
328
+ started_at: datetime | None,
329
+ seconds: float | None,
330
+ metadata: dict[str, Any],
331
+ declared: dict[str, int],
332
+ ) -> None:
333
+ self.element = element
334
+ self.name = name
335
+ self.path = path
336
+ self.identity = identity
337
+ self.parent_id = parent_id
338
+ self.started_at = started_at
339
+ self.seconds = seconds
340
+ self.metadata = metadata
341
+ self.declared = declared
342
+ self.own_counts: dict[str, int] = {}
343
+ self.descendant_counts: dict[str, int] = {}
344
+
345
+ def record(self, status: TestStatus) -> None:
346
+ self.own_counts["tests"] = self.own_counts.get("tests", 0) + 1
347
+ bucket = {
348
+ TestStatus.FAILED: "failures",
349
+ TestStatus.XPASSED: "failures",
350
+ TestStatus.ERROR: "errors",
351
+ TestStatus.SKIPPED: "skipped",
352
+ TestStatus.XFAILED: "skipped",
353
+ }.get(status)
354
+ if bucket:
355
+ self.own_counts[bucket] = self.own_counts.get(bucket, 0) + 1
356
+
357
+ def rolled_up(self) -> dict[str, int]:
358
+ merged = dict(self.own_counts)
359
+ for key, value in self.descendant_counts.items():
360
+ merged[key] = merged.get(key, 0) + value
361
+ return merged
362
+
363
+
364
+ class _Attempt:
365
+ """One execution of one test, before it becomes a ``test.finished`` event."""
366
+
367
+ __slots__ = (
368
+ "failure",
369
+ "original_status",
370
+ "seconds",
371
+ "skip_reason",
372
+ "status",
373
+ "stderr",
374
+ "stdout",
375
+ )
376
+
377
+ def __init__(
378
+ self,
379
+ *,
380
+ status: TestStatus,
381
+ original_status: str | None = None,
382
+ seconds: float | None = None,
383
+ failure: FailureDetail | None = None,
384
+ stdout: str | None = None,
385
+ stderr: str | None = None,
386
+ skip_reason: str | None = None,
387
+ ) -> None:
388
+ self.status = status
389
+ self.original_status = original_status
390
+ self.seconds = seconds
391
+ self.failure = failure
392
+ self.stdout = stdout
393
+ self.stderr = stderr
394
+ self.skip_reason = skip_reason
395
+
396
+
397
+ # ------------------------------------------------------------------ parser
398
+
399
+
400
+ class _JUnitParser:
401
+ """The streaming state machine. One instance per file, never reused."""
402
+
403
+ def __init__(self, context: ParseContext) -> None:
404
+ self.context = context
405
+ self.warnings: WarningCollector = context.warnings
406
+ self.project = context.project_key
407
+ self.source = context.source
408
+ self.run_id = context.run_id
409
+ self.text_limit = min(context.max_text_bytes, _LONG_TEXT_LIMIT)
410
+
411
+ self.stack: list[_SuiteFrame] = []
412
+ self.root_element: Any = None
413
+ self.root_attrib: dict[str, str] = {}
414
+ self.in_testcase = 0
415
+ self.header_flushed = False
416
+ self.pending_suites: list[_SuiteFrame] = []
417
+ self.used_paths: set[str] = set()
418
+ self.implicit_suite: _SuiteFrame | None = None
419
+
420
+ self.parse_started = utcnow()
421
+ self.run_started_at: datetime | None = None
422
+ self.total_seconds = 0.0
423
+ self.case_seconds = 0.0
424
+ self.suite_seconds = 0.0
425
+ self.clock = self.parse_started
426
+ self.context_fields: dict[str, Any] = {}
427
+ self.run_metadata: dict[str, Any] = dict(context.metadata)
428
+ self.saw_timestamp = False
429
+ self._warned: set[str] = set()
430
+
431
+ # ------------------------------------------------------------ warnings
432
+
433
+ def warn_once(self, code: str, message: str, **detail: Any) -> None:
434
+ if code in self._warned:
435
+ return
436
+ self._warned.add(code)
437
+ self.warnings.add(code, message, **detail)
438
+
439
+ def info_once(self, code: str, message: str, **detail: Any) -> None:
440
+ if code in self._warned:
441
+ return
442
+ self._warned.add(code)
443
+ self.warnings.info(code, message, **detail)
444
+
445
+ # -------------------------------------------------------------- values
446
+
447
+ def seconds(self, raw: object, location: str) -> float | None:
448
+ """Parse a ``time`` attribute, in seconds.
449
+
450
+ Accepts ``1.5``, ``1,5`` (decimal comma — de_DE locales write it that
451
+ way), ``1,234.5`` (thousands separator) and whitespace-padded junk.
452
+ """
453
+ if raw is None:
454
+ return None
455
+ text = str(raw).strip().replace(" ", "").replace("\u00a0", "")
456
+ if not text:
457
+ return None
458
+ if "," in text and "." in text:
459
+ text = text.replace(",", "")
460
+ elif "," in text:
461
+ text = text.replace(",", ".")
462
+ try:
463
+ value = float(text)
464
+ except ValueError:
465
+ self.warnings.add(
466
+ "bad_duration",
467
+ f"could not read time={raw!r} as a number of seconds; the duration is unknown",
468
+ location=location,
469
+ )
470
+ return None
471
+ if value != value or value in (float("inf"), float("-inf")):
472
+ self.warnings.add(
473
+ "bad_duration", f"time={raw!r} is not a finite duration", location=location
474
+ )
475
+ return None
476
+ if value < 0:
477
+ self.warnings.add(
478
+ "bad_duration", f"negative time={raw!r} clamped to 0", location=location
479
+ )
480
+ return 0.0
481
+ return value
482
+
483
+ def timestamp(self, raw: object, location: str) -> datetime | None:
484
+ if raw is None:
485
+ return None
486
+ text = str(raw).strip()
487
+ if not text:
488
+ return None
489
+ try:
490
+ parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
491
+ except ValueError:
492
+ self.warnings.add(
493
+ "bad_timestamp",
494
+ f"could not read timestamp={text!r} as ISO-8601; ignoring it",
495
+ location=location,
496
+ )
497
+ return None
498
+ if parsed.tzinfo is None:
499
+ self.info_once(
500
+ "naive_timestamp",
501
+ "timestamps in this report carry no UTC offset; they are read as UTC",
502
+ location=location,
503
+ )
504
+ parsed = parsed.replace(tzinfo=UTC)
505
+ self.saw_timestamp = True
506
+ return parsed
507
+
508
+ def declared_counts(self, attrib: dict[str, str], location: str) -> dict[str, int]:
509
+ counts: dict[str, int] = {}
510
+ for key in ("tests", "failures", "errors", "skipped", "disabled"):
511
+ raw = attrib.get(key)
512
+ if raw is None:
513
+ continue
514
+ try:
515
+ counts["skipped" if key == "disabled" else key] = int(str(raw).strip())
516
+ except ValueError:
517
+ self.warnings.add(
518
+ "bad_count",
519
+ f"{key}={raw!r} is not an integer; ignoring the publisher's own total",
520
+ location=location,
521
+ )
522
+ return counts
523
+
524
+ # ---------------------------------------------------------------- emit
525
+
526
+ def emit(
527
+ self,
528
+ event_type: EventType,
529
+ data: Any,
530
+ *,
531
+ subject: str | None = None,
532
+ time: datetime | None = None,
533
+ ) -> DomainEvent:
534
+ sequence = self.context.next_sequence()
535
+ return make_event(
536
+ event_type,
537
+ run_id=self.run_id,
538
+ workspace_id=self.context.workspace_id,
539
+ sequence=sequence,
540
+ data=data,
541
+ source=self.source,
542
+ event_id=_event_id(self.source, self.run_id, sequence, event_type),
543
+ time=time or self.clock,
544
+ subject=subject,
545
+ )
546
+
547
+ # ----------------------------------------------------------------- run
548
+
549
+ def run(self, stream: IO[bytes]) -> Iterator[DomainEvent]:
550
+ """Stream the file, yielding events as the elements close."""
551
+ try:
552
+ iterator = defused_iterparse(
553
+ stream,
554
+ events=("start", "end"),
555
+ forbid_dtd=True,
556
+ forbid_entities=True,
557
+ forbid_external=True,
558
+ )
559
+ except Exception as exc: # pragma: no cover - constructor rarely fails
560
+ raise AdapterError(f"could not start the XML parser: {exc}") from exc
561
+
562
+ try:
563
+ yield from self._walk(iterator)
564
+ except AdapterError:
565
+ raise
566
+ except Exception as exc:
567
+ raise self._translate(exc) from exc
568
+
569
+ if not self.header_flushed:
570
+ yield from self._flush_header()
571
+ yield from self._close_open_suites()
572
+ yield self._run_finished()
573
+
574
+ def _translate(self, exc: Exception) -> AdapterError:
575
+ """Turn a parser failure into an error a user can act on."""
576
+ name = type(exc).__name__
577
+ if name in ("DTDForbidden", "EntitiesForbidden", "ExternalReferenceForbidden"):
578
+ logger.warning("refused a JUnit upload containing %s", name)
579
+ return AdapterError(
580
+ "this XML declares a DTD or entities and was refused before it was expanded. "
581
+ "Entity expansion is how an 'XML bomb' turns a 200-byte upload into gigabytes "
582
+ "of memory, and how an XXE reads files off the server, so Test Base never "
583
+ "processes it. Re-export the report without a DOCTYPE.",
584
+ detail={"reason": name},
585
+ )
586
+ return AdapterError(
587
+ f"this file is not well-formed XML: {exc}",
588
+ detail={"reason": name},
589
+ )
590
+
591
+ def _walk(self, iterator: Any) -> Iterator[DomainEvent]:
592
+ for xml_event, element in iterator:
593
+ tag = _localname(element.tag)
594
+ if xml_event == "start":
595
+ if tag == "testsuites" and self.root_element is None:
596
+ self.root_element = element
597
+ self.root_attrib = dict(element.attrib)
598
+ elif tag == "testsuite":
599
+ if self.root_element is None:
600
+ self.root_element = element # bare <testsuite> root
601
+ yield from self._open_suite(element)
602
+ elif tag == "testcase":
603
+ self.in_testcase += 1
604
+ if not self.header_flushed:
605
+ yield from self._flush_header()
606
+ continue
607
+
608
+ # --- end events
609
+ if tag == "testcase":
610
+ self.in_testcase = max(0, self.in_testcase - 1)
611
+ yield from self._finish_case(element)
612
+ self._release(element)
613
+ elif tag == "testsuite":
614
+ yield from self._close_suite(element)
615
+ elif self.in_testcase == 0 and tag == "properties":
616
+ self._absorb_properties(element)
617
+ self._release(element)
618
+ elif self.in_testcase == 0 and tag in ("system-out", "system-err"):
619
+ self._absorb_suite_output(tag, element)
620
+ self._release(element)
621
+
622
+ # -------------------------------------------------------------- header
623
+
624
+ def _flush_header(self) -> Iterator[DomainEvent]:
625
+ """Emit ``run.queued``/``run.started`` plus every buffered ``suite.started``.
626
+
627
+ Deferred to the first ``<testcase>`` so that the first suite's
628
+ ``<properties>`` block — where branch, commit and environment live — has
629
+ already been read and can be lifted onto the run context.
630
+ """
631
+ self.header_flushed = True
632
+ if self.run_started_at is None:
633
+ self.run_started_at = self.timestamp(
634
+ self.root_attrib.get("timestamp"), "testsuites"
635
+ ) or (self.pending_suites[0].started_at if self.pending_suites else None)
636
+ if self.run_started_at is None:
637
+ self.run_started_at = self.parse_started
638
+ self.info_once(
639
+ "synthesized_timestamps",
640
+ "the report carries no timestamp attribute, so the run's clock is the time "
641
+ "it was imported; per-test durations are exact, the wall clock is not",
642
+ )
643
+ self.clock = self.run_started_at
644
+
645
+ run_context = self._build_run_context()
646
+ yield self.emit(
647
+ EventType.RUN_QUEUED,
648
+ RunQueuedData(
649
+ project_key=self.project,
650
+ name=self.context.name or self._run_name(),
651
+ team=self.context.team,
652
+ component=self.context.component,
653
+ test_type=self.context.test_type,
654
+ trigger=self.context.trigger,
655
+ context=run_context,
656
+ tags=list(self.context.tags),
657
+ metadata=self.run_metadata,
658
+ expected_tests=self._declared_total(),
659
+ ),
660
+ subject=self.project,
661
+ time=self.run_started_at,
662
+ )
663
+ yield self.emit(
664
+ EventType.RUN_STARTED,
665
+ RunStartedData(
666
+ started_at=self.run_started_at,
667
+ project_key=self.project,
668
+ context=run_context,
669
+ ),
670
+ subject=self.project,
671
+ time=self.run_started_at,
672
+ )
673
+ for frame in self.pending_suites:
674
+ yield self._suite_started(frame)
675
+ self.pending_suites.clear()
676
+
677
+ def _run_name(self) -> str | None:
678
+ name = self.root_attrib.get("name")
679
+ if not name and self.pending_suites:
680
+ name = self.pending_suites[0].name
681
+ clipped, _ = _clip(name, _SHORT_TEXT_LIMIT)
682
+ return clipped
683
+
684
+ def _declared_total(self) -> int | None:
685
+ raw = self.root_attrib.get("tests")
686
+ if raw is None:
687
+ return None
688
+ try:
689
+ total = int(str(raw).strip())
690
+ except ValueError:
691
+ return None
692
+ return total if total > 0 else None
693
+
694
+ def _build_run_context(self) -> RunContext:
695
+ """The importer's context wins; the file only fills in what it left blank."""
696
+ merged: dict[str, Any] = {}
697
+ request = self.context.context
698
+ for field in RunContext.model_fields:
699
+ chosen = getattr(request, field, None)
700
+ if chosen is None:
701
+ chosen = self.context_fields.get(field)
702
+ if chosen is not None:
703
+ merged[field] = chosen
704
+ try:
705
+ return RunContext(**merged)
706
+ except PydanticValidationError as exc:
707
+ bad = {str(error["loc"][0]) for error in exc.errors() if error.get("loc")}
708
+ for field in sorted(bad):
709
+ self.warnings.add(
710
+ "invalid_run_context",
711
+ f"run context field {field}={merged.get(field)!r} came from <properties> "
712
+ "and did not validate; it was dropped and the rest kept",
713
+ location="properties",
714
+ )
715
+ remaining = {key: value for key, value in merged.items() if key not in bad}
716
+ try:
717
+ return RunContext(**remaining)
718
+ except PydanticValidationError: # pragma: no cover - belt and braces
719
+ return RunContext()
720
+
721
+ # -------------------------------------------------------------- suites
722
+
723
+ def _open_suite(self, element: Any) -> Iterator[DomainEvent]:
724
+ attrib = {str(key): str(value) for key, value in element.attrib.items()}
725
+ parent = self.stack[-1] if self.stack else None
726
+ raw_name = attrib.get("name") or attrib.get("package") or "suite"
727
+ name, _ = _clip(raw_name, _SHORT_TEXT_LIMIT)
728
+ name = name or "suite"
729
+ path = f"{parent.path}/{name}" if parent else name
730
+ path = self._unique_path(path)
731
+ location = f"testsuite[{path}]"
732
+
733
+ metadata: dict[str, Any] = {}
734
+ for key in ("package", "hostname", "id", "file", "timestamp", "group"):
735
+ value = attrib.get(key)
736
+ if value:
737
+ metadata[key], _ = _clip(value, _SHORT_TEXT_LIMIT)
738
+ if attrib.get("hostname") and "hostname" not in self.context_fields:
739
+ self.context_fields["hostname"], _ = _clip(attrib["hostname"], _SHORT_TEXT_LIMIT)
740
+
741
+ frame = _SuiteFrame(
742
+ element=element,
743
+ name=name,
744
+ path=path,
745
+ identity=suite_identity(project=self.project, suite_path=path),
746
+ parent_id=parent.identity if parent else None,
747
+ started_at=self.timestamp(attrib.get("timestamp"), location),
748
+ seconds=self.seconds(attrib.get("time"), location),
749
+ metadata=metadata,
750
+ declared=self.declared_counts(attrib, location),
751
+ )
752
+ self.stack.append(frame)
753
+ if self.run_started_at is None and frame.started_at is not None:
754
+ self.run_started_at = frame.started_at
755
+ if self.header_flushed:
756
+ yield self._suite_started(frame)
757
+ else:
758
+ self.pending_suites.append(frame)
759
+
760
+ def _unique_path(self, path: str) -> str:
761
+ """Two suites with the same name are two suites, not one.
762
+
763
+ Their identities are derived from the path, so an unqualified duplicate
764
+ would silently merge two files' results into one suite.
765
+ """
766
+ if path not in self.used_paths:
767
+ self.used_paths.add(path)
768
+ return path
769
+ for suffix in range(2, 1000):
770
+ candidate = f"{path}#{suffix}"
771
+ if candidate not in self.used_paths:
772
+ self.used_paths.add(candidate)
773
+ self.warn_once(
774
+ "duplicate_suite",
775
+ f"more than one suite is called {path!r}; the later ones are "
776
+ "distinguished with a #n suffix so their results do not merge",
777
+ location=path,
778
+ )
779
+ return candidate
780
+ return path # pragma: no cover - a thousand identically named suites
781
+
782
+ def _suite_started(self, frame: _SuiteFrame) -> DomainEvent:
783
+ return self.emit(
784
+ EventType.SUITE_STARTED,
785
+ SuiteStartedData(
786
+ suite_id=frame.identity,
787
+ name=frame.name,
788
+ path=frame.path,
789
+ parent_id=frame.parent_id,
790
+ started_at=frame.started_at,
791
+ metadata=frame.metadata,
792
+ ),
793
+ subject=frame.path,
794
+ time=frame.started_at or self.clock,
795
+ )
796
+
797
+ def _close_suite(self, element: Any) -> Iterator[DomainEvent]:
798
+ if not self.stack:
799
+ return
800
+ frame = self.stack.pop()
801
+ if not self.header_flushed:
802
+ yield from self._flush_header()
803
+ self._check_counts(frame)
804
+ if frame.seconds is not None:
805
+ self.suite_seconds += frame.seconds
806
+ duration_ms = round(frame.seconds * 1000) if frame.seconds is not None else None
807
+ finished_at = (
808
+ frame.started_at + timedelta(seconds=frame.seconds)
809
+ if frame.started_at is not None and frame.seconds is not None
810
+ else None
811
+ )
812
+ if not frame.own_counts and not frame.descendant_counts:
813
+ self.warnings.info(
814
+ "empty_suite",
815
+ f"suite {frame.path!r} reported no test cases at all",
816
+ location=frame.path,
817
+ )
818
+ yield self.emit(
819
+ EventType.SUITE_FINISHED,
820
+ SuiteFinishedData(
821
+ suite_id=frame.identity,
822
+ finished_at=finished_at,
823
+ duration_ms=duration_ms,
824
+ ),
825
+ subject=frame.path,
826
+ time=finished_at or self.clock,
827
+ )
828
+ if self.stack:
829
+ rolled = frame.rolled_up()
830
+ for key, value in rolled.items():
831
+ self.stack[-1].descendant_counts[key] = (
832
+ self.stack[-1].descendant_counts.get(key, 0) + value
833
+ )
834
+ self._release(element)
835
+
836
+ def _close_open_suites(self) -> Iterator[DomainEvent]:
837
+ """A truncated file can leave suites open; close them rather than lose them."""
838
+ while self.stack:
839
+ frame = self.stack[-1]
840
+ self.warn_once(
841
+ "unclosed_suite",
842
+ f"suite {frame.path!r} was never closed by the file; closing it",
843
+ location=frame.path,
844
+ )
845
+ yield from self._close_suite(frame.element)
846
+ if self.implicit_suite is not None:
847
+ frame = self.implicit_suite
848
+ self.implicit_suite = None
849
+ yield self.emit(
850
+ EventType.SUITE_FINISHED,
851
+ SuiteFinishedData(suite_id=frame.identity, finished_at=None, duration_ms=None),
852
+ subject=frame.path,
853
+ time=self.clock,
854
+ )
855
+
856
+ def _check_counts(self, frame: _SuiteFrame) -> None:
857
+ """Compare the publisher's own totals with what was actually parsed."""
858
+ if not frame.declared:
859
+ return
860
+ own = frame.own_counts
861
+ rolled = frame.rolled_up()
862
+ keys = set(frame.declared)
863
+ if all(frame.declared[key] == own.get(key, 0) for key in keys):
864
+ return
865
+ if all(frame.declared[key] == rolled.get(key, 0) for key in keys):
866
+ return
867
+ self.warnings.add(
868
+ "counts_mismatch",
869
+ f"suite {frame.path!r} declares "
870
+ + ", ".join(f"{key}={frame.declared[key]}" for key in sorted(keys))
871
+ + " but "
872
+ + ", ".join(f"{key}={rolled.get(key, 0)}" for key in sorted(keys))
873
+ + " were parsed; the parsed numbers are the ones Test Base keeps",
874
+ location=frame.path,
875
+ declared=frame.declared,
876
+ parsed={key: rolled.get(key, 0) for key in sorted(keys)},
877
+ )
878
+
879
+ # ---------------------------------------------------------- properties
880
+
881
+ def _absorb_properties(self, element: Any) -> None:
882
+ frame = self.stack[-1] if self.stack else None
883
+ collected: dict[str, str] = {}
884
+ for child in element:
885
+ if _localname(child.tag) != "property":
886
+ continue
887
+ raw_name = child.attrib.get("name")
888
+ if not raw_name:
889
+ continue
890
+ value = child.attrib.get("value")
891
+ if value is None:
892
+ value = (child.text or "").strip()
893
+ clipped, _ = _clip(value, _SHORT_TEXT_LIMIT)
894
+ collected[str(raw_name)] = clipped or ""
895
+ if not collected:
896
+ return
897
+ if frame is not None:
898
+ frame.metadata.setdefault("properties", {}).update(collected)
899
+ else:
900
+ self.run_metadata.setdefault("properties", {}).update(collected)
901
+ self._lift_properties(collected, frame.path if frame else "testsuites")
902
+
903
+ def _lift_properties(self, collected: dict[str, str], location: str) -> None:
904
+ """Promote the well-known property names onto the run context."""
905
+ for raw_name, value in collected.items():
906
+ field = _RUN_PROPERTIES.get(raw_name.strip().lower().replace("-", "_"))
907
+ if field is None or not value:
908
+ continue
909
+ if self.header_flushed:
910
+ self.info_once(
911
+ "late_run_property",
912
+ f"property {raw_name!r} names run-level context but arrived after the run "
913
+ "header had been emitted; it is kept as suite metadata only",
914
+ location=location,
915
+ )
916
+ continue
917
+ if field in self.context_fields:
918
+ continue
919
+ cleaned = self._validated_property(field, value, raw_name, location)
920
+ if cleaned is not None:
921
+ self.context_fields[field] = cleaned
922
+
923
+ def _validated_property(
924
+ self, field: str, value: str, raw_name: str, location: str
925
+ ) -> str | None:
926
+ if field == "environment" and not _SLUG_RE.match(value):
927
+ self.warnings.add(
928
+ "invalid_property",
929
+ f"property {raw_name}={value!r} cannot be an environment name "
930
+ "(letters, digits, dot, dash, underscore and space only); kept as metadata",
931
+ location=location,
932
+ )
933
+ return None
934
+ if field == "pipeline_url" and not value.lower().startswith(("http://", "https://")):
935
+ self.warnings.add(
936
+ "invalid_property",
937
+ f"property {raw_name}={value!r} is not an http(s) URL; kept as metadata",
938
+ location=location,
939
+ )
940
+ return None
941
+ if field == "commit_sha":
942
+ return value[:64]
943
+ return value[:_SHORT_TEXT_LIMIT]
944
+
945
+ def _absorb_suite_output(self, tag: str, element: Any) -> None:
946
+ text = "".join(element.itertext()).strip()
947
+ if not text:
948
+ return
949
+ key = "system_out" if tag == "system-out" else "system_err"
950
+ if key in self.run_metadata:
951
+ return
952
+ clipped, truncated = _clip(text, _SUITE_OUTPUT_LIMIT)
953
+ self.run_metadata[key] = clipped
954
+ cut = f" (cut to {_SUITE_OUTPUT_LIMIT} characters)" if truncated else ""
955
+ self.info_once(
956
+ "suite_output_lifted",
957
+ f"suite-level <{tag}> has nowhere to live on a suite event, so it is attached "
958
+ f"to the run's metadata{cut}",
959
+ location=self.stack[-1].path if self.stack else "testsuites",
960
+ )
961
+
962
+ # ------------------------------------------------------------- testcase
963
+
964
+ def _finish_case(self, element: Any) -> Iterator[DomainEvent]:
965
+ frame = self.stack[-1] if self.stack else self._ensure_implicit_suite()
966
+ attrib = {str(key): str(value) for key, value in element.attrib.items()}
967
+ raw_name = attrib.get("name") or "(unnamed test)"
968
+ name, _ = _clip(raw_name, _SHORT_TEXT_LIMIT)
969
+ name = name or "(unnamed test)"
970
+ class_name, _ = _clip(attrib.get("classname") or attrib.get("class"), _SHORT_TEXT_LIMIT)
971
+ location = f"{frame.path}/{class_name or ''}::{name}"
972
+
973
+ attempts = self._collect_attempts(element, attrib, location)
974
+ seconds = self.seconds(attrib.get("time"), location)
975
+ if seconds is not None:
976
+ self.case_seconds += seconds
977
+
978
+ metadata: dict[str, Any] = {}
979
+ for key in ("line", "assertions", "group", "owner", "retries"):
980
+ if attrib.get(key):
981
+ metadata[key] = attrib[key][:_SHORT_TEXT_LIMIT]
982
+ case_properties = self._case_properties(element)
983
+ if case_properties:
984
+ metadata["properties"] = case_properties
985
+
986
+ file_name, _ = _clip(attrib.get("file"), _SHORT_TEXT_LIMIT)
987
+ identity = test_identity(
988
+ project=self.project,
989
+ suite_path=frame.path,
990
+ test_name=name,
991
+ class_name=class_name,
992
+ )
993
+ full_name = f"{class_name}.{name}" if class_name else name
994
+
995
+ # The whole case's `time` belongs to the final attempt; earlier attempts
996
+ # have no duration of their own in this format.
997
+ for number, attempt in enumerate(attempts, start=1):
998
+ is_last = number == len(attempts)
999
+ duration = attempt.seconds
1000
+ if duration is None and is_last:
1001
+ duration = seconds
1002
+ duration_ms = round(duration * 1000) if duration is not None else None
1003
+ if duration_ms is not None:
1004
+ self.clock = self.clock + timedelta(milliseconds=duration_ms)
1005
+ frame_status = attempt.status
1006
+ if is_last:
1007
+ frame.record(frame_status)
1008
+ yield self.emit(
1009
+ EventType.TEST_FINISHED,
1010
+ TestFinishedData(
1011
+ test_id=identity,
1012
+ suite_id=frame.identity,
1013
+ name=name,
1014
+ class_name=class_name,
1015
+ suite_path=frame.path,
1016
+ params=None,
1017
+ full_name=_clip(full_name, _SHORT_TEXT_LIMIT)[0],
1018
+ file=file_name,
1019
+ owner=None,
1020
+ tags=[],
1021
+ markers=[],
1022
+ attempt_number=number,
1023
+ status=attempt.status,
1024
+ original_status=attempt.original_status,
1025
+ started_at=None,
1026
+ finished_at=None,
1027
+ duration_ms=duration_ms,
1028
+ failure=attempt.failure,
1029
+ stdout=attempt.stdout,
1030
+ stderr=attempt.stderr,
1031
+ logs=None,
1032
+ skip_reason=attempt.skip_reason,
1033
+ worker=None,
1034
+ metadata=metadata if is_last else {"attempt_source": "surefire-rerun"},
1035
+ ),
1036
+ subject=full_name[:_SHORT_TEXT_LIMIT],
1037
+ )
1038
+
1039
+ def _ensure_implicit_suite(self) -> _SuiteFrame:
1040
+ """A ``<testcase>`` outside any ``<testsuite>`` still needs somewhere to live."""
1041
+ if self.implicit_suite is None:
1042
+ path = self._unique_path(self.root_attrib.get("name") or "(root)")
1043
+ self.implicit_suite = _SuiteFrame(
1044
+ element=None,
1045
+ name=path,
1046
+ path=path,
1047
+ identity=suite_identity(project=self.project, suite_path=path),
1048
+ parent_id=None,
1049
+ started_at=None,
1050
+ seconds=None,
1051
+ metadata={},
1052
+ declared={},
1053
+ )
1054
+ self.warn_once(
1055
+ "orphan_testcase",
1056
+ "the file has <testcase> elements outside any <testsuite>; they were grouped "
1057
+ f"into a synthetic suite called {path!r}",
1058
+ location=path,
1059
+ )
1060
+ return self.implicit_suite
1061
+
1062
+ def _case_properties(self, element: Any) -> dict[str, str]:
1063
+ collected: dict[str, str] = {}
1064
+ for child in element:
1065
+ if _localname(child.tag) != "properties":
1066
+ continue
1067
+ for prop in child:
1068
+ if _localname(prop.tag) != "property":
1069
+ continue
1070
+ key = prop.attrib.get("name")
1071
+ if not key:
1072
+ continue
1073
+ value = prop.attrib.get("value")
1074
+ if value is None:
1075
+ value = (prop.text or "").strip()
1076
+ collected[str(key)] = str(value)[:_SHORT_TEXT_LIMIT]
1077
+ return collected
1078
+
1079
+ def _collect_attempts(
1080
+ self, element: Any, attrib: dict[str, str], location: str
1081
+ ) -> list[_Attempt]:
1082
+ """Earlier attempts from the rerun elements, then the case's own outcome."""
1083
+ attempts: list[_Attempt] = []
1084
+ outcomes: list[tuple[str, Any]] = []
1085
+ stdout_parts: list[str] = []
1086
+ stderr_parts: list[str] = []
1087
+
1088
+ for child in element:
1089
+ tag = _localname(child.tag)
1090
+ if tag in _RETRY_TAGS:
1091
+ attempts.append(self._retry_attempt(tag, child, location))
1092
+ elif tag in _OUTCOME_TAGS:
1093
+ outcomes.append((tag, child))
1094
+ elif tag == "system-out":
1095
+ stdout_parts.append("".join(child.itertext()))
1096
+ elif tag == "system-err":
1097
+ stderr_parts.append("".join(child.itertext()))
1098
+
1099
+ attempts.append(self._final_attempt(attrib, outcomes, location))
1100
+ final = attempts[-1]
1101
+ final.stdout = self._text("stdout", stdout_parts, location)
1102
+ final.stderr = self._text("stderr", stderr_parts, location)
1103
+ return attempts
1104
+
1105
+ def _text(self, what: str, parts: list[str], location: str) -> str | None:
1106
+ if not parts:
1107
+ return None
1108
+ joined = "\n".join(part for part in parts if part.strip())
1109
+ clipped, truncated = _clip(joined, self.text_limit)
1110
+ if truncated:
1111
+ self.warn_once(
1112
+ "text_truncated",
1113
+ f"{what} longer than {self.text_limit} characters was cut; the rest is in the "
1114
+ "stored raw report",
1115
+ location=location,
1116
+ )
1117
+ return clipped
1118
+
1119
+ def _retry_attempt(self, tag: str, child: Any, location: str) -> _Attempt:
1120
+ status = _RETRY_TAGS[tag]
1121
+ return _Attempt(
1122
+ status=status,
1123
+ original_status=tag,
1124
+ seconds=self.seconds(child.attrib.get("time"), location),
1125
+ failure=self._failure(child, location),
1126
+ stdout=self._text("stdout", [self._child_text(child, "system-out")], location),
1127
+ stderr=self._text("stderr", [self._child_text(child, "system-err")], location),
1128
+ )
1129
+
1130
+ def _final_attempt(
1131
+ self, attrib: dict[str, str], outcomes: list[tuple[str, Any]], location: str
1132
+ ) -> _Attempt:
1133
+ raw_status = (attrib.get("status") or "").strip()
1134
+ if outcomes:
1135
+ statuses = [_OUTCOME_TAGS[tag] for tag, _ in outcomes]
1136
+ status = worst_status(statuses)
1137
+ if len({*statuses}) > 1:
1138
+ self.warnings.add(
1139
+ "conflicting_outcome",
1140
+ "this test carries "
1141
+ + " and ".join(sorted({f"<{tag}>" for tag, _ in outcomes}))
1142
+ + f"; the more severe one ({status.value}) is the outcome",
1143
+ location=location,
1144
+ )
1145
+ chosen = next(child for tag, child in outcomes if _OUTCOME_TAGS[tag] is status)
1146
+ skip_reason = None
1147
+ failure = None
1148
+ original = raw_status or _localname(chosen.tag)
1149
+ if status is TestStatus.SKIPPED:
1150
+ skip_reason, _ = _clip(
1151
+ chosen.attrib.get("message") or "".join(chosen.itertext()).strip(),
1152
+ _SHORT_TEXT_LIMIT,
1153
+ )
1154
+ status, original = self._refine_skip(chosen, status, original)
1155
+ else:
1156
+ failure = self._failure(chosen, location)
1157
+ return _Attempt(
1158
+ status=status,
1159
+ original_status=original,
1160
+ failure=failure,
1161
+ skip_reason=skip_reason,
1162
+ )
1163
+
1164
+ if not raw_status or raw_status.lower() in _EXECUTION_MARKERS:
1165
+ # An empty <testcase/> means the test ran and nothing went wrong.
1166
+ return _Attempt(status=TestStatus.PASSED, original_status=raw_status or None)
1167
+
1168
+ status = normalize_status(raw_status)
1169
+ if status is TestStatus.UNKNOWN:
1170
+ self.warnings.add(
1171
+ "unknown_status",
1172
+ f"status={raw_status!r} is not a word Test Base can map; the test is recorded "
1173
+ "as unknown rather than guessed at",
1174
+ location=location,
1175
+ status=raw_status,
1176
+ )
1177
+ return _Attempt(status=status, original_status=raw_status)
1178
+
1179
+ def _refine_skip(
1180
+ self, element: Any, status: TestStatus, original: str
1181
+ ) -> tuple[TestStatus, str]:
1182
+ """Let ``<skipped type="pytest.xfail">`` be an xfail rather than a plain skip.
1183
+
1184
+ pytest has no ``<xfail>`` element — it writes an expected failure as a
1185
+ ``<skipped>`` with the intent in the ``type`` attribute. Recording that
1186
+ as ``skipped`` would quietly remove every xfail from the pass-rate
1187
+ denominator, which is the opposite of what ``xfailed`` means.
1188
+ """
1189
+ raw_type = element.attrib.get("type")
1190
+ if not raw_type:
1191
+ return status, original
1192
+ refined = normalize_status(str(raw_type).rsplit(".", 1)[-1])
1193
+ if refined in (TestStatus.XFAILED, TestStatus.XPASSED, TestStatus.CANCELLED):
1194
+ return refined, str(raw_type)[:_SHORT_TEXT_LIMIT]
1195
+ return status, original
1196
+
1197
+ def _child_text(self, element: Any, tag: str) -> str:
1198
+ for child in element:
1199
+ if _localname(child.tag) == tag:
1200
+ return "".join(child.itertext())
1201
+ return ""
1202
+
1203
+ def _failure(self, element: Any, location: str) -> FailureDetail | None:
1204
+ """Build a :class:`FailureDetail` from a ``<failure>``-shaped element.
1205
+
1206
+ The trace is the element's own text plus any ``<stackTrace>`` child, and
1207
+ pointedly *not* ``itertext()``: a Surefire rerun element also contains
1208
+ ``<system-out>``, and folding the console log into the stack trace makes
1209
+ every failure look identical.
1210
+ """
1211
+ parts: list[str] = []
1212
+ if element.text and element.text.strip():
1213
+ parts.append(element.text)
1214
+ for child in element:
1215
+ if _localname(child.tag) == "stacktrace":
1216
+ parts.append("".join(child.itertext()))
1217
+ body = "\n".join(part for part in parts if part.strip()).strip()
1218
+
1219
+ type_text, _ = _clip(element.attrib.get("type"), _SHORT_TEXT_LIMIT)
1220
+ message, _ = _clip(element.attrib.get("message"), self.text_limit)
1221
+ if message is None and body:
1222
+ message, _ = _clip(body.splitlines()[0], _SHORT_TEXT_LIMIT)
1223
+ stack, truncated = _clip(body, self.text_limit)
1224
+ if truncated:
1225
+ self.warn_once(
1226
+ "text_truncated",
1227
+ f"a stack trace longer than {self.text_limit} characters was cut",
1228
+ location=location,
1229
+ )
1230
+ if not any((type_text, message, stack)):
1231
+ return None
1232
+ return FailureDetail(
1233
+ type=type_text,
1234
+ message=message,
1235
+ stack_trace=stack,
1236
+ truncated=truncated,
1237
+ )
1238
+
1239
+ # ------------------------------------------------------------- closing
1240
+
1241
+ def _run_finished(self) -> DomainEvent:
1242
+ started = self.run_started_at or self.parse_started
1243
+ total = self.suite_seconds or self.case_seconds
1244
+ root_time = self.seconds(self.root_attrib.get("time"), "testsuites")
1245
+ if root_time:
1246
+ total = root_time
1247
+ duration_ms = round(total * 1000) if total else None
1248
+ finished = started + timedelta(seconds=total) if total else max(started, self.clock)
1249
+ return self.emit(
1250
+ EventType.RUN_FINISHED,
1251
+ RunFinishedData(
1252
+ finished_at=finished,
1253
+ status=None, # advisory only; the server folds the attempts
1254
+ duration_ms=duration_ms,
1255
+ metadata=self.run_metadata,
1256
+ ),
1257
+ subject=self.project,
1258
+ time=finished,
1259
+ )
1260
+
1261
+ # ------------------------------------------------------------- memory
1262
+
1263
+ def _release(self, element: Any) -> None:
1264
+ """Drop a finished element so a huge file stays a stream, not a DOM."""
1265
+ if element is None:
1266
+ return
1267
+ element.clear()
1268
+ parent = self.stack[-1].element if self.stack else self.root_element
1269
+ if parent is None or parent is element:
1270
+ return
1271
+ with contextlib.suppress(ValueError, AttributeError):
1272
+ parent.remove(element)