blob: d0c3c6bc494c133b10254d7e9982294a51592ace [file] [log] [blame]
Alexei Frolov925fb8f2019-11-05 16:32:30 -08001# Copyright 2019 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
15import("$dir_pw_build/pw_executable.gni")
Alexei Frolovd1f98fa2019-11-08 15:41:37 -080016import("$dir_pw_build/python_script.gni")
Alexei Frolov925fb8f2019-11-05 16:32:30 -080017
Alexei Frolov925fb8f2019-11-05 16:32:30 -080018# Creates an executable target for a unit test.
Alexei Frolova454c682019-11-19 10:55:07 -080019#
Alexei Frolov8403f0a2019-11-20 13:57:54 -080020# If the pw_automatic_test_runner variable is set, this template also creates a
21# "${test_name}_run" target which runs the unit test executable after building
22# it.
Alexei Frolov925fb8f2019-11-05 16:32:30 -080023#
24# This template accepts all of the regular "executable" target args.
25template("pw_test") {
26 # This is required in order to reference the pw_test template's target name
27 # within the test_metadata of the metadata group below. The group() definition
28 # creates a new scope where the "target_name" variable is set to its target,
29 # shadowing the one in this scope.
Alexei Frolovd1f98fa2019-11-08 15:41:37 -080030 _test_target_name = target_name
Alexei Frolov925fb8f2019-11-05 16:32:30 -080031
Alexei Frolova454c682019-11-19 10:55:07 -080032 pw_executable(_test_target_name) {
33 # Metadata for this test when used as part of a pw_test_group target.
Alexei Frolov925fb8f2019-11-05 16:32:30 -080034 metadata = {
Alexei Frolova454c682019-11-19 10:55:07 -080035 tests = [
Alexei Frolov925fb8f2019-11-05 16:32:30 -080036 {
Alexei Frolova454c682019-11-19 10:55:07 -080037 type = "test"
Alexei Frolovd1f98fa2019-11-08 15:41:37 -080038 test_name = _test_target_name
Alexei Frolova454c682019-11-19 10:55:07 -080039 test_directory = rebase_path(target_out_dir, root_build_dir)
Alexei Frolov925fb8f2019-11-05 16:32:30 -080040 },
41 ]
42 }
Alexei Frolov925fb8f2019-11-05 16:32:30 -080043
Alexei Frolova454c682019-11-19 10:55:07 -080044 forward_variables_from(invoker, "*", [ "metadata" ])
Paul Mathieu97b967a2019-11-21 09:01:03 -080045
46 if (!defined(deps)) {
47 deps = []
48 }
49 deps += [ pw_unit_test_main ]
Alexei Frolov925fb8f2019-11-05 16:32:30 -080050 }
51
Alexei Frolov8403f0a2019-11-20 13:57:54 -080052 if (pw_automatic_test_runner != "") {
53 # When the automatic runner is set, create an action which runs the unit
54 # test executable using the test runner script.
Alexei Frolovd1f98fa2019-11-08 15:41:37 -080055 _run_action_name = _test_target_name + "_run"
Alexei Frolov925fb8f2019-11-05 16:32:30 -080056
Alexei Frolovd1f98fa2019-11-08 15:41:37 -080057 pw_python_script(_run_action_name) {
Rob Mohra0ba54f2020-02-27 11:43:49 -080058 deps = [ ":$_test_target_name" ]
59 inputs = [ pw_automatic_test_runner ]
Alexei Frolov3dbfcf02019-11-22 16:38:02 -080060 script = "$dir_pw_unit_test/py/pw_unit_test/test_runner.py"
Alexei Frolov8403f0a2019-11-20 13:57:54 -080061 args = [
62 "--runner",
63 pw_automatic_test_runner,
64 "--test",
65 get_path_info("$target_out_dir:$_test_target_name", "abspath"),
66 ]
Alexei Frolov917756d2019-11-12 14:34:34 -080067 stamp = true
Alexei Frolov925fb8f2019-11-05 16:32:30 -080068 }
69 }
70}
Alexei Frolova454c682019-11-19 10:55:07 -080071
72# Defines a related collection of unit tests.
73#
74# pw_test_group targets output a JSON metadata file for the Pigweed test runner.
75#
76# Args:
77# tests: List of pw_test targets for each of the tests in the group.
78# group_deps: Optional pw_test_group targets on which this group depends.
79template("pw_test_group") {
Alexei Frolov8403f0a2019-11-20 13:57:54 -080080 _group_target = target_name
81 _group_deps_metadata = []
Wyatt Hepleredf6f292019-11-25 18:52:21 -080082 if (defined(invoker.tests)) {
83 _deps = invoker.tests
84 } else {
85 _deps = []
86 }
Alexei Frolov8403f0a2019-11-20 13:57:54 -080087
Alexei Frolova454c682019-11-19 10:55:07 -080088 if (defined(invoker.group_deps)) {
89 # If the group specified any other group dependencies, create a metadata
90 # entry for each of them indicating that they are another group and a group
91 # target to collect that metadata.
Alexei Frolova454c682019-11-19 10:55:07 -080092 foreach(dep, invoker.group_deps) {
Alexei Frolov8403f0a2019-11-20 13:57:54 -080093 _group_deps_metadata += [
Alexei Frolova454c682019-11-19 10:55:07 -080094 {
95 type = "dep"
96 group = get_path_info(dep, "abspath")
97 },
98 ]
99 }
100
Alexei Frolov8403f0a2019-11-20 13:57:54 -0800101 _deps += invoker.group_deps
Alexei Frolova454c682019-11-19 10:55:07 -0800102 }
103
Alexei Frolov8403f0a2019-11-20 13:57:54 -0800104 _metadata_group_target = "${target_name}_pw_test_group_metadata"
105 group(_metadata_group_target) {
106 metadata = {
107 group_deps = _group_deps_metadata
108 self = [
109 {
110 type = "self"
111 name = get_path_info(":$_group_target", "abspath")
112 },
113 ]
114
115 # Metadata from the group's own unit test targets is forwarded through
116 # the group dependencies group. This entry is listed as a "walk_key" in
117 # the generated file so that only test targets' metadata (not group
118 # targets) appear in the output.
Wyatt Hepleredf6f292019-11-25 18:52:21 -0800119 if (defined(invoker.tests)) {
120 propagate_metadata_from = invoker.tests
121 }
Alexei Frolov8403f0a2019-11-20 13:57:54 -0800122 }
123 deps = _deps
124 }
125
126 _test_group_deps = [ ":$_metadata_group_target" ]
127
128 generated_file(_group_target) {
Rob Mohra0ba54f2020-02-27 11:43:49 -0800129 outputs = [ "$target_out_dir/$target_name.testinfo.json" ]
Alexei Frolova454c682019-11-19 10:55:07 -0800130 data_keys = [
131 "group_deps",
Alexei Frolov8403f0a2019-11-20 13:57:54 -0800132 "self",
Alexei Frolova454c682019-11-19 10:55:07 -0800133 "tests",
134 ]
135 walk_keys = [ "propagate_metadata_from" ]
136 output_conversion = "json"
137 deps = _test_group_deps
138 }
Wyatt Hepleredf6f292019-11-25 18:52:21 -0800139
140 # If automatic test running is enabled, create a *_run group that collects all
141 # of the individual *_run targets and groups.
142 if (pw_automatic_test_runner != "") {
143 group(_group_target + "_run") {
Rob Mohra0ba54f2020-02-27 11:43:49 -0800144 deps = [ ":$_group_target" ]
Wyatt Hepleredf6f292019-11-25 18:52:21 -0800145 foreach(_target, _deps) {
146 deps += [ "${_target}_run" ]
147 }
148 }
149 }
Alexei Frolova454c682019-11-19 10:55:07 -0800150}