diff options
author | Richard Levasseur <rlevasseur@google.com> | 2023-07-11 11:23:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 18:23:10 +0000 |
commit | 02b521fce3c7b36b05813aa986d72777cc3ee328 (patch) | |
tree | 04ab4f5e2bbf7ec809d78fab8434b7cc14a41d60 | |
parent | 95ad6ccbdbfd911d14405e4e597c5fef79146ee1 (diff) | |
download | bazelbuild-rules_python-02b521fce3c7b36b05813aa986d72777cc3ee328.tar.gz |
cleanup(tests): Use new APIs in rules_testing 0.4.0 (#1307)upstream/0.24.0
* Use public APIs for DictSubject and StrSubject.
* Use rules_testing's StructSubject instead of our own.
Work towards 1297
-rw-r--r-- | tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl | 16 | ||||
-rw-r--r-- | tests/py_cc_toolchain_info_subject.bzl | 17 | ||||
-rw-r--r-- | tests/struct_subject.bzl | 50 |
3 files changed, 13 insertions, 70 deletions
diff --git a/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl b/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl index 09bd646..609518d 100644 --- a/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl +++ b/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl @@ -44,15 +44,10 @@ def _py_cc_toolchain_test_impl(env, target): ) toolchain.python_version().equals("3.999") - toolchain_headers = toolchain.headers() - toolchain_headers.providers_map().keys().contains_exactly(["CcInfo", "DefaultInfo"]) + headers_providers = toolchain.headers().providers_map() + headers_providers.keys().contains_exactly(["CcInfo", "DefaultInfo"]) - cc_info = cc_info_subject( - # TODO: Use DictSubject.get once available, - # https://github.com/bazelbuild/rules_testing/issues/51 - toolchain_headers.actual.providers_map["CcInfo"], - meta = env.expect.meta.derive(expr = "cc_info"), - ) + cc_info = headers_providers.get("CcInfo", factory = cc_info_subject) compilation_context = cc_info.compilation_context() compilation_context.direct_headers().contains_exactly([ @@ -68,8 +63,11 @@ def _py_cc_toolchain_test_impl(env, target): matching.str_matches("*/fake_include"), ]) + # TODO: Once subjects.default_info is available, do + # default_info = headers_providers.get("DefaultInfo", factory=subjects.default_info) + # https://github.com/bazelbuild/rules_python/issues/1297 default_info = default_info_subject( - toolchain_headers.actual.providers_map["DefaultInfo"], + headers_providers.get("DefaultInfo", factory = lambda v, meta: v), meta = env.expect.meta.derive(expr = "default_info"), ) default_info.runfiles().contains_predicate( diff --git a/tests/py_cc_toolchain_info_subject.bzl b/tests/py_cc_toolchain_info_subject.bzl index 20585e9..ab9d1b8 100644 --- a/tests/py_cc_toolchain_info_subject.bzl +++ b/tests/py_cc_toolchain_info_subject.bzl @@ -13,14 +13,7 @@ # limitations under the License. """PyCcToolchainInfo testing subject.""" -# TODO: Load this through truth.bzl#subjects when made available -# https://github.com/bazelbuild/rules_testing/issues/54 -load("@rules_testing//lib/private:dict_subject.bzl", "DictSubject") # buildifier: disable=bzl-visibility - -# TODO: Load this through truth.bzl#subjects when made available -# https://github.com/bazelbuild/rules_testing/issues/54 -load("@rules_testing//lib/private:str_subject.bzl", "StrSubject") # buildifier: disable=bzl-visibility -load(":struct_subject.bzl", "struct_subject") +load("@rules_testing//lib:truth.bzl", "subjects") def _py_cc_toolchain_info_subject_new(info, *, meta): # buildifier: disable=uninitialized @@ -33,14 +26,16 @@ def _py_cc_toolchain_info_subject_new(info, *, meta): return public def _py_cc_toolchain_info_subject_headers(self): - return struct_subject( + return subjects.struct( self.actual.headers, meta = self.meta.derive("headers()"), - providers_map = DictSubject.new, + attrs = dict( + providers_map = subjects.dict, + ), ) def _py_cc_toolchain_info_subject_python_version(self): - return StrSubject.new( + return subjects.str( self.actual.python_version, meta = self.meta.derive("python_version()"), ) diff --git a/tests/struct_subject.bzl b/tests/struct_subject.bzl deleted file mode 100644 index 9d18980..0000000 --- a/tests/struct_subject.bzl +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2023 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# TODO: Replace this with rules_testing StructSubject -# https://github.com/bazelbuild/rules_testing/issues/53 -"""Subject for an arbitrary struct.""" - -def struct_subject(actual, *, meta, **attr_factories): - """Creates a struct subject. - - Args: - actual: struct, the struct to wrap. - meta: rules_testing ExpectMeta object. - **attr_factories: dict of attribute names to factory functions. Each - attribute must exist on the `actual` value. The factory functions - have the signature `def factory(value, *, meta)`, where `value` - is the actual attribute value of the struct, and `meta` is - a rules_testing ExpectMeta object. - - Returns: - StructSubject object. - """ - public_attrs = {} - for name, factory in attr_factories.items(): - if not hasattr(actual, name): - fail("Struct missing attribute: '{}'".format(name)) - - def attr_accessor(*, __name = name, __factory = factory): - return __factory( - getattr(actual, __name), - meta = meta.derive(__name + "()"), - ) - - public_attrs[name] = attr_accessor - public = struct( - actual = actual, - **public_attrs - ) - return public |