upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/ci_expected_failures.py
blob: 866706dddd9e1c90f2708ef368dce9fc2225ec42 (plain)
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
"""Judge a CI test run that has known failures, each fixed by an open pull request.

    python -I scripts/ci_expected_failures.py FAILED_LIST EXIT_STATUS [EXPECTED ...]

FAILED_LIST is a UTF-8 file with one failed test name per line and an optional BOM.
EXIT_STATUS is the test runner's exit status. Each EXPECTED argument is one or more whitespace-separated
test names, so a workflow can pass one quoted list.

Exits 1 when an unexpected test failed, the runner failed without naming a test, or a signal terminated the runner.
Exits 2 on a usage error. `--help` prints this text. An expected failure absent from a completed run gets a GitHub
Actions notice so the list in the workflow can shrink.
"""

import sys


def judge(failed: list[str], status: int, expected: list[str]) -> tuple[int, list[str]]:
    """(exit status, GitHub Actions workflow commands to print)."""
    unexpected = [name for name in failed if name not in expected]
    lines = [f"::error::{name} failed" for name in unexpected]
    # subprocess reports a signal as -N; the shell pipelines in runtime.yml report it as 128+N.
    if status < 0 or 128 < status <= 255:
        lines.append(f"::error::abnormal test runner exit {status}; the failure list may be incomplete")
        return 1, lines
    if status != 0 and not failed:
        lines.append(f"::error::the test runner exited with {status} without naming a failed test")
        return 1, lines
    for name in expected:
        if name in failed:
            lines.append(f"::warning::{name} failed, as expected until its fix is merged")
        else:
            lines.append(f"::notice::{name} passed: drop it from the expected failures")
    return (1 if unexpected else 0), lines


def main(argv: list[str]) -> int:
    if argv and argv[0] in ("-h", "--help"):
        print(__doc__.strip())
        return 0
    if len(argv) < 2:
        print(__doc__.strip(), file=sys.stderr)
        return 2
    try:
        status = int(argv[1])
    except ValueError:
        print(f"ci_expected_failures: EXIT_STATUS {argv[1]!r} is not an integer", file=sys.stderr)
        return 2
    try:
        with open(argv[0], encoding="utf-8-sig") as f:
            failed = [line.strip() for line in f if line.strip()]
    except (OSError, UnicodeDecodeError) as e:
        print(f"ci_expected_failures: {argv[0]}: {getattr(e, 'strerror', None) or e}", file=sys.stderr)
        return 2
    expected = [name for arg in argv[2:] for name in arg.split()]
    code, lines = judge(failed, status, expected)
    print("\n".join(lines))
    return code


if __name__ == "__main__":
    try:  # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode
        sys.stdout.reconfigure(errors="replace")
    except (AttributeError, ValueError, OSError):
        pass
    sys.exit(main(sys.argv[1:]))