aboutsummaryrefslogtreecommitdiffstats
path: root/.github/scripts/tests/pytest-postprocess.py
blob: 9c0873691fc66b462b7a6d5b0bee0b48c15b5cd6 (plain) (blame)
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
#!/usr/bin/env python3
import argparse
import glob
import os
import xml.etree.ElementTree as ET
from mute_utils import MuteTestCheck, mute_target, recalc_suite_info
from junit_utils import get_property_value


def update_testname(testcase):
    filename = get_property_value(testcase, "filename")

    clsname = testcase.get("classname")
    tstname = testcase.get("name")

    if filename is None:
        return f"{clsname}::{tstname}"

    filename = filename.split("/")
    test_fn = filename[-1]
    folder = "/".join(filename[:-1])

    testcase.set("classname", folder)

    clsname = clsname.split(".")[-1]

    test_name = f"{test_fn}::{clsname}::{tstname}"

    testcase.set("name", test_name)
    testcase.set("id", f"{folder}_{test_fn}_{clsname}_{tstname}")

    return f"{folder}/{test_name}"


def postprocess_pytest(fn, mute_check, dry_run):
    tree = ET.parse(fn)
    root = tree.getroot()

    for testsuite in root.findall("testsuite"):
        need_recalc = False
        for testcase in testsuite.findall("testcase"):
            new_name = update_testname(testcase)
            if mute_check(new_name) and mute_target(testcase):
                print(f"mute {new_name}")
                need_recalc = True

        if need_recalc:
            recalc_suite_info(testsuite)

    print(f"{'(dry-run) ' if dry_run else ''}save {fn}")

    if not dry_run:
        tree.write(fn, xml_declaration=True, encoding="UTF-8")


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--filter-file", required=True)
    parser.add_argument("--dry-run", action="store_true", default=False)
    parser.add_argument("pytest_xml_path")

    args = parser.parse_args()

    if not os.path.isdir(args.pytest_xml_path):
        print(f"{args.pytest_xml_path} is not a directory, exit")
        raise SystemExit(-1)

    mute_check = MuteTestCheck(args.filter_file)

    for fn in glob.glob(os.path.join(args.pytest_xml_path, "*.xml")):
        postprocess_pytest(fn, mute_check, args.dry_run)


if __name__ == "__main__":
    main()