blob: 7893f8a0ac0719aab53232de06e9f9546771bd81 [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_unit_test_create_run_targets variable is set to true, 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_unit_test_create_run_targets) {
# When the run targets arg 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/test_runner.py"
args = [ 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") {
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.
_group_deps = []
foreach(dep, invoker.group_deps) {
_group_deps += [
{
type = "dep"
group = get_path_info(dep, "abspath")
},
]
}
_metadata_group_target = "${target_name}_pw_test_group_metadata"
group(_metadata_group_target) {
metadata = {
group_deps = _group_deps
# 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.
propagate_metadata_from = invoker.tests
}
deps = invoker.tests + invoker.group_deps
}
_test_group_deps = [ ":$_metadata_group_target" ]
} else {
_test_group_deps = invoker.tests
}
generated_file(target_name) {
outputs = [
"$target_out_dir/$target_name.utmeta.json",
]
data_keys = [
"group_deps",
"tests",
]
walk_keys = [ "propagate_metadata_from" ]
output_conversion = "json"
deps = _test_group_deps
}
}