xref: /aosp_15_r20/external/bazel-skylib/tests/subpackages_tests.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1# Copyright 2022 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"""Unit tests for subpackages.bzl."""
16
17load("//lib:subpackages.bzl", "subpackages")
18load("//lib:unittest.bzl", "loadingtest")
19
20def _all_test(env):
21    """Unit tests for subpackages.all."""
22
23    all_pkgs = [
24        "bzl_library",
25        "common_settings",
26        "copy_directory",
27        "copy_file",
28        "diff_test",
29        "directory",
30        "expand_template",
31        "select_file",
32        "write_file",
33    ]
34
35    # Not all pkgs exist in all test environments.
36    if subpackages.exists("run_binary"):
37        all_pkgs.append("run_binary")
38
39    if subpackages.exists("native_binary"):
40        all_pkgs.append("native_binary")
41
42    # These exist in all cases
43    filtered_pkgs = [
44        "bzl_library",
45        "common_settings",
46        "copy_directory",
47        "copy_file",
48        "directory",
49        "expand_template",
50        "select_file",
51        "write_file",
52    ]
53
54    # subpackages is always in sorted order:
55    all_pkgs = sorted(all_pkgs)
56
57    # test defaults
58    loadingtest.equals(
59        env,
60        "all",
61        ["//tests/" + pkg for pkg in all_pkgs],
62        subpackages.all(),
63    )
64
65    # test non-fully-qualified output
66    loadingtest.equals(
67        env,
68        "all_not_fully_qualified",
69        all_pkgs,
70        subpackages.all(fully_qualified = False),
71    )
72
73    # test exclusion
74    loadingtest.equals(
75        env,
76        "all_w_exclude",
77        filtered_pkgs,
78        subpackages.all(exclude = ["diff_test", "run_binary", "native_binary"], fully_qualified = False),
79    )
80
81def _exists_test(env):
82    """Unit tests for subpackages.exists."""
83    loadingtest.equals(env, "exists_yes", True, subpackages.exists("copy_file"))
84    loadingtest.equals(env, "exists_no", False, subpackages.exists("never_existed"))
85
86def subpackages_test_suite():
87    """Creates the test targets and test suite for subpackages.bzl tests."""
88
89    if subpackages.supported():
90        env = loadingtest.make("subpackages")
91        _all_test(env)
92        _exists_test(env)
93