File size: 3,365 Bytes
6b1923e
 
 
 
 
 
 
 
 
c172b70
6b1923e
c7eced2
6b1923e
 
 
 
 
 
 
7991bb4
6b1923e
 
 
 
 
 
 
 
dead914
 
 
 
 
 
 
6b1923e
 
 
dead914
 
 
6b1923e
dead914
 
6b1923e
 
 
 
 
 
 
 
 
 
 
 
dead914
 
6b1923e
 
 
 
 
 
 
 
 
 
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
"""Fail closed on named real-browser DOM-search contracts, not coverage."""
import hashlib
import os
from pathlib import Path
import re
import subprocess
import sys


ROOT = Path(__file__).resolve().parents[2]
MODULE = ROOT / 'static/reader/search/reader-dom-search.mjs'
TEST_MODULE = 'tests.reader.browser.test_reader_dom_search_contracts'
CONTRACTS = {
    'test_literal_escape_snippets_and_pagination': 'helpers',
    'test_cross_node_highlight_cleanup_and_stale_navigation': 'crossNodeAndStaleNavigation',
    'test_progressive_paging_snapshot_reuse_and_activation': 'progressivePagingAndReuse',
    'test_synchronous_mutation_invalidates_snapshot': 'synchronousSnapshotInvalidation',
    'test_cancelled_search_cannot_publish_final_results': 'cancellation',
    'test_heading_labels_and_excluded_text': 'headingsAndExcludedNodes',
    'test_large_heading_order_snapshot_and_fallback': 'largeHeadingOrder',
    'test_occurrence_selection_and_stale_navigation': 'occurrenceSelection',
}


def valid_output(stdout, stderr, browser, digest):
    """Require exactly one success per named test and matching browser marker."""
    if browser not in ('chromium', 'firefox') or not re.fullmatch(r'[0-9a-f]{64}', digest):
        return False
    markers = stdout.splitlines()
    pattern = rf'DOM_SEARCH_CONTRACT_COMPLETED {browser} ([A-Za-z][A-Za-z0-9_]*) {digest}'
    matches = [re.fullmatch(pattern, line) for line in markers]
    if any(match is None for match in matches):
        return False
    completed = {match[1] for match in matches if match is not None}
    if len(completed) != len(markers) or not set(CONTRACTS.values()) <= completed:
        return False
    lines = stderr.splitlines()
    expected_tests = {f'{name} ({TEST_MODULE}.ReaderDomSearchContracts.{name}) ... ok' for name in CONTRACTS}
    passed_test = re.compile(rf'(test_\w+) \({re.escape(TEST_MODULE)}\.ReaderDomSearchContracts\.\1\) \.\.\. ok')
    found = [line for line in lines if passed_test.fullmatch(line)]
    if len(found) != len(set(found)) or len(found) != len(completed) or not expected_tests <= set(found):
        return False
    summaries = [line for line in lines if re.fullmatch(rf'Ran {len(found)} tests in \d+(?:\.\d+)?s', line)]
    allowed = set(found) | {'', '-' * 70, 'OK'} | set(summaries)
    return (len(summaries) == 1 and lines.count('OK') == 1 and all(line in allowed for line in lines))


def run_browser(browser, digest):
    env = {**os.environ, 'READER_DOM_SEARCH_BROWSER': browser}
    # Fixed interpreter, unittest module and test name; no shell or input arguments.
    result = subprocess.run([sys.executable, '-B', '-m', 'unittest', '-v', TEST_MODULE],  # noqa: S603
                            cwd=ROOT, env=env, text=True, capture_output=True)
    sys.stdout.write(result.stdout)
    sys.stderr.write(result.stderr)
    if result.returncode or not valid_output(result.stdout, result.stderr, browser, digest):
        raise SystemExit(f'DOM-search browser contracts incomplete: {browser}')
    print(f'DOM-search browser contracts: {browser}, {len(result.stdout.splitlines())} passed, '
          f'zero skipped, source SHA-256 {digest}', flush=True)


def main():
    digest = hashlib.sha256(MODULE.read_bytes()).hexdigest()
    for browser in ('chromium', 'firefox'):
        run_browser(browser, digest)


if __name__ == '__main__':
    main()