summaryrefslogtreecommitdiffstats
path: root/layout/tools/reftest/reftest/__init__.py
blob: f82a07ec44cd6d35bc9c125ac051fb8f31c34144 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import io
import os
import re

import six

RE_COMMENT = re.compile(r"\s+#")
RE_HTTP = re.compile(r"HTTP\((\.\.(\/\.\.)*)\)")
RE_PROTOCOL = re.compile(r"^\w+:")
FAILURE_TYPES = (
    "fails",
    "fails-if",
    "needs-focus",
    "random",
    "random-if",
    "silentfail",
    "silentfail-if",
    "skip",
    "skip-if",
    "slow",
    "slow-if",
    "fuzzy",
    "fuzzy-if",
    "require-or",
    "asserts",
    "asserts-if",
)
PREF_ITEMS = (
    "pref",
    "test-pref",
    "ref-pref",
)
RE_ANNOTATION = re.compile(r"(.*)\((.*)\)")


class ReftestManifest(object):
    """Represents a parsed reftest manifest."""

    def __init__(self, finder=None):
        self.path = None
        self.dirs = set()
        self.files = set()
        self.manifests = set()
        self.tests = []
        self.finder = finder

    def load(self, path):
        """Parse a reftest manifest file."""

        def add_test(file, annotations, referenced_test=None):
            # We can't package about:, data:, or chrome: URIs.
            # Discarding data isn't correct for a parser. But retaining
            # all data isn't currently a requirement.
            if RE_PROTOCOL.match(file):
                return
            test = os.path.normpath(os.path.join(mdir, urlprefix + file))
            if test in self.files:
                # if test path has already been added, make no changes, to
                # avoid duplicate paths in self.tests
                return
            self.files.add(test)
            self.dirs.add(os.path.dirname(test))
            test_dict = {
                "path": test,
                "here": os.path.dirname(test),
                "manifest": normalized_path,
                "name": os.path.basename(test),
                "head": "",
                "support-files": "",
                "subsuite": "",
            }
            if referenced_test:
                test_dict["referenced-test"] = referenced_test
            for annotation in annotations:
                m = RE_ANNOTATION.match(annotation)
                if m:
                    if m.group(1) not in test_dict:
                        test_dict[m.group(1)] = m.group(2)
                    else:
                        test_dict[m.group(1)] += ";" + m.group(2)
                else:
                    test_dict[annotation] = None
            self.tests.append(test_dict)

        normalized_path = os.path.normpath(os.path.abspath(path))
        self.manifests.add(normalized_path)
        if not self.path:
            self.path = normalized_path

        mdir = os.path.dirname(normalized_path)
        self.dirs.add(mdir)

        if self.finder:
            lines = self.finder.get(path).read().splitlines()
        else:
            with io.open(path, "r", encoding="utf-8") as fh:
                lines = fh.read().splitlines()

        urlprefix = ""
        defaults = []
        for i, line in enumerate(lines):
            lineno = i + 1
            line = six.ensure_text(line)

            # Entire line is a comment.
            if line.startswith("#"):
                continue

            # Comments can begin mid line. Strip them.
            m = RE_COMMENT.search(line)
            if m:
                line = line[: m.start()]
            line = line.strip()
            if not line:
                continue

            items = line.split()
            if items[0] == "defaults":
                defaults = items[1:]
                continue

            items = defaults + items
            annotations = []
            for i in range(len(items)):
                item = items[i]

                if item.startswith(FAILURE_TYPES) or item.startswith(PREF_ITEMS):
                    annotations += [item]
                    continue
                if item == "HTTP":
                    continue

                m = RE_HTTP.match(item)
                if m:
                    # Need to package the referenced directory.
                    self.dirs.add(os.path.normpath(os.path.join(mdir, m.group(1))))
                    continue

                if i < len(defaults):
                    raise ValueError(
                        "Error parsing manifest {}, line {}: "
                        "Invalid defaults token '{}'".format(path, lineno, item)
                    )

                if item == "url-prefix":
                    urlprefix = items[i + 1]
                    break

                if item == "include":
                    self.load(os.path.join(mdir, items[i + 1]))
                    break

                if item == "load" or item == "script":
                    add_test(items[i + 1], annotations)
                    break

                if item == "==" or item == "!=" or item == "print":
                    add_test(items[i + 1], annotations)
                    add_test(items[i + 2], annotations, items[i + 1])
                    break