From c6753b9d068191ced6b1f806f05937f9abdb751f Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 22 Oct 2024 12:04:50 -0700 Subject: [PATCH] Make import! of protobuf_gtest_matchers work properly. This required making a shim file for a crate to be able to re-export either the _cpp or _upb impl so that it can be one crate that conditionally delegates to the intended one. PiperOrigin-RevId: 688639897 --- rust/BUILD | 22 +++++++++------- rust/gtest_matchers.rs | 30 +++------------------ rust/gtest_matchers_impl.rs | 35 +++++++++++++++++++++++++ rust/test/shared/gtest_matchers_test.rs | 4 +++ 4 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 rust/gtest_matchers_impl.rs diff --git a/rust/BUILD b/rust/BUILD index 7e7be822bc13..fbcda882760d 100644 --- a/rust/BUILD +++ b/rust/BUILD @@ -161,21 +161,25 @@ rust_library( deps = [":protobuf_cpp"], ) -alias( +rust_library( name = "protobuf_gtest_matchers", - actual = select({ - ":use_upb_kernel": ":protobuf_gtest_matchers_upb", - "//conditions:default": ":protobuf_gtest_matchers_cpp", + testonly = True, + srcs = ["gtest_matchers.rs"], + aliases = select({ + ":use_upb_kernel": {"//rust:protobuf_gtest_matchers_upb": "protobuf_gtest_matchers_impl"}, + "//conditions:default": {"//rust:protobuf_gtest_matchers_cpp": "protobuf_gtest_matchers_impl"}, + }), + visibility = ["//visibility:public"], + deps = select({ + ":use_upb_kernel": [":protobuf_gtest_matchers_upb"], + "//conditions:default": [":protobuf_gtest_matchers_cpp"], }), - visibility = [ - "//visibility:public", - ], ) rust_library( name = "protobuf_gtest_matchers_cpp", testonly = True, - srcs = ["gtest_matchers.rs"], + srcs = ["gtest_matchers_impl.rs"], aliases = { "//rust:protobuf_cpp": "protobuf", }, @@ -189,7 +193,7 @@ rust_library( rust_library( name = "protobuf_gtest_matchers_upb", testonly = True, - srcs = ["gtest_matchers.rs"], + srcs = ["gtest_matchers_impl.rs"], aliases = { "//rust:protobuf_upb": "protobuf", }, diff --git a/rust/gtest_matchers.rs b/rust/gtest_matchers.rs index 0dc833a7584a..70ea26a1e57f 100644 --- a/rust/gtest_matchers.rs +++ b/rust/gtest_matchers.rs @@ -5,31 +5,7 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -use googletest::description::Description; -use googletest::matcher::{Matcher, MatcherBase, MatcherResult}; -use protobuf::__internal::MatcherEq; +extern crate protobuf_gtest_matchers_impl; -#[derive(MatcherBase)] -pub struct MessageMatcher { - expected: T, -} - -impl Matcher<&T> for MessageMatcher -where - T: MatcherEq, -{ - fn matches(&self, actual: &T) -> MatcherResult { - actual.matches(&self.expected).into() - } - - fn describe(&self, matcher_result: MatcherResult) -> Description { - match matcher_result { - MatcherResult::Match => format!("is equal to {:?}", self.expected).into(), - MatcherResult::NoMatch => format!("is not equal to {:?}", self.expected).into(), - } - } -} - -pub fn proto_eq(expected: T) -> MessageMatcher { - MessageMatcher { expected } -} +// Re-export all symbols from the underlying implementation of gtest_matchers. +pub use protobuf_gtest_matchers_impl::*; diff --git a/rust/gtest_matchers_impl.rs b/rust/gtest_matchers_impl.rs new file mode 100644 index 000000000000..0dc833a7584a --- /dev/null +++ b/rust/gtest_matchers_impl.rs @@ -0,0 +1,35 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2024 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +use googletest::description::Description; +use googletest::matcher::{Matcher, MatcherBase, MatcherResult}; +use protobuf::__internal::MatcherEq; + +#[derive(MatcherBase)] +pub struct MessageMatcher { + expected: T, +} + +impl Matcher<&T> for MessageMatcher +where + T: MatcherEq, +{ + fn matches(&self, actual: &T) -> MatcherResult { + actual.matches(&self.expected).into() + } + + fn describe(&self, matcher_result: MatcherResult) -> Description { + match matcher_result { + MatcherResult::Match => format!("is equal to {:?}", self.expected).into(), + MatcherResult::NoMatch => format!("is not equal to {:?}", self.expected).into(), + } + } +} + +pub fn proto_eq(expected: T) -> MessageMatcher { + MessageMatcher { expected } +} diff --git a/rust/test/shared/gtest_matchers_test.rs b/rust/test/shared/gtest_matchers_test.rs index 7db88f785f27..f1425cb9268c 100644 --- a/rust/test/shared/gtest_matchers_test.rs +++ b/rust/test/shared/gtest_matchers_test.rs @@ -5,6 +5,10 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd +google3::import! { + "//third_party/protobuf/rust:protobuf_gtest_matchers"; +} + use edition_unittest_rust_proto::TestAllTypes as TestAllTypesEditions; use googletest::prelude::*; use paste::paste;