xref: /aosp_15_r20/external/bazelbuild-rules_license/tools/test_helpers.bzl (revision f578df4fd057ffe2023728444759535685631548)
1# Copyright 2020 Google LLC
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# https://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"""Compare rule output to a golden version."""
16
17def golden_test(
18        name,
19        golden,
20        subject):
21    """Check that output from a rule matches the expected output.
22
23    Args:
24      name: test name
25      golden: expected content of subect
26      subject: build target
27    """
28    native.sh_test(
29        name = name,
30        size = "medium",
31        srcs = ["@rules_license//tools:diff_test.sh"],
32        args = [
33            "$(location %s)" % golden,
34            "$(location %s)" % subject,
35        ],
36        data = [
37            subject,
38            golden,
39        ],
40    )
41
42def golden_cmd_test(
43        name,
44        cmd,
45        golden,  # Required
46        toolchains = [],
47        tools = None,
48        exec_tools = None,
49        srcs = [],  # Optional
50        **kwargs):  # Rest
51    """Compares cmd output to golden output, passes if they are identical.
52
53    Args:
54      name: Name of the build rule.
55      cmd: The command to run to generate output.
56      golden: The golden file to be compared.
57      toolchains: List of toolchains needed to run the command, passed to genrule.
58      tools: List of tools needed to run the command, passed to genrule.
59      exec_tools: List of tools needed to run the command, passed to genrule.
60      srcs: List of sources needed as input to the command, passed to genrule.
61      **kwargs: Any additional parameters for the generated golden_test.
62    """
63    actual = name + ".output"
64
65    # There are some cases where tools are provided and exec_tools are provided.
66    # Specifying both in the same genrule, confuses the host vs exec rules,
67    # which prevents python3 from execution.
68    if tools and exec_tools:
69        fail("Only set one: tools or exec_tools.  " +
70             "Setting both confuses python execution mode (host vs exec).")
71    native.genrule(
72        name = name + "_output",
73        srcs = srcs,
74        outs = [actual],
75        cmd = cmd + " > '$@'",  # Redirect to collect output
76        toolchains = toolchains,
77        tools = tools,
78        exec_tools = exec_tools,
79        testonly = True,
80    )
81
82    golden_test(
83        name = name,
84        subject = actual,
85        golden = golden,
86        **kwargs
87    )
88