xref: /aosp_15_r20/external/skia/tools/testrunners/unit/unit_tests.bzl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1"""This module contains macros to generate C++ unit test targets."""
2
3load("@skia_user_config//:copts.bzl", "DEFAULT_COPTS")
4load("@skia_user_config//:linkopts.bzl", "DEFAULT_LINKOPTS")
5
6def unit_tests(
7        name,
8        tests,
9        deps,
10        resources = [],
11        extra_srcs = [],
12        tags = None):
13    """This macro will create one cc_test rule for each file in tests.
14
15    These tests are configured to use the BazelUnitTestRunner and run all tests
16    (e.g. those defined with DEF_TEST) in the file.
17
18    Args:
19        name: The name of the test_suite that groups these tests together.
20        tests: A list of strings, corresponding to C++ files with one or more DEF_TEST (see Test.h).
21        deps: A list of labels corresponding to cc_library targets which this test needs to work.
22            This typically includes some Skia modules and maybe some test utils.
23        resources: A label corresponding to a file_group target that has any skia resource files
24            (e.g. images, fonts) needed to run these tests. Resources change infrequently, so
25            it's not super important that this be a precise list.
26        extra_srcs: Any extra files (e.g. headers) that are needed to build these tests. This is
27            a more convenient way to include a few extra files without needing to create a
28            distinct test cc_library.
29        tags: Added to all the generated test targets
30    """
31    test_targets = []
32    if not tags:
33        tags = []
34    for filename in tests:
35        new_target = name + "_" + filename[:-4]  # trim .cpp
36        test_targets.append(new_target)
37        native.cc_test(
38            name = new_target,
39            copts = DEFAULT_COPTS,
40            linkopts = DEFAULT_LINKOPTS,
41            size = "small",
42            srcs = [filename] + extra_srcs,
43            deps = deps + ["//tools/testrunners/unit:testrunner"],
44            data = resources,
45            tags = tags,
46        )
47
48    # https://bazel.build/reference/be/general#test_suite
49    native.test_suite(
50        name = name,
51        tests = test_targets,
52    )
53