|
| 1 | +// Copyright (c) 2020-2025, Google LLC |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file or at |
| 6 | +// https://developers.google.com/open-source/licenses/bsd |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | +#include <fstream> |
| 10 | +#include <memory> |
| 11 | +#include <set> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include "gmock/gmock.h" |
| 16 | +#include "google/protobuf/descriptor.pb.h" |
| 17 | +#include "gtest/gtest.h" |
| 18 | +#include "tools/cpp/runfiles/runfiles.h" |
| 19 | + |
| 20 | +using bazel::tools::cpp::runfiles::Runfiles; |
| 21 | +using google::protobuf::FileDescriptorProto; |
| 22 | +using google::protobuf::FileDescriptorSet; |
| 23 | + |
| 24 | +namespace rulesproto { |
| 25 | +constexpr char kWorkspaceRlocation[] = "protobuf/"; |
| 26 | +constexpr char kWorkspaceRlocationBzlmod[] = "_main/"; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +std::string GetRlocation(const std::string& file) { |
| 31 | + static std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest()); |
| 32 | + std::string path = |
| 33 | + runfiles->Rlocation(rulesproto::kWorkspaceRlocation + file); |
| 34 | + std::ifstream input(path, std::ifstream::binary); |
| 35 | + if (!input) { |
| 36 | + path = runfiles->Rlocation(rulesproto::kWorkspaceRlocationBzlmod + file); |
| 37 | + } |
| 38 | + return path; |
| 39 | +} |
| 40 | + |
| 41 | +template <typename T, typename K> |
| 42 | +bool Contains(const T& container, const K& key) { |
| 43 | + return container.find(key) != container.end(); |
| 44 | +} |
| 45 | + |
| 46 | +std::vector<std::string> ReadFileDescriptorSet(const std::string& path) { |
| 47 | + std::ifstream input(path, std::ifstream::binary); |
| 48 | + EXPECT_TRUE(input) << "Could not open " << path; |
| 49 | + |
| 50 | + FileDescriptorSet file_descriptor_set; |
| 51 | + EXPECT_TRUE(file_descriptor_set.ParseFromIstream(&input)); |
| 52 | + |
| 53 | + std::set<std::string> unordered_proto_files; |
| 54 | + for (FileDescriptorProto file_descriptor : file_descriptor_set.file()) { |
| 55 | + EXPECT_FALSE(Contains(unordered_proto_files, file_descriptor.name())) |
| 56 | + << "Already saw " << file_descriptor.name(); |
| 57 | + unordered_proto_files.insert(file_descriptor.name()); |
| 58 | + } |
| 59 | + |
| 60 | + std::vector<std::string> proto_files(unordered_proto_files.begin(), |
| 61 | + unordered_proto_files.end()); |
| 62 | + std::sort(proto_files.begin(), proto_files.end()); |
| 63 | + return proto_files; |
| 64 | +} |
| 65 | + |
| 66 | +void AssertFileDescriptorSetContains( |
| 67 | + const std::string& path, |
| 68 | + const std::vector<std::string>& expected_proto_files) { |
| 69 | + std::vector<std::string> actual_proto_files = |
| 70 | + ReadFileDescriptorSet(GetRlocation(path)); |
| 71 | + EXPECT_THAT(actual_proto_files, |
| 72 | + ::testing::IsSupersetOf(expected_proto_files)); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace |
| 76 | + |
| 77 | +TEST(ProtoDescriptorSetTest, NoProtos) { |
| 78 | + AssertFileDescriptorSetContains("bazel/tests/no_protos.pb", {}); |
| 79 | +} |
| 80 | + |
| 81 | +TEST(ProtoDescriptorSetTest, WellKnownProtos) { |
| 82 | + AssertFileDescriptorSetContains("bazel/tests/well_known_protos.pb", |
| 83 | + { |
| 84 | + "google/protobuf/any.proto", |
| 85 | + "google/protobuf/api.proto", |
| 86 | + "google/protobuf/descriptor.proto", |
| 87 | + "google/protobuf/duration.proto", |
| 88 | + "google/protobuf/empty.proto", |
| 89 | + "google/protobuf/field_mask.proto", |
| 90 | + "google/protobuf/source_context.proto", |
| 91 | + "google/protobuf/struct.proto", |
| 92 | + "google/protobuf/timestamp.proto", |
| 93 | + "google/protobuf/type.proto", |
| 94 | + "google/protobuf/wrappers.proto", |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace rulesproto |
0 commit comments