xref: /aosp_15_r20/external/bazel-skylib/tests/modules_test.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1*bcb5dc79SHONG Yifan# Copyright 2017 The Bazel Authors. All rights reserved.
2*bcb5dc79SHONG Yifan#
3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License");
4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License.
5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at
6*bcb5dc79SHONG Yifan#
7*bcb5dc79SHONG Yifan#    http://www.apache.org/licenses/LICENSE-2.0
8*bcb5dc79SHONG Yifan#
9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software
10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS,
11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and
13*bcb5dc79SHONG Yifan# limitations under the License.
14*bcb5dc79SHONG Yifan
15*bcb5dc79SHONG Yifan"""Test usage of modules.bzl."""
16*bcb5dc79SHONG Yifan
17*bcb5dc79SHONG Yifanload("//lib:modules.bzl", "modules")
18*bcb5dc79SHONG Yifanload("//rules:build_test.bzl", "build_test")
19*bcb5dc79SHONG Yifan
20*bcb5dc79SHONG Yifandef _repo_rule_impl(repository_ctx):
21*bcb5dc79SHONG Yifan    repository_ctx.file("WORKSPACE")
22*bcb5dc79SHONG Yifan    repository_ctx.file("BUILD", """exports_files(["hello"])""")
23*bcb5dc79SHONG Yifan    repository_ctx.file("hello", "Hello, Bzlmod!")
24*bcb5dc79SHONG Yifan
25*bcb5dc79SHONG Yifan_repo_rule = repository_rule(_repo_rule_impl)
26*bcb5dc79SHONG Yifan
27*bcb5dc79SHONG Yifandef _workspace_macro(register_toolchains = False):
28*bcb5dc79SHONG Yifan    _repo_rule(name = "foo")
29*bcb5dc79SHONG Yifan    _repo_rule(name = "bar")
30*bcb5dc79SHONG Yifan    if register_toolchains:
31*bcb5dc79SHONG Yifan        native.register_toolchains()
32*bcb5dc79SHONG Yifan
33*bcb5dc79SHONG Yifanas_extension_test_ext = modules.as_extension(
34*bcb5dc79SHONG Yifan    _workspace_macro,
35*bcb5dc79SHONG Yifan    doc = "Only used for testing modules.as_extension().",
36*bcb5dc79SHONG Yifan)
37*bcb5dc79SHONG Yifan
38*bcb5dc79SHONG Yifandef _use_all_repos_ext_impl(module_ctx):
39*bcb5dc79SHONG Yifan    _repo_rule(name = "baz")
40*bcb5dc79SHONG Yifan    _repo_rule(name = "qux")
41*bcb5dc79SHONG Yifan    return modules.use_all_repos(module_ctx)
42*bcb5dc79SHONG Yifan
43*bcb5dc79SHONG Yifanuse_all_repos_test_ext = module_extension(
44*bcb5dc79SHONG Yifan    _use_all_repos_ext_impl,
45*bcb5dc79SHONG Yifan    doc = "Only used for testing modules.use_all_repos().",
46*bcb5dc79SHONG Yifan)
47*bcb5dc79SHONG Yifan
48*bcb5dc79SHONG Yifan# buildifier: disable=unnamed-macro
49*bcb5dc79SHONG Yifandef modules_test_suite():
50*bcb5dc79SHONG Yifan    """Creates the tests for modules.bzl if Bzlmod is enabled."""
51*bcb5dc79SHONG Yifan
52*bcb5dc79SHONG Yifan    is_bzlmod_enabled = str(Label("//tests:module_tests.bzl")).startswith("@@")
53*bcb5dc79SHONG Yifan    if not is_bzlmod_enabled:
54*bcb5dc79SHONG Yifan        return
55*bcb5dc79SHONG Yifan
56*bcb5dc79SHONG Yifan    build_test(
57*bcb5dc79SHONG Yifan        name = "modules_as_extension_test",
58*bcb5dc79SHONG Yifan        targets = [
59*bcb5dc79SHONG Yifan            "@foo//:hello",
60*bcb5dc79SHONG Yifan            "@bar//:hello",
61*bcb5dc79SHONG Yifan        ],
62*bcb5dc79SHONG Yifan    )
63*bcb5dc79SHONG Yifan
64*bcb5dc79SHONG Yifan    build_test(
65*bcb5dc79SHONG Yifan        name = "modules_use_all_repos_test",
66*bcb5dc79SHONG Yifan        targets = [
67*bcb5dc79SHONG Yifan            "@baz//:hello",
68*bcb5dc79SHONG Yifan            "@qux//:hello",
69*bcb5dc79SHONG Yifan        ],
70*bcb5dc79SHONG Yifan    )
71