• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2023 The Android Open Source Project
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"""
16This rule runs a test during the build. It's intended for use in the bazel sandwich.
17It should mimic bazel's TestRunner action (like run the tests with
18@bazel_tools//tools/test:test_setup), but currently it just does the bare minimum to get skylib's
19diff_test to work.
20"""
21
22def _run_test_in_build_impl(ctx):
23    out = ctx.actions.declare_file(ctx.attr.name + "_out.xml")
24    ctx.actions.run_shell(
25        outputs = [out],
26        command = "{test} && touch {stampfile}".format(
27            test = ctx.attr.test[DefaultInfo].files_to_run.executable.path,
28            stampfile = out.path,
29        ),
30        inputs = ctx.attr.test[DefaultInfo].default_runfiles.files,
31        env = {
32            "TEST_WORKSPACE": ".",
33            "TEST_SRCDIR": ctx.attr.test[DefaultInfo].default_runfiles.files.to_list()[0].root.path,
34        },
35    )
36
37    return DefaultInfo(files = depset([out]))
38
39_run_test_in_build = rule(
40    implementation = _run_test_in_build_impl,
41    attrs = {
42        # TODO: Currently can't use executable = true / config = exec because we may want to build
43        # things for device, and then diff them. We need to add a transition from the diff_test back
44        # to device
45        "test": attr.label(),
46    },
47)
48
49def run_test_in_build(**kwargs):
50    _run_test_in_build(testonly = True, **kwargs)
51