blob: b55c22b4930914471199690b18cf7b09ae3f38b4 [file] [log] [blame]
# Copyright 2019 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import("$dir_pw_build/pw_executable.gni")
import("$dir_pw_build/python_script.gni")
# Creates an executable target for a unit test.
#
# If the pw_automatic_test_runner variable is set, this template also creates a
# "${test_name}_run" target which runs the unit test executable after building
# it.
#
# This template accepts all of the regular "executable" target args.
template("pw_test") {
# This is required in order to reference the pw_test template's target name
# within the test_metadata of the metadata group below. The group() definition
# creates a new scope where the "target_name" variable is set to its target,
# shadowing the one in this scope.
_test_target_name = target_name
pw_executable(_test_target_name) {
# Metadata for this test when used as part of a pw_test_group target.
metadata = {
tests = [
{
type = "test"
test_name = _test_target_name
test_directory = rebase_path(target_out_dir, root_build_dir)
},
]
}
forward_variables_from(invoker, "*", [ "metadata" ])
if (!defined(deps)) {
deps = []
}
deps += [ pw_unit_test_main ]
}
if (pw_automatic_test_runner != "") {
# When the automatic runner is set, create an action which runs the unit
# test executable using the test runner script.
_run_action_name = _test_target_name + "_run"
pw_python_script(_run_action_name) {
deps = [
":$_test_target_name",
]
script = "$dir_pw_unit_test/py/pw_unit_test/test_runner.py"
args = [
"--runner",
pw_automatic_test_runner,
"--test",
get_path_info("$target_out_dir:$_test_target_name", "abspath"),
]
stamp = true
}
}
}
# Defines a related collection of unit tests.
#
# pw_test_group targets output a JSON metadata file for the Pigweed test runner.
#
# Args:
# tests: List of pw_test targets for each of the tests in the group.
# group_deps: Optional pw_test_group targets on which this group depends.
template("pw_test_group") {
_group_target = target_name
_group_deps_metadata = []
if (defined(invoker.tests)) {
_deps = invoker.tests
} else {
_deps = []
}
if (defined(invoker.group_deps)) {
# If the group specified any other group dependencies, create a metadata
# entry for each of them indicating that they are another group and a group
# target to collect that metadata.
foreach(dep, invoker.group_deps) {
_group_deps_metadata += [
{
type = "dep"
group = get_path_info(dep, "abspath")
},
]
}
_deps += invoker.group_deps
}
_metadata_group_target = "${target_name}_pw_test_group_metadata"
group(_metadata_group_target) {
metadata = {
group_deps = _group_deps_metadata
self = [
{
type = "self"
name = get_path_info(":$_group_target", "abspath")
},
]
# Metadata from the group's own unit test targets is forwarded through
# the group dependencies group. This entry is listed as a "walk_key" in
# the generated file so that only test targets' metadata (not group
# targets) appear in the output.
if (defined(invoker.tests)) {
propagate_metadata_from = invoker.tests
}
}
deps = _deps
}
_test_group_deps = [ ":$_metadata_group_target" ]
generated_file(_group_target) {
outputs = [
"$target_out_dir/$target_name.testinfo.json",
]
data_keys = [
"group_deps",
"self",
"tests",
]
walk_keys = [ "propagate_metadata_from" ]
output_conversion = "json"
deps = _test_group_deps
}
# If automatic test running is enabled, create a *_run group that collects all
# of the individual *_run targets and groups.
if (pw_automatic_test_runner != "") {
group(_group_target + "_run") {
deps = [
":$_group_target",
]
foreach(_target, _deps) {
deps += [ "${_target}_run" ]
}
}
}
}