xref: /aosp_15_r20/external/bazelbuild-rules_testing/e2e/bzlmod/tests.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1# Copyright 2023 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"""Tests for basic bzlmod functionality."""
16
17load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
18load("@rules_testing//lib:util.bzl", "util")
19
20def _simple_test(name):
21    util.helper_target(
22        native.filegroup,
23        name = name + "_subject",
24        srcs = ["src.txt"],
25        data = ["data.txt"],
26    )
27    analysis_test(
28        name = name,
29        impl = _simple_test_impl,
30        target = name + "_subject",
31    )
32
33def _simple_test_impl(env, target):
34    subject = env.expect.that_target(target)
35    subject.default_outputs().contains_exactly(["src.txt"])
36    subject.runfiles().contains_exactly(["{workspace}/data.txt"])
37
38def bzlmod_test_suite(name):
39    test_suite(name = name, tests = [
40        _simple_test,
41        _trigger_toolchains_test,
42    ])
43
44def _needs_toolchain_impl(ctx):
45    # We just need to trigger toolchain resolution, we don't
46    # care about the result.
47    _ = ctx.toolchains["//:fake"]  # @unused
48
49_needs_toolchain = rule(
50    implementation = _needs_toolchain_impl,
51    toolchains = [config_common.toolchain_type("//:fake", mandatory = False)],
52)
53
54def _trigger_toolchains_test_impl(env, target):
55    # Building is sufficient evidence of success
56    _ = env, target  # @unused
57
58# A regression test for https://github.com/bazelbuild/rules_testing/issues/33
59def _trigger_toolchains_test(name):
60    util.helper_target(
61        _needs_toolchain,
62        name = name + "_subject",
63    )
64    analysis_test(
65        name = name,
66        impl = _trigger_toolchains_test_impl,
67        target = name + "_subject",
68    )
69