summaryrefslogtreecommitdiff
path: root/util/twister_tags.py
blob: 692fdc1e99102ab16e923cff67a7dff4474e6f11 (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
#!/usr/bin/env vpython3

# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Script that contains definitions for Twister test tags while also checking
testcase.yaml files to see if they only used predefined tags.
"""

# [VPYTHON:BEGIN]
# python_version: "3.8"
# wheel: <
#   name: "infra/python/wheels/pyyaml-py3"
#   version: "version:5.3.1"
# >
# [VPYTHON:END]

import argparse
import logging
import os
import pathlib
import sys

import yaml  # pylint: disable=import-error

TAG_TO_DESCRIPTION = {
    "common": "Directly test shared code in the ec/common dir",
    "mkbp": "Testing the MKBP (Matrix Keyboard Protocol) stack",
    "system": "Directly test functions in common/system.c or shim/src/system.c",
    "spi": "SPI related tests",
}

SCRIPT_PATH = os.path.realpath(__file__)


def main(args):
    """List and/or validate testcase.yaml tags."""
    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--list-tags", action="store_true")
    parser.add_argument("--validate-files", nargs="*", type=pathlib.Path)
    args = parser.parse_args()

    list_tags = args.list_tags
    validate_files = args.validate_files

    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__file__)

    if list_tags:
        logger.info("Listing defined Twister testcase.yaml test tags:")
        for tag, description in TAG_TO_DESCRIPTION.items():
            logger.info("%s=%s", tag, description)

    if validate_files:
        testcase_yamls = []

        bad_tag = False

        for validated_file in validate_files:
            if os.path.basename(validated_file) == "testcase.yaml":
                if validated_file.exists():
                    testcase_yamls.append(validated_file)

        def test_tags_generator(root):
            """Returns space separated tags denoted by a tags key"""
            if "tags" in root:
                for tag in root["tags"].split(" "):
                    yield tag

            for val in root.values():
                if isinstance(val, dict):
                    for found in test_tags_generator(val):
                        yield found

        for testcase_yaml in testcase_yamls:
            logger.info("Validating test tags in %s", testcase_yaml)
            with open(testcase_yaml) as yaml_fd:
                yaml_obj = yaml.safe_load(yaml_fd)
                for tag in test_tags_generator(yaml_obj):
                    if tag not in TAG_TO_DESCRIPTION:
                        bad_tag = True
                        # TODO(b/249280155) Suggest tag
                        logger.error(
                            f'Unrecognized tag: "{tag}" in {testcase_yaml}'
                            + f' define "{tag}" in {SCRIPT_PATH}',
                        )

        if bad_tag:
            sys.exit(1)


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