xref: /aosp_15_r20/external/pigweed/pw_build/test_info.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15# Generate metadata for tests.
16#
17# Various types of tests may use this template to create metadata used to
18# describe those tests. All entries will have the following form:
19#
20#   {
21#       "build_label": <GN source-absolute label>,
22#       "test_name": <Name of test>,
23#       "test_type": <Type of test>,
24#       ...
25#   }
26#
27# where "..." may represent additional, type-specific details.
28#
29# Args:
30#   - test_type (required): String describing the type of test. One of:
31#         "test_group"
32#         "unit_test"
33#         "action_test"
34#         "perf_test"
35#         "fuzz_test"
36#   - test_name (optional): Name of the test as a string. Defaults to the
37#         target name.
38#   - build_label (optional): GN label for the test being described. Defaults to
39#         the source-absolute label corresponding to `test_name`.
40#   - extra_metadata (optional): Scope that defines additional metadata to
41#         include for this test. Variables defined in the scope will be included
42#         directly in the metadata, and thus should not be named "build_label",
43#         "test_name", or "test_type".
44template("pw_test_info") {
45  assert(defined(invoker.test_type),
46         "`pw_test_info($target_name)` is missing `test_type`")
47  _type = invoker.test_type
48
49  if (defined(invoker.test_name)) {
50    _name = invoker.test_name
51  } else {
52    _name = get_label_info(target_name, "name")
53  }
54
55  if (defined(invoker.build_label)) {
56    _label = invoker.build_label
57  } else {
58    _label = _name
59  }
60
61  _metadata = {
62    # Include the extra metadata first, so any name collisions trigger errors.
63    forward_variables_from(invoker.extra_metadata, "*")
64    build_label = get_label_info(":$_label", "label_no_toolchain")
65    test_name = _name
66    test_type = _type
67    if (defined(invoker.tags) && invoker.tags != []) {
68      tags = invoker.tags
69    }
70  }
71
72  group(target_name) {
73    forward_variables_from(invoker, [ "testonly" ])
74    if (_type == "test_group") {
75      metadata = {
76        test_groups = [ _metadata ]
77      }
78      forward_variables_from(invoker, [ "deps" ])
79    } else if (_type == "unit_test") {
80      metadata = {
81        unit_tests = [ _metadata ]
82      }
83    } else if (_type == "action_test") {
84      metadata = {
85        action_tests = [ _metadata ]
86      }
87    } else if (_type == "perf_test") {
88      metadata = {
89        perf_tests = [ _metadata ]
90      }
91    } else if (_type == "fuzz_test") {
92      metadata = {
93        fuzz_tests = [ _metadata ]
94      }
95    } else {
96      assert(
97          false,
98          "pw_test_info($target_name) does not have a valid `test_type`: $_type")
99    }
100  }
101}
102