xref: /aosp_15_r20/external/bazel-skylib/tests/modules_test.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1# Copyright 2017 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"""Test usage of modules.bzl."""
16
17load("//lib:modules.bzl", "modules")
18load("//rules:build_test.bzl", "build_test")
19
20def _repo_rule_impl(repository_ctx):
21    repository_ctx.file("WORKSPACE")
22    repository_ctx.file("BUILD", """exports_files(["hello"])""")
23    repository_ctx.file("hello", "Hello, Bzlmod!")
24
25_repo_rule = repository_rule(_repo_rule_impl)
26
27def _workspace_macro(register_toolchains = False):
28    _repo_rule(name = "foo")
29    _repo_rule(name = "bar")
30    if register_toolchains:
31        native.register_toolchains()
32
33as_extension_test_ext = modules.as_extension(
34    _workspace_macro,
35    doc = "Only used for testing modules.as_extension().",
36)
37
38def _use_all_repos_ext_impl(module_ctx):
39    _repo_rule(name = "baz")
40    _repo_rule(name = "qux")
41    return modules.use_all_repos(module_ctx)
42
43use_all_repos_test_ext = module_extension(
44    _use_all_repos_ext_impl,
45    doc = "Only used for testing modules.use_all_repos().",
46)
47
48# buildifier: disable=unnamed-macro
49def modules_test_suite():
50    """Creates the tests for modules.bzl if Bzlmod is enabled."""
51
52    is_bzlmod_enabled = str(Label("//tests:module_tests.bzl")).startswith("@@")
53    if not is_bzlmod_enabled:
54        return
55
56    build_test(
57        name = "modules_as_extension_test",
58        targets = [
59            "@foo//:hello",
60            "@bar//:hello",
61        ],
62    )
63
64    build_test(
65        name = "modules_use_all_repos_test",
66        targets = [
67            "@baz//:hello",
68            "@qux//:hello",
69        ],
70    )
71