summaryrefslogtreecommitdiffstats
path: root/tools/lint/test-manifest-toml/__init__.py
blob: 08f0e4ed93665792b45dfcf1b266c9c50aee0262 (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
# 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

from manifestparser import TestManifest
from manifestparser.toml import DEFAULT_SECTION, alphabetize_toml_str, sort_paths
from mozlint import result
from mozlint.pathutils import expand_exclusions
from mozpack import path as mozpath
from tomlkit.items import Array, Table

SECTION_REGEX = r"^\[.*\]$"
DISABLE_REGEX = r"^[ \t]*#[ \t]*\[.*\]"


def make_result(path, message, is_error=False):
    if is_error:
        level = "error"
    else:
        level = "warning"
    result = {
        "path": path,
        "lineno": 0,  # tomlkit does not report lineno/column
        "column": 0,
        "message": message,
        "level": level,
    }
    return result


def lint(paths, config, fix=None, **lintargs):
    results = []
    fixed = 0
    topsrcdir = lintargs["root"]
    file_names = list(expand_exclusions(paths, config, topsrcdir))
    file_names = [os.path.normpath(f) for f in file_names]
    section_rx = re.compile(SECTION_REGEX, flags=re.M)
    disable_rx = re.compile(DISABLE_REGEX, flags=re.M)

    for file_name in file_names:
        path = mozpath.relpath(file_name, topsrcdir)
        os.path.basename(file_name)
        parser = TestManifest(use_toml=True, document=True)

        try:
            parser.read(file_name)
        except Exception:
            r = make_result(path, "The manifest is not valid TOML.", True)
            results.append(result.from_config(config, **r))
            continue

        manifest = parser.source_documents[file_name]
        manifest_str = io.open(file_name, "r", encoding="utf-8").read()

        if not DEFAULT_SECTION in manifest:
            r = make_result(
                path, f"The manifest does not start with a [{DEFAULT_SECTION}] section."
            )
            if fix:
                fixed += 1
            results.append(result.from_config(config, **r))

        sections = [k for k in manifest.keys() if k != DEFAULT_SECTION]
        sorted_sections = sort_paths(sections)
        if sections != sorted_sections:
            r = make_result(
                path, "The manifest sections are not in alphabetical order."
            )
            if fix:
                fixed += 1
            results.append(result.from_config(config, **r))

        m = section_rx.findall(manifest_str)
        if len(m) > 0:
            for section_match in m:
                section = section_match[1:-1]
                if section == DEFAULT_SECTION:
                    continue
                if not section.startswith('"'):
                    r = make_result(
                        path, f"The section name must be double quoted: [{section}]"
                    )
                    if fix:
                        fixed += 1
                    results.append(result.from_config(config, **r))

        m = disable_rx.findall(manifest_str)
        if len(m) > 0:
            for disabled_section in m:
                r = make_result(
                    path,
                    f"Use 'disabled = \"<reason>\"' to disable a test instead of a comment: {disabled_section}",
                    True,
                )
                results.append(result.from_config(config, **r))

        for section, keyvals in manifest.body:
            if section is None:
                continue
            if not isinstance(keyvals, Table):
                r = make_result(
                    path, f"Bad assignment in preamble: {section} = {keyvals}", True
                )
                results.append(result.from_config(config, **r))
            else:
                for k, v in keyvals.items():
                    if k.endswith("-if"):
                        if not isinstance(v, Array):
                            r = make_result(
                                path,
                                f'Value for conditional must be an array: {k} = "{v}"',
                                True,
                            )
                            results.append(result.from_config(config, **r))
                        else:
                            for e in v:
                                if e.find("||") > 0 and e.find("&&") < 0:
                                    r = make_result(
                                        path,
                                        f'Value for conditional must not include explicit ||, instead put on multiple lines: {k} = [ ... "{e}" ... ]',
                                        True,
                                    )
                                    results.append(result.from_config(config, **r))

        if fix:
            manifest_str = alphabetize_toml_str(manifest)
            fp = io.open(file_name, "w", encoding="utf-8", newline="\n")
            fp.write(manifest_str)
            fp.close()

    return {"results": results, "fixed": fixed}