1# Copyright 2023 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"""Utilities for testing rules_testing code.""" 16 17# buildifier: disable=bzl-visibility 18load("//lib/private:expect_meta.bzl", "ExpectMeta") 19load("//lib:truth.bzl", "matching") 20 21def _fake_meta(real_env): 22 """Create a fake ExpectMeta object for testing. 23 24 The fake ExpectMeta object copies a real ExpectMeta object, except: 25 * Failures are only recorded and don't cause a failure in `real_env`. 26 * `failures` attribute is added; this is a list of failures seen. 27 * `reset` attribute is added; this clears the failures list. 28 29 Args: 30 real_env: A real env object from the rules_testing framework. 31 32 Returns: 33 struct, a fake ExpectMeta object. 34 """ 35 failures = [] 36 fake_env = struct( 37 ctx = real_env.ctx, 38 fail = lambda msg: failures.append(msg), 39 failures = failures, 40 ) 41 meta_impl = ExpectMeta.new(fake_env) 42 meta_impl_kwargs = { 43 attr: getattr(meta_impl, attr) 44 for attr in dir(meta_impl) 45 if attr not in ("to_json", "to_proto") 46 } 47 fake_meta = struct( 48 failures = failures, 49 reset = lambda: failures.clear(), 50 **meta_impl_kwargs 51 ) 52 return fake_meta 53 54def _expect_no_failures(env, fake_meta, case): 55 """Check that a fake meta object had no failures. 56 57 NOTE: This clears the list of failures after checking. This is done 58 so that an earlier failure is only reported once. 59 60 Args: 61 env: Real `Expect` object to perform asserts. 62 fake_meta: A fake meta object that had failures recorded. 63 case: str, a description of the case that was tested. 64 """ 65 env.expect.that_collection( 66 fake_meta.failures, 67 expr = case, 68 ).contains_exactly([]) 69 fake_meta.reset() 70 71def _expect_failures(env, fake_meta, case, *errors): 72 """Check that a fake meta object has matching error strings. 73 74 NOTE: This clears the list of failures after checking. This is done 75 so that an earlier failure is only reported once. 76 77 Args: 78 env: Real `Expect` object to perform asserts. 79 fake_meta: A fake meta object that had failures recorded. 80 case: str, a description of the case that was tested. 81 *errors: list of strings. These are patterns to match, as supported 82 by `matching.str_matches` (e.g. `*`-style patterns) 83 """ 84 env.expect.that_collection( 85 fake_meta.failures, 86 expr = case, 87 ).contains_at_least_predicates( 88 [matching.str_matches(e) for e in errors], 89 ) 90 fake_meta.reset() 91 92test_util = struct( 93 fake_meta = _fake_meta, 94 expect_no_failures = _expect_no_failures, 95 expect_failures = _expect_failures, 96) 97