aboutsummaryrefslogtreecommitdiffstats
path: root/.github/scripts/tests/junit-postprocess.py
blob: 3c4fdb05cc4f2e7db31c502ed8ca9dc361985751 (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
#!/usr/bin/env python3
import os
import glob
import argparse
import xml.etree.ElementTree as ET
from mute_utils import mute_target, update_suite_info, MutedTestCheck
from junit_utils import add_junit_property


def case_iterator(root):
    for case in root.findall("testcase"):
        cls, method = case.attrib["classname"], case.attrib["name"]
        yield case, cls, method


def attach_filename(testcase, filename):
    shardname = os.path.splitext(filename)[0]
    add_junit_property(testcase, "shard", shardname)


def postprocess_junit(is_mute_test, folder, dry_run):
    for fn in glob.glob(os.path.join(folder, "*.xml")):
        tree = ET.parse(fn)
        root = tree.getroot()
        total_muted = 0
        for suite in root.findall("testsuite"):
            muted_cnt = 0

            for case, cls, method in case_iterator(suite):
                attach_filename(case, os.path.basename(fn))

                if is_mute_test(cls, method):
                    if mute_target(case):
                        print(f"mute {cls}::{method}")
                        muted_cnt += 1

            if muted_cnt:
                update_suite_info(suite, n_skipped=muted_cnt, n_remove_failures=muted_cnt)
                total_muted += muted_cnt

        if total_muted:
            update_suite_info(root, n_skipped=total_muted, n_remove_failures=total_muted)

        print(f"{'(dry-run) ' if dry_run else ''}patch {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("yunit_path")
    args = parser.parse_args()

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

    # FIXME: add gtest filter file ?
    is_mute_test = MutedTestCheck(args.filter_file)

    if not is_mute_test.has_rules:
        print("nothing to mute")
        return

    postprocess_junit(is_mute_test, args.yunit_path, args.dry_run)


if __name__ == "__main__":
    main()