xref: /aosp_15_r20/external/bazelbuild-rules_testing/e2e/bzlmod/tests.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1*d6050574SRomain Jobredeaux# Copyright 2023 The Bazel Authors. All rights reserved.
2*d6050574SRomain Jobredeaux#
3*d6050574SRomain Jobredeaux# Licensed under the Apache License, Version 2.0 (the "License");
4*d6050574SRomain Jobredeaux# you may not use this file except in compliance with the License.
5*d6050574SRomain Jobredeaux# You may obtain a copy of the License at
6*d6050574SRomain Jobredeaux#
7*d6050574SRomain Jobredeaux#     http://www.apache.org/licenses/LICENSE-2.0
8*d6050574SRomain Jobredeaux#
9*d6050574SRomain Jobredeaux# Unless required by applicable law or agreed to in writing, software
10*d6050574SRomain Jobredeaux# distributed under the License is distributed on an "AS IS" BASIS,
11*d6050574SRomain Jobredeaux# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d6050574SRomain Jobredeaux# See the License for the specific language governing permissions and
13*d6050574SRomain Jobredeaux# limitations under the License.
14*d6050574SRomain Jobredeaux
15*d6050574SRomain Jobredeaux"""Tests for basic bzlmod functionality."""
16*d6050574SRomain Jobredeaux
17*d6050574SRomain Jobredeauxload("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
18*d6050574SRomain Jobredeauxload("@rules_testing//lib:util.bzl", "util")
19*d6050574SRomain Jobredeaux
20*d6050574SRomain Jobredeauxdef _simple_test(name):
21*d6050574SRomain Jobredeaux    util.helper_target(
22*d6050574SRomain Jobredeaux        native.filegroup,
23*d6050574SRomain Jobredeaux        name = name + "_subject",
24*d6050574SRomain Jobredeaux        srcs = ["src.txt"],
25*d6050574SRomain Jobredeaux        data = ["data.txt"],
26*d6050574SRomain Jobredeaux    )
27*d6050574SRomain Jobredeaux    analysis_test(
28*d6050574SRomain Jobredeaux        name = name,
29*d6050574SRomain Jobredeaux        impl = _simple_test_impl,
30*d6050574SRomain Jobredeaux        target = name + "_subject",
31*d6050574SRomain Jobredeaux    )
32*d6050574SRomain Jobredeaux
33*d6050574SRomain Jobredeauxdef _simple_test_impl(env, target):
34*d6050574SRomain Jobredeaux    subject = env.expect.that_target(target)
35*d6050574SRomain Jobredeaux    subject.default_outputs().contains_exactly(["src.txt"])
36*d6050574SRomain Jobredeaux    subject.runfiles().contains_exactly(["{workspace}/data.txt"])
37*d6050574SRomain Jobredeaux
38*d6050574SRomain Jobredeauxdef bzlmod_test_suite(name):
39*d6050574SRomain Jobredeaux    test_suite(name = name, tests = [
40*d6050574SRomain Jobredeaux        _simple_test,
41*d6050574SRomain Jobredeaux        _trigger_toolchains_test,
42*d6050574SRomain Jobredeaux    ])
43*d6050574SRomain Jobredeaux
44*d6050574SRomain Jobredeauxdef _needs_toolchain_impl(ctx):
45*d6050574SRomain Jobredeaux    # We just need to trigger toolchain resolution, we don't
46*d6050574SRomain Jobredeaux    # care about the result.
47*d6050574SRomain Jobredeaux    _ = ctx.toolchains["//:fake"]  # @unused
48*d6050574SRomain Jobredeaux
49*d6050574SRomain Jobredeaux_needs_toolchain = rule(
50*d6050574SRomain Jobredeaux    implementation = _needs_toolchain_impl,
51*d6050574SRomain Jobredeaux    toolchains = [config_common.toolchain_type("//:fake", mandatory = False)],
52*d6050574SRomain Jobredeaux)
53*d6050574SRomain Jobredeaux
54*d6050574SRomain Jobredeauxdef _trigger_toolchains_test_impl(env, target):
55*d6050574SRomain Jobredeaux    # Building is sufficient evidence of success
56*d6050574SRomain Jobredeaux    _ = env, target  # @unused
57*d6050574SRomain Jobredeaux
58*d6050574SRomain Jobredeaux# A regression test for https://github.com/bazelbuild/rules_testing/issues/33
59*d6050574SRomain Jobredeauxdef _trigger_toolchains_test(name):
60*d6050574SRomain Jobredeaux    util.helper_target(
61*d6050574SRomain Jobredeaux        _needs_toolchain,
62*d6050574SRomain Jobredeaux        name = name + "_subject",
63*d6050574SRomain Jobredeaux    )
64*d6050574SRomain Jobredeaux    analysis_test(
65*d6050574SRomain Jobredeaux        name = name,
66*d6050574SRomain Jobredeaux        impl = _trigger_toolchains_test_impl,
67*d6050574SRomain Jobredeaux        target = name + "_subject",
68*d6050574SRomain Jobredeaux    )
69