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"""Helper functions for testing directory rules.""" 16 17load("@rules_testing//lib:analysis_test.bzl", "analysis_test") 18load("@rules_testing//lib:truth.bzl", "subjects") 19load("@rules_testing//lib:util.bzl", "util") 20 21_depset_as_list_subject = lambda value, *, meta: subjects.collection( 22 value.to_list(), 23 meta = meta, 24) 25 26directory_info_subject = lambda value, *, meta: subjects.struct( 27 value, 28 meta = meta, 29 attrs = dict( 30 entries = subjects.dict, 31 transitive_files = _depset_as_list_subject, 32 path = subjects.str, 33 human_readable = subjects.str, 34 ), 35) 36 37def failure_matching(matcher): 38 def test(env, target): 39 env.expect.that_target(target).failures().contains_exactly_predicates([ 40 matcher, 41 ]) 42 43 return test 44 45def directory_subject(env, directory_info): 46 return env.expect.that_value( 47 value = directory_info, 48 expr = "DirectoryInfo(%r)" % directory_info.path, 49 factory = directory_info_subject, 50 ) 51 52def failure_test(*, name, impl, rule, **kwargs): 53 subject_name = "_%s_subject" % name 54 util.helper_target( 55 rule, 56 name = subject_name, 57 **kwargs 58 ) 59 60 analysis_test( 61 name = name, 62 expect_failure = True, 63 impl = impl, 64 target = subject_name, 65 ) 66