summaryrefslogtreecommitdiff
path: root/util/check_ztest.py
blob: 69cb6a08df5c1d06f26602ee3d29ef31d8199ce7 (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
#!/usr/bin/env vpython3
# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Checks files for invalid ZTEST decls."""

import re
import sys
from typing import List, Optional

import preupload.lib


ZTEST_RE = re.compile(
    r"(ZTEST|ZTEST_F|ZTEST_USER|ZTEST_USER_F)\(\w+,\s*($|\w+)"
)


def main(argv: Optional[List[str]] = None) -> Optional[int]:
    """Look at all files passed in on commandline for ZTEST decls where the
    test is not named test_.
    """
    return_code = 0
    parser = preupload.lib.argument_parser()

    args = parser.parse_args(argv)
    for filename in args.filename:
        lines = preupload.lib.cat_file(args, filename).splitlines()
        line_iter = iter(enumerate(lines, start=1))
        for linenum, line in line_iter:
            line = line.rstrip("\n")
            match = ZTEST_RE.search(line)
            if match and match.group(2) == "":
                linenum, line2 = next(line_iter)
                line += line2.rstrip("\n")
                match = ZTEST_RE.search(line)
            if match and not match.group(2).startswith("test_"):
                print(
                    "error: 'test_' prefix missing from test function name"
                    f"\n{filename}:{linenum}: {match.group(2)}",
                    file=sys.stderr,
                )
                return_code = 1

    return return_code


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