Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 709763608
  • Loading branch information
dhoekwater authored and copybara-github committed Jan 2, 2025
1 parent 6bc81d4 commit accbe5b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
24 changes: 24 additions & 0 deletions propeller/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,16 @@ cc_library(
],
)

cc_library(
name = "text_proto_flag",
hdrs = ["text_proto_flag.h"],
deps = [
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/strings:string_view",
"@com_google_protobuf//:protobuf",
],
)

########################
# Tests & Test Utils #
########################
Expand Down Expand Up @@ -1611,3 +1621,17 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "text_proto_flag_test",
srcs = ["text_proto_flag_test.cc"],
deps = [
":parse_text_proto",
":propeller_options_cc_proto",
":status_testing_macros",
":text_proto_flag",
"//base:commandlineflags",
"@abseil-cpp//absl/flags:flag",
"@com_google_googletest//:gtest_main",
],
)
32 changes: 32 additions & 0 deletions propeller/text_proto_flag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef THIRD_PARTY_LLVM_PROPELLER_TEXT_PROTO_FLAG_H
#define THIRD_PARTY_LLVM_PROPELLER_TEXT_PROTO_FLAG_H

#include <string>

#include "absl/log/check.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/text_format.h"

namespace propeller {

// A wrapper around a proto message that can be used as a flag.
template <typename Proto>
struct TextProtoFlag {
Proto message;
};

template <typename Proto>
inline bool AbslParseFlag(absl::string_view text, TextProtoFlag<Proto>* flag,
std::string* err) {
return google::protobuf::TextFormat::ParseFromString(text, &flag->message);
}

template <typename Proto>
inline std::string AbslUnparseFlag(const TextProtoFlag<Proto>& flag) {
std::string text_proto;
CHECK(google::protobuf::TextFormat::PrintToString(flag.message, &text_proto));
return text_proto;
}
} // namespace propeller

#endif // THIRD_PARTY_LLVM_PROPELLER_TEXT_PROTO_FLAG_H
52 changes: 52 additions & 0 deletions propeller/text_proto_flag_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "propeller/text_proto_flag.h"

#include <string>

#include "absl/flags/flag.h"
#include "base/commandlineflags.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "propeller/parse_text_proto.h"
#include "propeller/propeller_options.pb.h"
#include "propeller/status_testing_macros.h"

ABSL_FLAG(propeller::TextProtoFlag<propeller::PropellerOptions>,
propeller_options, {},
"Test flag for propeller::PropellerOptions encoded as a TextProto.");

namespace {

using ::propeller::PropellerOptions;
using ::propeller_testing::ParseTextProtoOrDie;
using ::testing::EqualsProto;

TEST(TextProtoFlagTest, ValidParse) {
ASSERT_FALSE(SetCommandLineOption("propeller_options",
"binary_name: '/path/to/binary'")
.empty());

EXPECT_THAT(absl::GetFlag(FLAGS_propeller_options).message,
EqualsProto(R"pb(binary_name: "/path/to/binary")pb"));
}

TEST(TextProtoFlagTest, InvalidParse) {
ASSERT_TRUE(SetCommandLineOption("propeller_options", "UNKNOWN_FIELD : true")
.empty());

// Since the parsing failed, the flag should be empty.
EXPECT_THAT(absl::GetFlag(FLAGS_propeller_options).message,
EqualsProto(PropellerOptions()));
}

TEST(TextProtoFlagTest, SetFlag) {
// Clear the flag.
ASSERT_FALSE(SetCommandLineOption("propeller_options", "").empty());

absl::SetFlag(&FLAGS_propeller_options,
propeller::TextProtoFlag<PropellerOptions>{
.message = ParseTextProtoOrDie(
R"pb(binary_name: "/path/to/binary")pb")});
EXPECT_THAT(absl::GetFlag(FLAGS_propeller_options).message,
EqualsProto(R"pb(binary_name: "/path/to/binary")pb"));
}
} // namespace

0 comments on commit accbe5b

Please sign in to comment.