summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webvtt/tools/categorize_results.py
blob: 6cb18c3c2de2f5d637e3ca38efdf85bfb34c031e (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
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
import os
import sys
import json
import fnmatch

TEST_DIR = "/webvtt/"
CATEGORIES_FILE = "../categories.json"

class Test:
    def __init__(self, file, name, status, message):
        self.file = file
        self.name = name
        self.status = status
        self.message = message
        self.passed = status == 'PASS'
        self.categories = []

    @classmethod
    def from_json(cls, json):
        file = json["test"]
        if not file.startswith(TEST_DIR):
            return []
        file = file[len(TEST_DIR):]

        status = json["status"]
        message = json["message"]

        tests = []

        for test in json["subtests"]:
            name = test["name"]
            if status == 'OK':
                test_status = test["status"]
                test_message = test["message"]
            else:
                test_status, test_message = status, message

            tests.append(Test(file, name, test_status, test_message))

        return tests

class Category:
    def __init__(self, names):
        self.names = set(names)
        self.tests = {}

    @classmethod
    def from_json(cls, json):
        return Category(json)

    def add_test(self, name, test):
        self.tests[test] = name

    def __contains__(self, name):
        return name in self.names

def parse_results(file):
    data = json.load(file)

    results = data["results"]
    tests = []
    for result in results:
        tests += Test.from_json(result)

    return tests

def parse_categories(file, tests, categories = None, categories_map = None):
    data = json.load(file)
    basepath = os.path.dirname(file.name)

    categories = categories or []

    if categories_map:
        categories_map = dict(categories_map)
    else:
        categories_map = {}

    if ":categories" in data:
        for cat_data in data[":categories"]:
            category = Category.from_json(cat_data)

            categories.append(category)
            for name in category.names:
                categories_map[name] = category

    for pattern, category_name in data.items():
        if pattern.startswith(":"):
            continue
        category = categories_map[category_name]

        file_pattern = os.path.normpath(os.path.join(basepath, pattern))
        for test in tests:
            if fnmatch.fnmatch(test.name, file_pattern) or fnmatch.fnmatch(test.file, file_pattern):
                category.add_test(category_name, test)
                test.categories.append(category)

    if ":subcategories" in data:
        for subcat_name in data[":subcategories"]:
            path = os.path.join(basepath, subcat_name)
            file = open(path, "r")
            parse_categories(file, tests, categories, categories_map)

    return categories

def main(argv):
    if len(argv) == 1:
        if argv[0] == '-':
            results_file = sys.stdin
        else:
            results_file = open(argv[0], "r")
    else:
        print("USAGE: python3 categorize_results.py <file>")
        print("<file>\tA file containing wpt results. Or `-` for reading results from stdin.")
        return

    filepath = os.path.dirname(__file__)
    categories_path = os.path.join(filepath, CATEGORIES_FILE)
    categories_file = open(categories_path, "r")

    tests = parse_results(results_file)
    categories = parse_categories(categories_file, tests)

    for category in categories:
        tests_by_name = { name: [] for name in category.names }
        for test, name in category.tests.items():
            tests_by_name[name].append(test)

        for name in category.names:
            test_group = tests_by_name[name]
            amount = len(test_group)
            if amount == 0:
                continue
            passed = sum(1 for test in test_group if test.passed)
            print("{}:\t{}/{} - {}%".format(name, passed, amount, round(passed / amount * 100, 2)))

if __name__ == "__main__":
    main(sys.argv[1:])