-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha11y_gate.py
More file actions
885 lines (757 loc) · 31.9 KB
/
Copy patha11y_gate.py
File metadata and controls
885 lines (757 loc) · 31.9 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
#!/usr/bin/env python3
"""Accessibility gate for the rendered receipt page (B-043).
The design constraint here is not "run an accessibility checker". It is
**never report a pass you did not earn**, because the failure mode this gate
exists to avoid is the one that keeps happening: a checker pointed at a 404
page reporting "5 passed, 0 violations, exit 0", or a page list that was empty
and exited 0 because there was nothing to disagree with. An error page has no
accessibility violations. Neither does a blank one.
So this gate is built around three assertions it makes about itself.
**It renders its own subjects from ground truth.** The pages are rendered here,
in process, from the bundled synthetic reference fixture, and the expected
payload hash, case id, and mandated-limitation wording come from the *receipt
document* rather than from the page. A page that does not carry the hash of the
receipt it was supposed to render is the wrong subject, and the gate says so
instead of auditing it.
**It counts.** Declared pages, audited pages, and per-check examined counts are
all in the report, and a check that examined nothing is a finding
(``check-examined-nothing``) rather than a silent pass. So is an empty page set
(``no-pages``).
**It never converts "could not determine" into "fine".** axe-core runs in a DOM
with no layout engine, so it cannot evaluate colour contrast; those results
arrive as ``undetermined``, are listed by name, and the contrast rule is
computed here from the stylesheet instead.
Checks
------
``html-validity``
Well-formedness, one ``<h1>``, one ``<main>``, ``lang`` on ``<html>``,
``<title>``, a charset, no duplicate ids, no skipped heading level, tables
with captions and scoped headers, every ``aria-labelledby`` and in-page
``href`` resolving to an id that exists, no script, no external resource.
``contrast``
WCAG 2.2 contrast for every foreground/background pair declared in the
stylesheet, in the screen rules and again in the print rules. A rule that
sets ``color`` without setting ``background-color`` in the same block is a
finding: not because that is invalid CSS, but because it makes the pair
unknowable, and an unknowable pair is how a contrast check quietly examines
nothing.
``color-only``
Every element carrying a status or boolean marker must also carry a text
label from the published catalog and a non-colour symbol. Removing all
colour from the page must lose no information.
``print``
A ``@media print`` block must exist, must not hide a mandated disclosure,
and must not leave text on a background it cannot be read against.
``axe``
axe-core in a headless DOM. Violations fail. Undetermined rules are
reported by name and never counted as passes. A page for which axe executed
no rules at all fails as ``engine-examined-nothing``.
Engines are requested explicitly and a requested engine that cannot run is a
failure, never a skip: ``--engines builtin,axe`` fails with
``engine-unavailable`` if the node harness is not installed. Requesting no
engine at all is ``no-engines`` for the same reason ``no-pages`` exists: a run
that examined nothing has not earned the word "clean", and ``--engines ''``
reaching exit 0 would be this gate committing the defect it was written to
catch.
pa11y is not available here. Its engine, HTML_CodeSniffer, loads its rulesets by
injecting script tags and does not complete in a headless DOM without a browser;
it is not wired in rather than wired in and quietly skipped. The rules it would
add over axe — contrast, colour-only encoding, print — are the ones computed
above.
Exit 0 when clean, 1 when anything is found, 2 on a usage error.
"""
from __future__ import annotations
import argparse
import json
import re
import shutil
import subprocess
import sys
from collections.abc import Iterable, Iterator, Sequence
from dataclasses import dataclass, field
from html.parser import HTMLParser
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT / "src") not in sys.path: # pragma: no cover - import shim
sys.path.insert(0, str(REPO_ROOT / "src"))
from contextsafe.evaluator import evaluate # noqa: E402
from contextsafe.html_receipt import PAGE_KIND, render_receipt_page # noqa: E402
from contextsafe.i18n import load_catalog # noqa: E402
from contextsafe.receipt import build_receipt_document # noqa: E402
from contextsafe.validation import parse_bundle # noqa: E402
REFERENCE = REPO_ROOT / "fixtures" / "reference"
HARNESS = REPO_ROOT / "tools" / "a11y" / "run.mjs"
HARNESS_MODULES = REPO_ROOT / "tools" / "a11y" / "node_modules"
MINIMUM_CONTRAST = 4.5
"""WCAG 2.2 AA for body text. Applied to every pair, including large text."""
BUILTIN_CHECKS = ("html-validity", "contrast", "color-only", "print")
ENGINES = ("builtin", "axe")
UNDETERMINED_COVERAGE = {
"color-contrast": "contrast",
"landmark-one-main": "html-validity",
"page-has-heading-one": "html-validity",
}
"""Rules axe cannot decide without layout, and the check that decides them.
A headless DOM has no rendering, so axe returns these as ``incomplete`` rather
than as passes. Treating "could not determine" as "fine" is how an audit ends
up reporting a clean page it never looked at, so every undetermined rule must
map to a check here that does decide it. An undetermined rule with no entry is
itself a finding: it means the gate stopped covering something and nobody
noticed.
"""
_VOID = frozenset(
{
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr",
}
)
_HEADINGS = ("h1", "h2", "h3", "h4", "h5", "h6")
@dataclass(frozen=True, slots=True)
class Finding:
"""One gate failure, reported by rule, subject, and detail."""
rule: str
subject: str
detail: str
def __str__(self) -> str:
"""Return the one-line report form."""
return f" {self.rule}: {self.subject}: {self.detail}"
@dataclass(frozen=True, slots=True)
class Subject:
"""A page the gate intends to audit, and how it will recognise it."""
name: str
locale: str
html: str
payload_sha256: str
case_id: str
required_text: tuple[str, ...]
@dataclass(slots=True)
class CheckResult:
"""What one check examined, and what it found."""
name: str
examined: int = 0
findings: list[Finding] = field(default_factory=list)
@dataclass(slots=True)
class Report:
"""The whole run, including the counts that make a pass meaningful."""
declared: int = 0
audited: int = 0
engines_requested: tuple[str, ...] = ()
engines_executed: list[str] = field(default_factory=list)
checks: list[CheckResult] = field(default_factory=list)
undetermined: list[str] = field(default_factory=list)
findings: list[Finding] = field(default_factory=list)
def to_dict(self) -> dict[str, object]:
"""Return the machine-readable form of this report."""
return {
"declared_pages": self.declared,
"audited_pages": self.audited,
"engines_requested": list(self.engines_requested),
"engines_executed": self.engines_executed,
"checks": [
{
"name": check.name,
"examined": check.examined,
"findings": len(check.findings),
}
for check in self.checks
],
"undetermined_rules": sorted(set(self.undetermined)),
"findings": [
{"rule": item.rule, "subject": item.subject, "detail": item.detail}
for item in self.findings
],
}
class _Document(HTMLParser):
"""A minimal structural model of the page, built without a browser."""
def __init__(self) -> None:
super().__init__(convert_charrefs=True)
self.stack: list[str] = []
self.elements: list[tuple[str, dict[str, str]]] = []
self.ids: list[str] = []
self.headings: list[str] = []
self.mismatched: list[str] = []
self.text_by_element: list[tuple[str, dict[str, str], str]] = []
self.raw_by_element: list[tuple[str, dict[str, str], str]] = []
self._open: list[tuple[str, dict[str, str], int]] = []
self._raw = ""
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
"""Record an element and push it if it can contain content."""
attributes = {name: (value or "") for name, value in attrs}
self.elements.append((tag, attributes))
if "id" in attributes:
self.ids.append(attributes["id"])
if tag in _HEADINGS:
self.headings.append(tag)
if tag not in _VOID:
self.stack.append(tag)
self._open.append((tag, attributes, len(self._raw)))
def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
"""Treat a self-closed tag as a void element."""
self.handle_starttag(tag, attrs)
if tag not in _VOID and self.stack and self.stack[-1] == tag:
self.stack.pop()
self._open.pop()
def handle_endtag(self, tag: str) -> None:
"""Pop the element, recording any tag that does not match."""
if not self.stack or self.stack[-1] != tag:
self.mismatched.append(tag)
return
self.stack.pop()
name, attributes, start = self._open.pop()
raw = self._raw[start:]
self.raw_by_element.append((name, attributes, raw))
self.text_by_element.append(
(name, attributes, re.sub(r"<[^>]*>", " ", raw).strip())
)
def handle_data(self, data: str) -> None:
"""Accumulate character data into the raw buffer."""
self._raw += data
def unknown_decl(self, data: str) -> None: # pragma: no cover - defensive
"""Ignore unknown declarations rather than failing the parse."""
def feed(self, data: str) -> None:
"""Parse ``data``, keeping raw offsets aligned with the input."""
for chunk in re.split(r"(<[^>]*>)", data):
if chunk.startswith("<"):
self._raw += chunk
super().feed(chunk)
else:
super().feed(chunk)
def parse_document(html: str) -> _Document:
"""Return the structural model of ``html``."""
document = _Document()
document.feed(html)
document.close()
return document
def _luminance(hex_colour: str) -> float:
value = hex_colour.lstrip("#")
if len(value) == 3:
value = "".join(char * 2 for char in value)
channels = [int(value[index : index + 2], 16) / 255 for index in (0, 2, 4)]
linear = [
channel / 12.92 if channel <= 0.04045 else ((channel + 0.055) / 1.055) ** 2.4
for channel in channels
]
return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2]
def contrast_ratio(foreground: str, background: str) -> float:
"""Return the WCAG 2.2 contrast ratio between two hex colours."""
lighter, darker = sorted(
(_luminance(foreground), _luminance(background)), reverse=True
)
return (lighter + 0.05) / (darker + 0.05)
def _stylesheet(html: str) -> str:
match = re.search(r"<style>(.*?)</style>", html, flags=re.S)
return match.group(1) if match else ""
def _split_media(style: str) -> tuple[str, str]:
"""Return the screen rules and the print rules separately."""
match = re.search(r"@media print \{(.*?)\n\}", style, flags=re.S)
if match is None:
return style, ""
return style[: match.start()] + style[match.end() :], match.group(1)
def _rules(css: str) -> Iterator[tuple[str, dict[str, str]]]:
for selector, block in re.findall(r"([^{}]+)\{([^{}]*)\}", css):
declarations = {}
for declaration in block.split(";"):
if ":" not in declaration:
continue
name, _, value = declaration.partition(":")
declarations[name.strip()] = value.strip()
yield selector.strip(), declarations
def check_contrast(subject: Subject) -> CheckResult:
"""Compute the contrast of every declared foreground/background pair."""
result = CheckResult("contrast")
screen, printed = _split_media(_stylesheet(subject.html))
for scope, css in (("screen", screen), ("print", printed)):
for selector, declarations in _rules(css):
foreground = declarations.get("color")
background = declarations.get("background-color")
if foreground is None:
continue
if background is None:
result.findings.append(
Finding(
"contrast",
subject.name,
f"{scope} rule {selector!r} sets color with no "
"background-color in the same block, so the pair "
"cannot be checked",
)
)
continue
result.examined += 1
ratio = contrast_ratio(foreground, background)
if ratio < MINIMUM_CONTRAST:
result.findings.append(
Finding(
"contrast",
subject.name,
f"{scope} rule {selector!r}: {foreground} on "
f"{background} is {ratio:.2f}:1, below "
f"{MINIMUM_CONTRAST}:1",
)
)
return result
def check_color_only(subject: Subject) -> CheckResult:
"""Every status must survive the page being printed without colour."""
result = CheckResult("color-only")
document = parse_document(subject.html)
catalog = load_catalog(subject.locale)
labels = {
message.text
for key, message in catalog.messages.items()
if key.startswith(("status.", "value."))
}
for tag, attributes, raw in document.raw_by_element:
marker = attributes.get("data-cs-status") or attributes.get("data-cs-boolean")
if marker is None or tag not in ("td", "th", "dd"):
continue
result.examined += 1
text = re.sub(r"<[^>]*>", " ", raw)
if 'class="status-symbol"' not in raw:
result.findings.append(
Finding(
"color-only",
subject.name,
f"{tag} marked {marker!r} carries no non-colour symbol",
)
)
if not any(label and label in text for label in labels):
result.findings.append(
Finding(
"color-only",
subject.name,
f"{tag} marked {marker!r} carries no published text label",
)
)
return result
def check_print(subject: Subject) -> CheckResult:
"""The page has to print, disclosures included."""
result = CheckResult("print")
_, printed = _split_media(_stylesheet(subject.html))
if not printed.strip():
result.findings.append(
Finding("print", subject.name, "the page has no @media print rules")
)
return result
protected = (".notice", ".source-text", "main", "section", "table")
for selector, declarations in _rules(printed):
result.examined += 1
if declarations.get("display") != "none":
continue
if any(name in selector for name in protected):
result.findings.append(
Finding(
"print",
subject.name,
f"print rule {selector!r} hides content the page must "
"keep when printed",
)
)
return result
def _validity_findings(subject: Subject, document: _Document) -> Iterator[Finding]:
html_attrs = next(
(attrs for tag, attrs in document.elements if tag == "html"), None
)
if html_attrs is None or not html_attrs.get("lang"):
yield Finding("html-validity", subject.name, "<html> has no lang attribute")
if document.mismatched:
yield Finding(
"html-validity",
subject.name,
f"unbalanced tags: {sorted(set(document.mismatched))}",
)
if document.stack:
yield Finding("html-validity", subject.name, f"unclosed tags: {document.stack}")
duplicates = sorted({i for i in document.ids if document.ids.count(i) > 1})
if duplicates:
yield Finding("html-validity", subject.name, f"duplicate ids: {duplicates}")
yield from _landmark_findings(subject, document)
yield from _heading_findings(subject, document)
yield from _reference_findings(subject, document)
yield from _resource_findings(subject, document)
def _landmark_findings(subject: Subject, document: _Document) -> Iterator[Finding]:
tags = [tag for tag, _ in document.elements]
for tag, expected in (("h1", 1), ("main", 1), ("title", 1)):
if tags.count(tag) != expected:
yield Finding(
"html-validity",
subject.name,
f"expected exactly {expected} <{tag}>, found {tags.count(tag)}",
)
if not any(
tag == "meta" and "charset" in attrs for tag, attrs in document.elements
):
yield Finding("html-validity", subject.name, "no character encoding declared")
for tag, attrs, _ in document.raw_by_element:
if tag == "table" and "<caption" not in _raw_of(document, tag, attrs):
yield Finding("html-validity", subject.name, "a table has no <caption>")
for tag, attrs in document.elements:
if tag == "th" and not attrs.get("scope"):
yield Finding("html-validity", subject.name, "a <th> has no scope")
if tag == "img" and "alt" not in attrs:
yield Finding("html-validity", subject.name, "an <img> has no alt")
def _raw_of(document: _Document, tag: str, attrs: dict[str, str]) -> str:
for name, attributes, raw in document.raw_by_element:
if name == tag and attributes == attrs:
return raw
return ""
def _heading_findings(subject: Subject, document: _Document) -> Iterator[Finding]:
previous = 0
for heading in document.headings:
level = int(heading[1])
if previous and level > previous + 1:
yield Finding(
"html-validity",
subject.name,
f"heading level jumps from h{previous} to h{level}",
)
previous = level
def _reference_findings(subject: Subject, document: _Document) -> Iterator[Finding]:
known = set(document.ids)
for tag, attrs in document.elements:
target = attrs.get("aria-labelledby")
if target and target not in known:
yield Finding(
"html-validity",
subject.name,
f"<{tag} aria-labelledby={target!r}> points at no element",
)
href = attrs.get("href", "")
if href.startswith("#") and href[1:] not in known:
yield Finding(
"html-validity",
subject.name,
f"in-page link {href!r} points at no element",
)
def _resource_findings(subject: Subject, document: _Document) -> Iterator[Finding]:
lowered = subject.html.lower()
for forbidden in ("<script", "<iframe", "<object", "<embed"):
if forbidden in lowered:
yield Finding("html-validity", subject.name, f"page contains {forbidden}>")
if re.search(r"\son[a-z]+\s*=", lowered):
yield Finding(
"html-validity", subject.name, "page contains an inline event handler"
)
for scheme in ("http://", "https://", "//cdn", "url("):
if scheme in lowered:
yield Finding(
"html-validity",
subject.name,
f"page references an external resource ({scheme})",
)
def check_html_validity(subject: Subject) -> CheckResult:
"""Structural validity, the parts a headless DOM cannot judge for us."""
result = CheckResult("html-validity")
document = parse_document(subject.html)
result.examined = len(document.elements)
result.findings.extend(_validity_findings(subject, document))
return result
def assert_subject(subject: Subject) -> list[Finding]:
"""Prove this page is the page we meant to audit, before auditing it.
Everything compared here comes from the receipt document rather than from
the page, so an error page, a truncated file, or a page rendered from some
other receipt cannot satisfy it by being internally consistent.
"""
findings: list[Finding] = []
html = subject.html
if not html.strip():
findings.append(Finding("wrong-subject", subject.name, "page is empty"))
return findings
if not html.startswith("<!DOCTYPE html>"):
findings.append(
Finding("wrong-subject", subject.name, "page has no HTML doctype")
)
expected = (
(f'data-cs-page="{PAGE_KIND}"', "is not a receipt page"),
(
f'data-cs-payload-sha256="{subject.payload_sha256}"',
"does not carry the payload hash of the receipt it should render",
),
(f'data-cs-case-id="{subject.case_id}"', "does not carry the expected case id"),
(f'lang="{subject.locale}"', "is not in the locale it was rendered for"),
("<h1 ", "has no level-one heading"),
("<main ", "has no main landmark"),
)
for needle, complaint in expected:
if needle not in html:
findings.append(Finding("wrong-subject", subject.name, f"page {complaint}"))
for required in subject.required_text:
if required not in html:
findings.append(
Finding(
"wrong-subject",
subject.name,
f"page is missing required content: {required[:60]!r}",
)
)
return findings
def build_subjects(locales: Sequence[str]) -> tuple[Subject, ...]:
"""Render one page per locale from the bundled reference fixture."""
def read(name: str) -> object:
return json.loads((REFERENCE / name).read_text(encoding="utf-8"))
bundle = parse_bundle(
read("case.json"), # type: ignore[arg-type]
read("observations.json"), # type: ignore[arg-type]
read("rules.json"), # type: ignore[arg-type]
)
document = build_receipt_document(bundle, evaluate(bundle))
payload = document["payload"]
if not isinstance(payload, dict): # pragma: no cover - build_receipt_document
raise TypeError("receipt payload is not an object")
limitations = payload["limitations"]
if not isinstance(limitations, list): # pragma: no cover - schema-pinned
raise TypeError("receipt limitations are not a list")
case_id = str(payload["case_id"])
return tuple(
Subject(
name=f"receipt.{locale}.html",
locale=locale,
html=render_receipt_page(document, locale=locale),
payload_sha256=str(document["payload_sha256"]),
case_id=case_id,
required_text=tuple(str(item) for item in limitations),
)
for locale in locales
)
def run_axe(
subjects: Sequence[Subject], workdir: Path
) -> tuple[list[Finding], list[str], bool]:
"""Run the axe harness, treating an unavailable engine as a failure."""
findings: list[Finding] = []
undetermined: list[str] = []
node = shutil.which("node")
if not HARNESS_MODULES.is_dir() or node is None:
findings.append(
Finding(
"engine-unavailable",
"axe",
"node or tools/a11y/node_modules is missing; run "
"`make a11y-install`. A requested engine that cannot run is a "
"failure, not a skip",
)
)
return findings, undetermined, False
paths: list[str] = []
for subject in subjects:
target = workdir / subject.name
target.write_text(subject.html, encoding="utf-8")
paths.append(str(target))
completed = subprocess.run( # noqa: S603 - resolved argv, no shell
[node, str(HARNESS), *paths],
capture_output=True,
text=True,
cwd=HARNESS.parent,
check=False,
)
try:
payload = json.loads(completed.stdout or "{}")
except json.JSONDecodeError:
payload = {}
if not payload.get("ok"):
findings.append(
Finding(
"engine-unavailable",
"axe",
f"harness did not run: {payload.get('error') or completed.stderr.strip()[:200]}",
)
)
return findings, undetermined, False
for page in payload.get("pages", []):
name = Path(str(page["page"])).name
if int(page.get("executedRules", 0)) == 0:
findings.append(
Finding(
"engine-examined-nothing",
name,
"axe executed no rules against this page, so its "
"zero-violation result means nothing",
)
)
undetermined.extend(str(rule) for rule in page.get("undetermined", []))
for violation in page.get("violations", []):
findings.append(
Finding(
"axe",
name,
f"{violation['id']} ({violation.get('impact')}) on "
f"{violation.get('nodes')} node(s): {violation.get('help')}",
)
)
return findings, undetermined, True
_CHECKS = {
"html-validity": check_html_validity,
"contrast": check_contrast,
"color-only": check_color_only,
"print": check_print,
}
def audit(
subjects: Sequence[Subject], *, engines: Sequence[str], workdir: Path
) -> Report:
"""Audit ``subjects`` and return a report that counts what it examined."""
report = Report(declared=len(subjects), engines_requested=tuple(engines))
if not engines:
report.findings.append(
Finding(
"no-engines",
"-",
"no engine was requested, so no check ran; a page nothing "
"examined is not a page that passed",
)
)
if not subjects:
report.findings.append(
Finding(
"no-pages",
"-",
"no page was declared, so nothing was audited and nothing is proved",
)
)
return report
for engine in engines:
if engine not in ENGINES:
report.findings.append(
Finding("engine-unknown", engine, f"known engines are {list(ENGINES)}")
)
audited = _accept_subjects(subjects, report)
_run_engines(audited, engines=engines, workdir=workdir, report=report)
report.findings.extend(_coverage_findings(report, engines))
report.findings.extend(_undetermined_findings(report))
return report
def _accept_subjects(subjects: Sequence[Subject], report: Report) -> list[Subject]:
"""Return only the pages that proved they are the pages we meant."""
audited: list[Subject] = []
for subject in subjects:
mismatches = assert_subject(subject)
if mismatches:
report.findings.extend(mismatches)
continue
audited.append(subject)
report.audited = len(audited)
return audited
def _run_engines(
audited: Sequence[Subject],
*,
engines: Sequence[str],
workdir: Path,
report: Report,
) -> None:
"""Run each requested engine and fold its results into ``report``."""
if "builtin" in engines:
report.engines_executed.append("builtin")
report.checks.extend(_run_builtin(audited))
for check in report.checks:
report.findings.extend(check.findings)
if "axe" in engines:
findings, undetermined, ran = run_axe(audited, workdir)
if ran:
report.engines_executed.append("axe")
report.findings.extend(findings)
report.undetermined.extend(undetermined)
def _coverage_findings(report: Report, engines: Sequence[str]) -> Iterator[Finding]:
"""Fail on a page that was not audited or an engine that did not run."""
if report.audited != report.declared:
yield Finding(
"coverage",
"-",
f"declared {report.declared} page(s) but audited "
f"{report.audited}; an unaudited page is not a passing page",
)
for engine in engines:
if engine in ENGINES and engine not in report.engines_executed:
yield Finding("engine-not-executed", engine, "requested but did not run")
def _undetermined_findings(report: Report) -> Iterator[Finding]:
"""Fail on any rule axe could not decide and nothing else decides either."""
executed = {check.name for check in report.checks if check.examined > 0}
for rule in sorted(set(report.undetermined)):
covering = UNDETERMINED_COVERAGE.get(rule)
if covering is None:
yield Finding(
"undetermined-uncovered",
rule,
"axe could not determine this rule and no check here decides "
"it, so nothing has actually been verified about it",
)
elif covering not in executed:
yield Finding(
"undetermined-uncovered",
rule,
f"axe could not determine this rule and its covering check "
f"{covering!r} examined nothing",
)
def _run_builtin(subjects: Iterable[Subject]) -> list[CheckResult]:
totals = {name: CheckResult(name) for name in BUILTIN_CHECKS}
for subject in subjects:
for name, check in _CHECKS.items():
outcome = check(subject)
totals[name].examined += outcome.examined
totals[name].findings.extend(outcome.findings)
for name, result in totals.items():
if result.examined == 0:
result.findings.append(
Finding(
"check-examined-nothing",
name,
"the check examined nothing, so its clean result is not a pass",
)
)
return list(totals.values())
def main(argv: Sequence[str] | None = None) -> int:
"""Run the gate and return 0 clean, 1 with findings, 2 on usage error."""
parser = argparse.ArgumentParser(description="Accessibility gate (B-043).")
parser.add_argument(
"--engines",
default="builtin",
help="comma-separated engines to require; a requested engine that "
"cannot run is a failure, never a skip",
)
parser.add_argument(
"--locale", action="append", help="restrict to one locale; repeatable"
)
parser.add_argument("--json", type=Path, help="write the full report here")
parser.add_argument(
"--workdir",
type=Path,
default=REPO_ROOT / ".a11y",
help="where rendered pages are written for external engines",
)
args = parser.parse_args(argv)
engines = tuple(part for part in args.engines.split(",") if part)
locales = tuple(args.locale) if args.locale else ("en-US", "es-US")
args.workdir.mkdir(parents=True, exist_ok=True)
report = audit(build_subjects(locales), engines=engines, workdir=args.workdir)
if args.json:
args.json.write_text(
json.dumps(report.to_dict(), indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
print(
f"a11y-gate: {report.audited}/{report.declared} page(s) audited; "
f"engines executed: {', '.join(report.engines_executed) or 'none'}"
)
for check in report.checks:
print(f" {check.name}: examined {check.examined}")
if report.undetermined:
print(
" undetermined by axe (never counted as a pass): "
+ ", ".join(sorted(set(report.undetermined)))
)
if report.findings:
print(f"a11y-gate: {len(report.findings)} finding(s)")
for finding in report.findings:
print(finding)
return 1
print("a11y-gate: clean")
return 0
if __name__ == "__main__": # pragma: no cover - module entry point
raise SystemExit(main())