xref: /aosp_15_r20/external/bazel-skylib/tests/directory/external_directory_tests.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1# Copyright 2024 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"""Generates tests for the directory rules from outside the repository."""
16
17_ROOT_BUILD_FILE_CONTENTS = """
18load("@bazel_skylib//rules/directory:directory.bzl", "directory")
19
20directory(
21    name = "root",
22    srcs = ["BUILD"],
23)
24"""
25
26def _external_directory_tests_impl(repo_ctx):
27    for f in repo_ctx.attr.files:
28        repo_ctx.symlink(repo_ctx.path(f), f.package + "/" + f.name)
29
30    repo_ctx.file("BUILD", _ROOT_BUILD_FILE_CONTENTS)
31
32# Directory paths work differently while inside and outside the repository.
33# To properly test this, we copy all our test code to an external
34# repository.
35external_directory_tests = repository_rule(
36    implementation = _external_directory_tests_impl,
37    attrs = {
38        "files": attr.label_list(default = [
39            "//tests/directory:BUILD",
40            "//tests/directory:directory_test.bzl",
41            "//tests/directory:glob_test.bzl",
42            "//tests/directory:subdirectory_test.bzl",
43            "//tests/directory:testdata/f1",
44            "//tests/directory:testdata/subdir/f2",
45            "//tests/directory:utils.bzl",
46        ]),
47    },
48)
49
50def _external_directory_tests_ext_impl(_module_ctx):
51    external_directory_tests(name = "external_directory_tests")
52
53# use_repo_rule would be preferred, but it isn't supported in bazel 6.
54external_directory_tests_ext = module_extension(
55    implementation = _external_directory_tests_ext_impl,
56)
57