Skip to content

Commit

Permalink
[ggj][infra][5/5] support goldens updates for composer tests (#348)
Browse files Browse the repository at this point in the history
* add golden folder

* format

* move all expected strings to golden files

* format

* update goldens

* update ast integration test

* keep service client test

* constant helper

* format

* format

* support update goldens for ast integration test

* support update goldens for omposer tests

* fix

* update

* remove unneeded dependencies

* remove unneeded dependencies
  • Loading branch information
xiaozhenliu-gg5 authored Oct 2, 2020
1 parent a3f3c5b commit 3c1f069
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 2 deletions.
16 changes: 16 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ java_binary(
srcs = [
"//src/test/java/com/google/api/generator/gapic/dummy:dummy_files",
"//src/test/java/com/google/api/generator/engine:engine_files",
"//src/test/java/com/google/api/generator/gapic/composer:composer_files",
"//src/test/java/com/google/api/generator/test/framework:framework_files",
],
data = [
"//src/test/java/com/google/api/generator/gapic/dummy/goldens:goldens_files",
"//src/test/java/com/google/api/generator/engine/goldens:goldens_files",
"//src/test/java/com/google/api/generator/gapic/composer/goldens:goldens_files",
"//src/test/java/com/google/api/generator/gapic/testdata:gapic_config_files",
"//src/test/java/com/google/api/generator/gapic/testdata:service_config_files",
],
jvm_flags = ["-Xmx512m"],
main_class = "com.google.api.generator.test.framework.SingleJUnitTestRunner",
Expand All @@ -67,7 +71,19 @@ java_binary(
"//src/main/java/com/google/api/generator/engine/writer",
"//src/test/java/com/google/api/generator/test/framework",
"//src/main/java/com/google/api/generator/engine/lexicon",
"//src/main/java/com/google/api/generator/gapic/composer",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/protoparser",
"//src/test/java/com/google/api/generator/gapic/testdata:showcase_java_proto",
"//src/test/java/com/google/api/generator/gapic/testdata:testgapic_java_proto",
"//src/test/java/com/google/api/generator/gapic/composer:common_resources_java_proto",
"@com_google_guava_guava//:com_google_guava_guava",
"@com_google_api_gax_java//gax",
"@com_google_googleapis//google/logging/v2:logging_java_proto",
"@com_google_googleapis//google/pubsub/v1:pubsub_java_proto",
"@com_google_googleapis//google/rpc:rpc_java_proto",
"@com_google_protobuf//:protobuf_java",
"//:service_config_java_proto",
"@com_google_truth_truth//jar",
"@io_github_java_diff_utils//jar",
"@junit_junit//jar",
Expand Down
2 changes: 1 addition & 1 deletion rules_bazel/java/java_diff_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ junit_output_zip = rule(
),
},
outputs = {
"output": "%{name}%.zip",
"output": "%{name}.zip",
},
implementation = _junit_output_impl,
)
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("//:rules_bazel/java/java_diff_test.bzl", "golden_update")

package(default_visibility = ["//visibility:public"])

TESTS = [
Expand All @@ -18,6 +20,19 @@ TESTS = [
"ServiceStubClassComposerTest",
]

UPDATE_GOLDENS_TESTS = [
"BatchingDescriptorComposerTest",
"ComposerTest",
"GrpcServiceCallableFactoryClassComposerTest",
"GrpcServiceStubClassComposerTest",
"MockServiceClassComposerTest",
"MockServiceImplClassComposerTest",
"ResourceNameHelperClassComposerTest",
"ServiceSettingsClassComposerTest",
"ServiceStubSettingsClassComposerTest",
"ServiceStubClassComposerTest",
]

filegroup(
name = "composer_files",
srcs = glob(["*.java"]),
Expand Down Expand Up @@ -69,3 +84,20 @@ java_proto_library(
"@junit_junit//jar",
],
) for test_name in TESTS]



TEST_CLASS_DIR = "com.google.api.generator.gapic.composer."
# Run `bazel run src/test/java/com/google/api/generator/gapic/composer:testTargetName_update`
# to update goldens as expected generated code.
# `ServiceClient*` tests are not supported now since they are still in active development.

[golden_update(
name = test_name + "_update",
srcs = [
":composer_files",
"//src/test/java/com/google/api/generator/gapic/testdata:gapic_config_files",
"//src/test/java/com/google/api/generator/gapic/testdata:service_config_files",
],
test_class_name = TEST_CLASS_DIR + test_name,
) for test_name in UPDATE_GOLDENS_TESTS]
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.gapic.protoparser.ServiceConfigParser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.logging.v2.LogEntryProto;
import com.google.logging.v2.LoggingConfigProto;
import com.google.logging.v2.LoggingMetricsProto;
Expand Down Expand Up @@ -99,6 +100,8 @@ public void batchingDescriptor_hasSubresponseField() {
method, batchingSetting, messageTypes);

batchingDescriptorExpr.accept(writerVisitor);
Utils.saveCodegenToFile(
this.getClass(), "BatchingDescriptorComposerTestSubresponse.golden", writerVisitor.write());
Path goldenFilePath =
Paths.get(
ComposerConstants.GOLDENFILES_DIRECTORY,
Expand Down Expand Up @@ -155,6 +158,10 @@ public void batchingDescriptor_noSubresponseField() {
method, batchingSetting, messageTypes);

batchingDescriptorExpr.accept(writerVisitor);
Utils.saveCodegenToFile(
this.getClass(),
"BatchingDescriptorComposerTestNoSubresponse.golden",
writerVisitor.write());
Path goldenFilePath =
Paths.get(
ComposerConstants.GOLDENFILES_DIRECTORY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicClass.Kind;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
Expand All @@ -39,6 +40,7 @@ public void gapicClass_addApacheLicense() {
Composer.addApacheLicense(Arrays.asList(GapicClass.create(Kind.TEST, classDef)));
JavaWriterVisitor visitor = new JavaWriterVisitor();
gapicClassWithHeaderList.get(0).classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "ComposerTest.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "ComposerTest.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void generateServiceClasses() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "GrpcEchoCallableFactory.golden", visitor.write());
Path goldenFilePath =
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "GrpcEchoCallableFactory.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void generateServiceClasses() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "GrpcEchoStub.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "GrpcEchoStub.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void generateServiceClasses() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "MockEcho.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "MockEcho.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void generateServiceClasses() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "MockEchoImpl.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "MockEchoImpl.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
Expand Down Expand Up @@ -97,6 +98,7 @@ public void generateResourceNameClass_echoFoobarMultiplePatterns() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "FoobarName.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "FoobarName.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand All @@ -122,6 +124,7 @@ public void generateResourceNameClass_testingSessionOnePattern() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "SessionName.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "SessionName.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void generateServiceClasses() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "EchoSettings.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "EchoSettings.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void generateServiceClasses() {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "EchoStub.golden", visitor.write());
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "EchoStub.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.gapic.protoparser.ServiceConfigParser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.logging.v2.LogEntryProto;
import com.google.logging.v2.LoggingConfigProto;
import com.google.logging.v2.LoggingMetricsProto;
Expand Down Expand Up @@ -93,6 +94,8 @@ public void generateServiceStubSettingsClasses_batchingWithEmptyResponses() thro

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(
this.getClass(), "LoggingServiceV2StubSettings.golden", visitor.write());
Path goldenFilePath =
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "LoggingServiceV2StubSettings.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
Expand Down Expand Up @@ -135,6 +138,7 @@ public void generateServiceStubSettingsClasses_batchingWithNonemptyResponses()

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "PublisherStubSettings.golden", visitor.write());
Path goldenFilePath =
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "PublisherStubSettings.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
Expand Down Expand Up @@ -165,6 +169,7 @@ public void generateServiceStubSettingsClasses_basic() throws IOException {

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "EchoStubSettings.golden", visitor.write());
Path goldenFilePath =
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "EchoStubSettings.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ public long countBytes(PublishRequest request) {
return request.getSerializedSize();
}

}
}

0 comments on commit 3c1f069

Please sign in to comment.