xref: /aosp_15_r20/external/bazelbuild-rules_android/test/utils/unittest.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2018 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Bazel lib that provides test helpers for testing."""
16
17load(":file.bzl", _file = "file")
18load(
19    "@bazel_skylib//lib:unittest.bzl",
20    _analysistest = "analysistest",
21    _unittest = "unittest",
22)
23
24TestInfo = provider(
25    doc = "Provides a test a suggested set of attributes.",
26    fields = {
27        "name": "The name of the test.",
28        "prefix": "The prefix used to isolate artifact and target names.",
29    },
30)
31
32def _prefix(prefix, name):
33    """Prepends the given prefix to the given name."""
34    return "%s-%s" % (prefix, name)
35
36def _prefix_from_test_info(test_info, name):
37    """Prepends the prefix of a TestInfo to the given name."""
38    return _prefix(test_info.prefix, name)
39
40def _test_suite(
41        name = None,
42        test_scenarios = None):
43    """Creates a test suite containing the list of test targets.
44
45    Args:
46      name: Name of the test suite, also used as part of a prefix for naming.
47      test_scenarios: A list of methods, that setup and the test. Each scenario
48        method should accept a TestInfo provider.
49    """
50    test_targets = []
51    for scenario_name, make_scenario in test_scenarios.items():
52        test_prefix = _prefix(name, scenario_name)
53        test_info = TestInfo(
54            prefix = test_prefix,
55            name = test_prefix + "_test",
56        )
57        make_scenario(test_info)
58        test_targets.append(test_info.name)
59
60    native.test_suite(
61        name = name,
62        tests = test_targets,
63    )
64
65def _fake_java_library(name):
66    class_name = "".join(
67        [part.title() for part in name.replace("-", "_").split("_")],
68    )
69    native.java_library(
70        name = name,
71        srcs = [_file.create(
72            class_name + ".java",
73            contents = """@SuppressWarnings("DefaultPackage")
74class %s{}""" % class_name,
75        )],
76    )
77
78def _fake_jar(name):
79    if not name.endswith(".jar"):
80        fail("fake_jar method requires name to end with '.jar'")
81    _fake_java_library(name[:-4])
82    return name
83
84def _fake_executable(name):
85    return _file.create(name, contents = "echo %s" % name, executable = True)
86
87def _analysis_test_error(message, *args):
88    return [
89        AnalysisTestResultInfo(
90            success = False,
91            message = message % args,
92        ),
93    ]
94
95analysistest = _analysistest
96
97unittest = struct(
98    # Forward through unittest methods through the current unittest.
99    analysis_test_error = _analysis_test_error,
100    begin = _unittest.begin,
101    end = _unittest.end,
102    fake_executable = _fake_executable,
103    fake_jar = _fake_jar,
104    fake_java_library = _fake_java_library,
105    make = _unittest.make,
106    prefix = _prefix_from_test_info,
107    test_suite = _test_suite,
108)
109