blob: 799193d735c20a112ee886cf3935d20b5478cea8 [file]
# Copyright 2026 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.
"""Defines the zephyr_cc_unit_test rule."""
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_python//python:py_test.bzl", "py_test")
load("@zephyr//:cc.bzl", "zephyr_app", "zephyr_cc_library")
def zephyr_cc_test(**kw_args):
"""Creates a Zephyr test library for use with a test runner."""
zephyr_cc_library(
deps = kw_args.pop("deps", []) + [
"@pigweed//pw_unit_test",
],
testonly = True,
**kw_args
)
def ztest(
name,
deps,
tags = None,
visibility = None,
stop_at = 10,
success_signature = "PROJECT EXECUTION SUCCESSFUL",
**kwargs):
"""Creates a Zephyr ZTest target runnable with `bazel test`.
This macro compiles the target application binary under the hood and wraps
it in an automated simulator runner validating the ZTest suite output.
"""
tags = tags or []
bin_name = name + "_bin"
# 1. Compile the executable Zephyr application (forwarding compilation kwargs)
zephyr_app(
name = bin_name,
deps = deps,
testonly = True,
visibility = ["//visibility:private"],
**kwargs
)
# 2. Copy the runner script to the local package to satisfy py_test constraints
runner_script = name + "_runner.py"
copy_file(
name = name + "_runner_copy",
src = "@zephyr-bazel//scripts/build:ztest_runner.py",
out = runner_script,
testonly = True,
)
# 3. Simulator test target (sandboxed, cached)
py_test(
name = name + "_sim",
srcs = [runner_script],
main = runner_script,
data = [
":" + bin_name,
":" + bin_name + "_runners_yaml",
],
deps = [
"@zephyr-bazel//scripts/build:ztest_runner",
],
args = [
"--binary",
"$(rlocationpath :" + bin_name + ")",
"--runners-yaml",
"$(rlocationpath :" + bin_name + "_runners_yaml)",
"--stop-at",
str(stop_at),
"--success-signature",
'"{}"'.format(success_signature),
],
tags = tags,
visibility = visibility,
target_compatible_with = ["@zephyr//boards:sim"],
)
# 4. Hardware test target (local, no-cache, exclusive)
py_test(
name = name + "_hw",
srcs = [runner_script],
main = runner_script,
data = [
":" + bin_name,
":" + bin_name + "_runners_yaml",
],
deps = [
"@zephyr-bazel//scripts/build:ztest_runner",
],
args = [
"--binary",
"$(rlocationpath :" + bin_name + ")",
"--runners-yaml",
"$(rlocationpath :" + bin_name + "_runners_yaml)",
"--stop-at",
str(stop_at),
"--success-signature",
'"{}"'.format(success_signature),
],
tags = tags + ["local", "exclusive"],
visibility = visibility,
target_compatible_with = ["@zephyr//boards:hw"],
)
# 5. Unified test suite
native.test_suite(
name = name,
tests = [
":" + name + "_sim",
":" + name + "_hw",
],
visibility = visibility,
)