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