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"""CcInfo testing subject.""" 15 16load("@rules_testing//lib:truth.bzl", "subjects") 17 18def cc_info_subject(info, *, meta): 19 """Creates a new `CcInfoSubject` for a CcInfo provider instance. 20 21 Args: 22 info: The CcInfo object. 23 meta: ExpectMeta object. 24 25 Returns: 26 A `CcInfoSubject` struct. 27 """ 28 29 # buildifier: disable=uninitialized 30 public = struct( 31 # go/keep-sorted start 32 compilation_context = lambda *a, **k: _cc_info_subject_compilation_context(self, *a, **k), 33 # go/keep-sorted end 34 ) 35 self = struct( 36 actual = info, 37 meta = meta, 38 ) 39 return public 40 41def _cc_info_subject_compilation_context(self): 42 """Returns the CcInfo.compilation_context as a subject. 43 44 Args: 45 self: implicitly added. 46 47 Returns: 48 [`CompilationContext`] instance. 49 """ 50 return _compilation_context_subject_new( 51 self.actual.compilation_context, 52 meta = self.meta.derive("compilation_context()"), 53 ) 54 55def _compilation_context_subject_new(info, *, meta): 56 """Creates a CompilationContextSubject. 57 58 Args: 59 info: ([`CompilationContext`]) object instance. 60 meta: rules_testing `ExpectMeta` instance. 61 62 Returns: 63 [`CompilationContextSubject`] object. 64 """ 65 66 # buildifier: disable=uninitialized 67 public = struct( 68 # go/keep-sorted start 69 direct_headers = lambda *a, **k: _compilation_context_subject_direct_headers(self, *a, **k), 70 direct_public_headers = lambda *a, **k: _compilation_context_subject_direct_public_headers(self, *a, **k), 71 system_includes = lambda *a, **k: _compilation_context_subject_system_includes(self, *a, **k), 72 # go/keep-sorted end 73 ) 74 self = struct( 75 actual = info, 76 meta = meta, 77 ) 78 return public 79 80def _compilation_context_subject_direct_headers(self): 81 """Returns the direct headers as a subjecct. 82 83 Args: 84 self: implicitly added 85 86 Returns: 87 [`CollectionSubject`] of `File` objects of the direct headers. 88 """ 89 return subjects.collection( 90 self.actual.direct_headers, 91 meta = self.meta.derive("direct_headers()"), 92 container_name = "direct_headers", 93 element_plural_name = "header files", 94 ) 95 96def _compilation_context_subject_direct_public_headers(self): 97 """Returns the direct public headers as a subjecct. 98 99 Args: 100 self: implicitly added 101 102 Returns: 103 [`CollectionSubject`] of `File` objects of the direct headers. 104 """ 105 return subjects.collection( 106 self.actual.direct_public_headers, 107 meta = self.meta.derive("direct_public_headers()"), 108 container_name = "direct_public_headers", 109 element_plural_name = "public header files", 110 ) 111 112def _compilation_context_subject_system_includes(self): 113 """Returns the system include directories as a subject. 114 115 NOTE: The system includes are the `cc_library.includes` attribute. 116 117 Args: 118 self: implicitly added 119 120 Returns: 121 [`CollectionSubject`] of [`str`] 122 """ 123 return subjects.collection( 124 self.actual.system_includes.to_list(), 125 meta = self.meta.derive("includes()"), 126 container_name = "includes", 127 element_plural_name = "include paths", 128 ) 129