|
| 1 | +// Copyright 2020 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <algorithm> |
| 16 | +#include <fstream> |
| 17 | +#include <memory> |
| 18 | +#include <set> |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include "tools/cpp/runfiles/runfiles.h" |
| 23 | +#include "google/protobuf/descriptor.pb.h" |
| 24 | +#include "gmock/gmock.h" |
| 25 | +#include "gtest/gtest.h" |
| 26 | + |
| 27 | + |
| 28 | +using bazel::tools::cpp::runfiles::Runfiles; |
| 29 | +using google::protobuf::FileDescriptorProto; |
| 30 | +using google::protobuf::FileDescriptorSet; |
| 31 | + |
| 32 | +namespace rulesproto { |
| 33 | +constexpr char kWorkspaceRlocation[] = "protobuf/"; |
| 34 | +constexpr char kWorkspaceRlocationBzlmod[] = "_main/"; |
| 35 | + |
| 36 | +namespace { |
| 37 | + |
| 38 | +std::string GetRlocation(const std::string& file) { |
| 39 | + static std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest()); |
| 40 | + std::string path = |
| 41 | + runfiles->Rlocation(rulesproto::kWorkspaceRlocation + file); |
| 42 | + std::ifstream input(path, std::ifstream::binary); |
| 43 | + if (!input) { |
| 44 | + path = runfiles->Rlocation(rulesproto::kWorkspaceRlocationBzlmod + file); |
| 45 | + } |
| 46 | + return path; |
| 47 | +} |
| 48 | + |
| 49 | +template <typename T, typename K> |
| 50 | +bool Contains(const T& container, const K& key) { |
| 51 | + return container.find(key) != container.end(); |
| 52 | +} |
| 53 | + |
| 54 | +std::vector<std::string> ReadFileDescriptorSet(const std::string& path) { |
| 55 | + std::ifstream input(path, std::ifstream::binary); |
| 56 | + EXPECT_TRUE(input) << "Could not open " << path; |
| 57 | + |
| 58 | + FileDescriptorSet file_descriptor_set; |
| 59 | + EXPECT_TRUE(file_descriptor_set.ParseFromIstream(&input)); |
| 60 | + |
| 61 | + std::set<std::string> unordered_proto_files; |
| 62 | + for (FileDescriptorProto file_descriptor : file_descriptor_set.file()) { |
| 63 | + EXPECT_FALSE(Contains(unordered_proto_files, file_descriptor.name())) |
| 64 | + << "Already saw " << file_descriptor.name(); |
| 65 | + unordered_proto_files.insert(file_descriptor.name()); |
| 66 | + } |
| 67 | + |
| 68 | + std::vector<std::string> proto_files(unordered_proto_files.begin(), |
| 69 | + unordered_proto_files.end()); |
| 70 | + std::sort(proto_files.begin(), proto_files.end()); |
| 71 | + return proto_files; |
| 72 | +} |
| 73 | + |
| 74 | +void AssertFileDescriptorSetContains( |
| 75 | + const std::string& path, |
| 76 | + const std::vector<std::string>& expected_proto_files) { |
| 77 | + std::vector<std::string> actual_proto_files = |
| 78 | + ReadFileDescriptorSet(GetRlocation(path)); |
| 79 | + EXPECT_THAT(actual_proto_files, |
| 80 | + ::testing::IsSupersetOf(expected_proto_files)); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace |
| 84 | + |
| 85 | +TEST(ProtoDescriptorSetTest, NoProtos) { |
| 86 | + AssertFileDescriptorSetContains( |
| 87 | + "bazel/tests/no_protos.pb", {}); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(ProtoDescriptorSetTest, WellKnownProtos) { |
| 91 | + AssertFileDescriptorSetContains( |
| 92 | + "bazel/tests/well_known_protos.pb", |
| 93 | + { |
| 94 | + "google/protobuf/any.proto", |
| 95 | + "google/protobuf/api.proto", |
| 96 | + "google/protobuf/descriptor.proto", |
| 97 | + "google/protobuf/duration.proto", |
| 98 | + "google/protobuf/empty.proto", |
| 99 | + "google/protobuf/field_mask.proto", |
| 100 | + "google/protobuf/source_context.proto", |
| 101 | + "google/protobuf/struct.proto", |
| 102 | + "google/protobuf/timestamp.proto", |
| 103 | + "google/protobuf/type.proto", |
| 104 | + "google/protobuf/wrappers.proto", |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace rulesproto |
0 commit comments