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"""Unit tests for subdirectory rules.""" 16 17load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo") 18load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory") 19load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") 20load("@rules_testing//lib:truth.bzl", "matching") 21load(":utils.bzl", "failure_matching", "failure_test") 22 23_NONEXISTENT_SUBDIRECTORY_ERR = """directory:root/testdata does not contain an entry named "nonexistent". 24Instead, it contains the following entries: 25f1 26subdir 27""" 28 29def _subdirectory_test(name): 30 testdata_name = "_%s_dir" % name 31 subdir_name = "_%s_subdir" % name 32 33 subdirectory( 34 name = testdata_name, 35 parent = ":root", 36 path = "testdata", 37 ) 38 39 subdirectory( 40 name = subdir_name, 41 parent = ":root", 42 path = "testdata/subdir", 43 ) 44 45 analysis_test( 46 name = name, 47 impl = _subdirectory_test_impl, 48 targets = { 49 "root": ":root", 50 "testdata": testdata_name, 51 "subdir": subdir_name, 52 "f1": ":f1_filegroup", 53 "f2": ":f2_filegroup", 54 }, 55 ) 56 57def _subdirectory_test_impl(env, targets): 58 f1 = targets.f1.files.to_list()[0] 59 f2 = targets.f2.files.to_list()[0] 60 61 root = targets.root[DirectoryInfo] 62 want_dir = root.entries["testdata"] 63 want_subdir = want_dir.entries["subdir"] 64 65 # Use that_str because it supports equality checks. They're not strings. 66 env.expect.that_str(targets.testdata[DirectoryInfo]).equals(want_dir) 67 env.expect.that_str(targets.subdir[DirectoryInfo]).equals(want_subdir) 68 69 env.expect.that_collection( 70 targets.testdata.files.to_list(), 71 ).contains_exactly([f1, f2]) 72 env.expect.that_collection( 73 targets.subdir.files.to_list(), 74 ).contains_exactly([f2]) 75 76def _nonexistent_subdirectory_test(name): 77 failure_test( 78 name = name, 79 impl = failure_matching(matching.contains(_NONEXISTENT_SUBDIRECTORY_ERR)), 80 rule = subdirectory, 81 parent = ":root", 82 path = "testdata/nonexistent", 83 ) 84 85def _subdirectory_of_file_test(name): 86 failure_test( 87 name = name, 88 impl = failure_matching(matching.contains("testdata/f1 to have type Directory, but got File")), 89 rule = subdirectory, 90 parent = ":root", 91 path = "testdata/f1/foo", 92 ) 93 94def _subdirectory_as_file_test(name): 95 failure_test( 96 name = name, 97 impl = failure_matching(matching.contains("testdata/f1 to have type Directory, but got File")), 98 rule = subdirectory, 99 parent = ":root", 100 path = "testdata/f1", 101 ) 102 103# buildifier: disable=function-docstring 104def subdirectory_test_suite(name): 105 test_suite( 106 name = name, 107 tests = [ 108 _subdirectory_test, 109 _nonexistent_subdirectory_test, 110 _subdirectory_as_file_test, 111 _subdirectory_of_file_test, 112 ], 113 ) 114