-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(generator): split DescriptorPoolFixture #10676
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "generator/testing/descriptor_pool_fixture.h" | ||
#include "generator/testing/error_collectors.h" | ||
#include "absl/memory/memory.h" | ||
#include <google/api/annotations.pb.h> | ||
#include <google/api/client.pb.h> | ||
#include <google/longrunning/operations.pb.h> | ||
#include <google/protobuf/any.pb.h> | ||
#include <google/protobuf/duration.pb.h> | ||
#include <google/protobuf/empty.pb.h> | ||
#include <google/rpc/status.pb.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace generator_testing { | ||
|
||
auto constexpr kEmptyFile = R"""(syntax = "proto3";)"""; | ||
|
||
auto constexpr kCommonFileContents = R"""( | ||
// We need to test that our generator handles references to different entities. | ||
// This simulated .proto file provides their definition. | ||
|
||
syntax = "proto3"; | ||
package test.v1; | ||
|
||
// A request type for the methods | ||
message Request {} | ||
// A response type for the methods | ||
message Response {} | ||
// A metadata type for some LROs | ||
message Metadata {} | ||
)"""; | ||
|
||
DescriptorPoolFixture::DescriptorPoolFixture() | ||
: descriptor_error_collector_( | ||
absl::make_unique<generator_testing::ErrorCollector>()), | ||
multifile_error_collector_( | ||
absl::make_unique<generator_testing::MultiFileErrorCollector>()), | ||
source_tree_db_(&source_tree_), | ||
merged_db_(&simple_db_, &source_tree_db_), | ||
pool_(&merged_db_, descriptor_error_collector_.get()) { | ||
source_tree_db_.RecordErrorsTo(multifile_error_collector_.get()); | ||
source_tree_.Insert("test/v1/common.proto", kCommonFileContents); | ||
source_tree_.Insert("google/api/annotations.proto", kEmptyFile); | ||
source_tree_.Insert("google/api/client.proto", kEmptyFile); | ||
|
||
// We need google.longrunning.* to be available. This also imports the | ||
// google.protobuf.*Descriptor protos. | ||
for (auto const* descriptor : { | ||
google::protobuf::FileDescriptorProto::descriptor(), | ||
google::rpc::Status::descriptor(), | ||
google::protobuf::Any::descriptor(), | ||
google::protobuf::Duration::descriptor(), | ||
google::protobuf::Empty::descriptor(), | ||
google::longrunning::Operation::descriptor(), | ||
}) { | ||
google::protobuf::FileDescriptorProto proto; | ||
descriptor->file()->CopyTo(&proto); | ||
simple_db_.Add(proto); | ||
} | ||
} | ||
|
||
google::protobuf::FileDescriptor const* DescriptorPoolFixture::FindFile( | ||
std::string const& name) const { | ||
return pool_.FindFileByName(name); | ||
} | ||
|
||
bool DescriptorPoolFixture::AddProtoFile(std::string const& name, | ||
std::string contents) { | ||
source_tree_.Insert(name, std::move(contents)); | ||
return pool_.FindFileByName(name) != nullptr; | ||
} | ||
|
||
} // namespace generator_testing | ||
} // namespace cloud | ||
} // namespace google |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_GENERATOR_TESTING_DESCRIPTOR_POOL_FIXTURE_H | ||
#define GOOGLE_CLOUD_CPP_GENERATOR_TESTING_DESCRIPTOR_POOL_FIXTURE_H | ||
|
||
#include "generator/testing/fake_source_tree.h" | ||
#include <google/protobuf/compiler/importer.h> | ||
#include <google/protobuf/descriptor.h> | ||
#include <google/protobuf/descriptor_database.h> | ||
#include <gmock/gmock.h> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace generator_testing { | ||
|
||
/** | ||
* Implements a fixture for test using a `google::protobuf::DescriptorPool`. | ||
* | ||
* Some test need a properly initialized descriptor pool, with: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/test/tests/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
* - the basic protobuf types and options already available | ||
* - connected to error collectors so the test fails with meaningful errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/connected to// There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
* if there is a test bug | ||
* - basic Google features, like longrunning operations. | ||
* | ||
* This class provides all these features so we don't duplicate them across | ||
* tests. It also provides helpers to add more (simulated) `.proto` files. | ||
*/ | ||
class DescriptorPoolFixture : public ::testing::Test { | ||
public: | ||
DescriptorPoolFixture(); | ||
|
||
/** | ||
* Returns the descriptor for a given file. | ||
* | ||
* Implicitly, this "compiles" the file and validates it. It can be used | ||
* to verify the imports compile correctly before adding some other test | ||
* proto contents. | ||
*/ | ||
google::protobuf::FileDescriptor const* FindFile( | ||
std::string const& name) const; | ||
|
||
/** | ||
* Adds a new proto file and "compiles" it. | ||
* | ||
* Typically used to set up the conditions of a test, as in: | ||
* | ||
* @code | ||
* class MyTest : public DescriptorPoolFixture {}; | ||
* | ||
* TEST_F(MyTest, Name) { | ||
* ASSERT_TRUE(AddProtoFile("foo.proto", contents)); | ||
* } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, fixed. |
||
*/ | ||
bool AddProtoFile(std::string const& name, std::string contents); | ||
|
||
google::protobuf::DescriptorPool const& pool() const { return pool_; } | ||
|
||
private: | ||
std::unique_ptr<google::protobuf::DescriptorPool::ErrorCollector> | ||
descriptor_error_collector_; | ||
std::unique_ptr<google::protobuf::compiler::MultiFileErrorCollector> | ||
multifile_error_collector_; | ||
FakeSourceTree source_tree_; | ||
google::protobuf::SimpleDescriptorDatabase simple_db_; | ||
google::protobuf::compiler::SourceTreeDescriptorDatabase source_tree_db_; | ||
google::protobuf::MergedDescriptorDatabase merged_db_; | ||
google::protobuf::DescriptorPool pool_; | ||
}; | ||
|
||
} // namespace generator_testing | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GENERATOR_TESTING_DESCRIPTOR_POOL_FIXTURE_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: s/test using/testing with/ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed