Skip to content

Commit 672d0fc

Browse files
committed
fix(generator): clean up protobuf debug markers and format test expectations
1 parent fb42587 commit 672d0fc

3 files changed

Lines changed: 33 additions & 23 deletions

File tree

generator/internal/discovery_type_vertex_test.cc

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,31 +248,28 @@ TEST(DiscoveryTypeVertexTest, FormatFieldOptionsDebugRedactNonMatches) {
248248
)""";
249249
auto json = nlohmann::json::parse(kFieldJson, nullptr, false);
250250
ASSERT_TRUE(json.is_object());
251-
EXPECT_THAT(
252-
DiscoveryTypeVertex::FormatFieldOptions("monkey", "monkey", json),
253-
Eq(" [json_name=\"monkey\"]"));
251+
EXPECT_THAT(DiscoveryTypeVertex::FormatFieldOptions("monkey", "monkey", json),
252+
Eq(" [json_name=\"monkey\"]"));
254253
EXPECT_THAT(
255254
DiscoveryTypeVertex::FormatFieldOptions("keyboard", "keyboard", json),
256255
Eq(" [json_name=\"keyboard\"]"));
257-
EXPECT_THAT(
258-
DiscoveryTypeVertex::FormatFieldOptions("hockey", "hockey", json),
259-
Eq(" [json_name=\"hockey\"]"));
260-
EXPECT_THAT(
261-
DiscoveryTypeVertex::FormatFieldOptions("keypad", "keypad", json),
262-
Eq(" [json_name=\"keypad\"]"));
256+
EXPECT_THAT(DiscoveryTypeVertex::FormatFieldOptions("hockey", "hockey", json),
257+
Eq(" [json_name=\"hockey\"]"));
258+
EXPECT_THAT(DiscoveryTypeVertex::FormatFieldOptions("keypad", "keypad", json),
259+
Eq(" [json_name=\"keypad\"]"));
263260
}
264261

265-
TEST(DiscoveryTypeVertexTest, FormatFieldOptionsDebugRedactNonStringNotRedacted) {
262+
TEST(DiscoveryTypeVertexTest,
263+
FormatFieldOptionsDebugRedactNonStringNotRedacted) {
266264
auto constexpr kFieldJson = R"""(
267265
{
268266
"type": "integer"
269267
}
270268
)""";
271269
auto json = nlohmann::json::parse(kFieldJson, nullptr, false);
272270
ASSERT_TRUE(json.is_object());
273-
EXPECT_THAT(
274-
DiscoveryTypeVertex::FormatFieldOptions("key_id", "keyId", json),
275-
Eq(" [json_name=\"keyId\"]"));
271+
EXPECT_THAT(DiscoveryTypeVertex::FormatFieldOptions("key_id", "keyId", json),
272+
Eq(" [json_name=\"keyId\"]"));
276273
}
277274

278275
TEST(DiscoveryTypeVertexTest, FormatFieldOptionsDebugRedactRequired) {
@@ -1420,8 +1417,7 @@ message TestSchema {
14201417
"optional string to: optional double")));
14211418
}
14221419

1423-
TEST_F(DiscoveryTypeVertexDescriptorTest,
1424-
JsonToProtobufCustomerEncryptionKey) {
1420+
TEST_F(DiscoveryTypeVertexDescriptorTest, JsonToProtobufCustomerEncryptionKey) {
14251421
auto constexpr kSchemaJson = R"""(
14261422
{
14271423
"id": "CustomerEncryptionKey",

google/cloud/internal/debug_string_protobuf.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,30 @@ class TimestampMessagePrinter
6363
};
6464

6565
template <typename Printer>
66-
auto SetRedact(Printer& p, int)
67-
-> decltype(p.SetRedactDebugString(true), void()) {
66+
auto SetRedact(Printer& p, int) -> decltype(p.SetRedactDebugString(true),
67+
void()) {
6868
p.SetRedactDebugString(true);
6969
}
7070

7171
template <typename Printer>
7272
void SetRedact(Printer&, ...) {}
7373

74+
void RemoveSilentMarker(std::string& str) {
75+
auto start = str.find("goo.gle/");
76+
if (start != std::string::npos) {
77+
auto nl = str.find('\n', start);
78+
if (nl != std::string::npos) {
79+
str.erase(0, nl + 1);
80+
} else {
81+
auto end = str.find_first_of(" \t", start);
82+
if (end != std::string::npos) {
83+
auto next = str.find_first_not_of(" \t", end);
84+
str.erase(0, next == std::string::npos ? str.size() : next);
85+
}
86+
}
87+
}
88+
}
89+
7490
} // namespace
7591

7692
std::string DebugString(google::protobuf::Message const& m,
@@ -90,6 +106,7 @@ std::string DebugString(google::protobuf::Message const& m,
90106
p.RegisterMessagePrinter(google::protobuf::Timestamp::descriptor(),
91107
new TimestampMessagePrinter);
92108
p.PrintToString(m, &str);
109+
RemoveSilentMarker(str);
93110
return absl::StrCat(m.GetTypeName(), " {",
94111
(options.single_line_mode() ? " " : "\n"), str, "}");
95112
}

google/cloud/internal/debug_string_protobuf_test.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ TEST(LogWrapperHelpers, DefaultOptions) {
5454
// clang-format off
5555
std::string const text =
5656
R"pb(google.iam.v1.Policy { )pb"
57-
R"pb(goo.gle/debugonly )pb"
5857
R"pb(bindings { )pb"
5958
R"pb(role: "roles/viewer" )pb"
6059
R"pb(members: "user:user1@example.com" )pb"
@@ -76,7 +75,6 @@ TEST(LogWrapperHelpers, MultiLine) {
7675
tracing_options.SetOptions("single_line_mode=off");
7776
// clang-format off
7877
std::string const text = R"pb(google.iam.v1.Policy {
79-
goo.gle/debugonly
8078
bindings {
8179
role: "roles/viewer"
8280
members: "user:user1@example.com"
@@ -99,7 +97,6 @@ TEST(LogWrapperHelpers, Truncate) {
9997
// clang-format off
10098
std::string const text =
10199
R"pb(google.iam.v1.Policy { )pb"
102-
R"pb(goo.gle/debugonly )pb"
103100
R"pb(bindings { )pb"
104101
R"pb(role: "roles/vi...<truncated>..." )pb"
105102
R"pb(members: "user:use...<truncated>..." )pb"
@@ -121,7 +118,7 @@ TEST(LogWrapperHelpers, Duration) {
121118
duration.set_seconds((11 * 60 + 22) * 60 + 33);
122119
duration.set_nanos(123456789);
123120
std::string const expected =
124-
R"(google.protobuf.Duration { goo.gle/debugonly "11h22m33.123456789s" })";
121+
R"(google.protobuf.Duration { "11h22m33.123456789s" })";
125122
EXPECT_EQ(expected, DebugString(duration, TracingOptions{}.SetOptions(
126123
"single_line_mode=on")));
127124
}
@@ -131,7 +128,6 @@ TEST(LogWrapperHelpers, Timestamp) {
131128
timestamp.set_seconds(1658470436);
132129
timestamp.set_nanos(123456789);
133130
std::string const expected = R"(google.protobuf.Timestamp {
134-
goo.gle/debugonly
135131
"2022-07-22T06:13:56.123456789Z"
136132
})";
137133
EXPECT_EQ(expected, DebugString(timestamp, TracingOptions{}.SetOptions(
@@ -150,7 +146,8 @@ TEST(LogWrapperHelpers, RedactedField) {
150146
message_proto->add_field();
151147
unredacted_field->set_name("public_field");
152148
unredacted_field->set_number(1);
153-
unredacted_field->set_type(google::protobuf::FieldDescriptorProto::TYPE_STRING);
149+
unredacted_field->set_type(
150+
google::protobuf::FieldDescriptorProto::TYPE_STRING);
154151

155152
google::protobuf::FieldDescriptorProto* redacted_field =
156153
message_proto->add_field();

0 commit comments

Comments
 (0)