Skip to content
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

[ggj][engx] build: add integration golden tests to CircleCI #440

Merged
merged 27 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
90c4f38
fix: support non-name fields with res-refs in resname def parsing
miraleung Oct 27, 2020
a74be70
fix: add workaround for missing default_host and oauth_scopes annotation
miraleung Oct 27, 2020
aef9843
Merge branch 'master' of github.com:googleapis/gapic-generator-java i…
miraleung Oct 29, 2020
fdb1ffb
fix: clarify LRO parsing error messages
miraleung Oct 27, 2020
642c606
feat: support deeply-nested types in AST and proto message parsing
miraleung Oct 28, 2020
3e26d87
fix: prevent resname tokens from matching subcomponents
miraleung Oct 28, 2020
837da38
fix: use TypeParser for proto message parsing
miraleung Oct 28, 2020
a5fc332
fix: use generic types in field instantiation in ServiceClientTest
miraleung Oct 28, 2020
b71b786
fix: prevent descension into map types in nested message parsing
miraleung Oct 29, 2020
602e1df
fix: merge master
miraleung Oct 29, 2020
07dddb8
fix: use both map generics in ServiceClientTest codegen
miraleung Oct 29, 2020
5e95e8f
fix: dir structure of generated files
miraleung Oct 29, 2020
c37c2d0
test: add asset API gradle pkg rules
miraleung Oct 29, 2020
8a7196f
fix: remove unused dep
miraleung Oct 29, 2020
f7f8099
test: add logging integration target and goldens, consolidate rules
miraleung Oct 29, 2020
9e0ad39
fix: merge master
miraleung Oct 29, 2020
9df3a5b
fix: fix asset_java_gapic build
miraleung Oct 30, 2020
96f8676
fix: fix test src packaging, update integration goldens
miraleung Oct 30, 2020
a91a085
fix: pass all tokens to instansiate in resname 1-pattern case
miraleung Oct 30, 2020
19e0a98
fix: update goldens
miraleung Oct 30, 2020
2dfd4b1
fix: update goldens
miraleung Oct 30, 2020
5dae221
build: add all integration tests to pre-commit hook
miraleung Oct 30, 2020
6917a2b
build: run pre-commit when goldens have changed
miraleung Oct 30, 2020
1aaf0a5
build: add integration golden tests to CircleCI
miraleung Oct 30, 2020
caf47e7
Merge branch 'master' into alpha/g18
miraleung Oct 31, 2020
ba71685
Update pre-commit
miraleung Oct 31, 2020
a4c5b95
fix: merge master
miraleung Oct 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ jobs:
cd /tmp/gapic-generator-java
bazel --batch test //src/test/... --noshow_progress
find . -type f -regex ".*/bazel-testlogs/.*xml" -exec cp {} ${TEST_REPORTS_DIR} \;
- run:
name: Run integration tests for gapic-generator-java
command: |
cd /tmp/gapic-generator-java
# Check only the goldens, rely on the pre-commits for client library compilation tests.
# Otherwise, this would take too long.
bazel --batch test //test/integration:asset //test/integration:logging //test/integration:redis --noshow_progress
find . -type f -regex ".*/bazel-testlogs/.*xml" -exec cp {} ${TEST_REPORTS_DIR} \;
- store_test_results:
path: ~/.cache/bazel
google-java-format:
Expand Down
24 changes: 21 additions & 3 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ fi
# Check only the staged files.
NUM_TOTAL_FILES_CHANGED=$(git diff --cached --name-only | wc -l)
NUM_JAVA_FILES_CHANGED=$(git diff --cached --name-only "*.java" | wc -l)
NUM_UNIT_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "src/test/*/*.golden" | wc -l)
NUM_INTEGRATION_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "test/integration/goldens/*/*.golden" | wc -l)
NUM_INTEGRATION_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "test/integration/*/BUILD.bazel" | wc -l)
NUM_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "*BUILD.bazel" | wc -l)

if [ $NUM_TOTAL_FILES_CHANGED -le 0 ]
Expand Down Expand Up @@ -101,10 +104,10 @@ then
fi
fi

# Check tests.
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ]
# Check unit tests.
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] || [ $NUM_UNIT_GOLDEN_FILES_CHANGED -gt 0 ]
then
echo_status "Checking tests..."
echo_status "Checking unit tests..."
bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //src/test/...
TEST_STATUS=$?
if [ $TEST_STATUS != 0 ]
Expand All @@ -114,6 +117,21 @@ then
fi
fi

# Check integration tests.
if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] \
|| [ $NUM_INTEGRATION_GOLDEN_FILES_CHANGED -gt 0 ] \
|| [ $NUM_INTEGRATION_BAZEL_FILES_CHANGED -gt 0 ]
then
echo_status "Checking integration tests..."
bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //test/integration/...
TEST_STATUS=$?
if [ $TEST_STATUS != 0 ]
then
echo_error "Tests failed." "Please fix them and try again."
exit 1
fi
fi

# Check and fix Bazel format.
if [ $NUM_BAZEL_FILES_CHANGED -gt 0 ]
then
Expand Down
11 changes: 8 additions & 3 deletions rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ def _java_gapic_postprocess_srcjar_impl(ctx):
unzip -q temp-codegen.srcjar -d {output_dir_path}
# This may fail if there are spaces and/or too many files (exceed max length of command length).
{formatter} --replace $(find {output_dir_path} -type f -printf "%p ")
zip -r -j {output_srcjar_name}.srcjar {output_dir_path}/src/main/*
zip -r -j {output_srcjar_name}-resource-name.srcjar {output_dir_path}/proto/src/main/*
zip -r -j {output_srcjar_name}-tests.srcjar {output_dir_path}/src/test/*
WORKING_DIR=`pwd`
cd {output_dir_path}/src/main/java
zip -r $WORKING_DIR/{output_srcjar_name}.srcjar ./
cd $WORKING_DIR/{output_dir_path}/proto/src/main/java
zip -r $WORKING_DIR/{output_srcjar_name}-resource-name.srcjar ./
cd $WORKING_DIR/{output_dir_path}/src/test/java
zip -r $WORKING_DIR/{output_srcjar_name}-tests.srcjar ./
cd $WORKING_DIR
mv {output_srcjar_name}.srcjar {output_main}
mv {output_srcjar_name}-resource-name.srcjar {output_resource_name}
mv {output_srcjar_name}-tests.srcjar {output_test}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ public String pakkage() {
public abstract boolean useFullName();

@Override
public String enclosingClassName() {
public ImmutableList<String> enclosingClassNames() {
if (!hasEnclosingClass()) {
return null;
return ImmutableList.of();
}
return clazz().getEnclosingClass().getSimpleName();
// The innermost type will be the last element in the list.
ImmutableList.Builder<String> listBuilder = new ImmutableList.Builder<>();
Class currentClz = clazz();
while (currentClz.getEnclosingClass() != null) {
listBuilder.add(currentClz.getEnclosingClass().getSimpleName());
currentClz = currentClz.getEnclosingClass();
}
return listBuilder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface Reference {
boolean useFullName();

@Nullable
String enclosingClassName();
ImmutableList<String> enclosingClassNames();

@Nullable
Reference wildcardUpperBound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package com.google.api.generator.engine.ast;

import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
Expand All @@ -43,7 +43,7 @@ public abstract class VaporReference implements Reference {

@Nullable
@Override
public abstract String enclosingClassName();
public abstract ImmutableList<String> enclosingClassNames();

@Nullable
public abstract Reference supertypeReference();
Expand All @@ -56,9 +56,9 @@ public Reference wildcardUpperBound() {

@Override
public String fullName() {
// TODO(unsupported): Nested classes with depth greater than 1.
if (hasEnclosingClass()) {
return String.format("%s.%s.%s", pakkage(), enclosingClassName(), plainName());
return String.format(
"%s.%s.%s", pakkage(), String.join(DOT, enclosingClassNames()), plainName());
}
return String.format("%s.%s", pakkage(), plainName());
}
Expand All @@ -68,7 +68,7 @@ public String fullName() {

@Override
public boolean hasEnclosingClass() {
return !Strings.isNullOrEmpty(enclosingClassName());
return !enclosingClassNames().isEmpty();
}

@Override
Expand All @@ -86,7 +86,7 @@ public boolean isSupertypeOrEquals(Reference other) {
VaporReference ref = (VaporReference) other;
return pakkage().equals(ref.pakkage())
&& plainName().equals(ref.plainName())
&& Objects.equals(enclosingClassName(), ref.enclosingClassName());
&& Objects.equals(enclosingClassNames(), ref.enclosingClassNames());
}

@Override
Expand All @@ -112,14 +112,14 @@ public boolean equals(Object o) {
return pakkage().equals(ref.pakkage())
&& name().equals(ref.name())
&& generics().equals(ref.generics())
&& Objects.equals(enclosingClassName(), ref.enclosingClassName());
&& Objects.equals(enclosingClassNames(), ref.enclosingClassNames());
}

@Override
public int hashCode() {
int hash = 17 * pakkage().hashCode() + 19 * name().hashCode() + 23 * generics().hashCode();
if (!Strings.isNullOrEmpty(enclosingClassName())) {
hash += 29 * enclosingClassName().hashCode();
if (!enclosingClassNames().isEmpty()) {
hash += 29 * enclosingClassNames().hashCode();
}
return hash;
}
Expand All @@ -133,7 +133,8 @@ public static Builder builder() {
return new AutoValue_VaporReference.Builder()
.setUseFullName(false)
.setGenerics(ImmutableList.of())
.setIsStaticImport(false);
.setIsStaticImport(false)
.setEnclosingClassNames(Collections.emptyList());
}

// Private.
Expand All @@ -153,7 +154,11 @@ public Builder setGenerics(Reference... references) {

public abstract Builder setGenerics(List<Reference> clazzes);

public abstract Builder setEnclosingClassName(String enclosingClassName);
public Builder setEnclosingClassNames(String... enclosingClassNames) {
return setEnclosingClassNames(Arrays.asList(enclosingClassNames));
}

public abstract Builder setEnclosingClassNames(List<String> enclosingClassNames);

public abstract Builder setIsStaticImport(boolean isStaticImport);

Expand All @@ -166,8 +171,7 @@ public Builder setGenerics(Reference... references) {

abstract ImmutableList<Reference> generics();

@Nullable
abstract String enclosingClassName();
abstract ImmutableList<String> enclosingClassNames();

abstract boolean isStaticImport();

Expand All @@ -180,11 +184,11 @@ public VaporReference build() {

setPlainName(name());

setIsStaticImport(enclosingClassName() != null && isStaticImport());
setIsStaticImport(!enclosingClassNames().isEmpty() && isStaticImport());

StringBuilder sb = new StringBuilder();
if (enclosingClassName() != null && !isStaticImport()) {
sb.append(enclosingClassName());
if (!enclosingClassNames().isEmpty() && !isStaticImport()) {
sb.append(String.join(DOT, enclosingClassNames()));
sb.append(DOT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import javax.annotation.Nullable;

public class ImportWriterVisitor implements AstNodeVisitor {
private static final String DOT = ".";
private static final String NEWLINE = "\n";
private static final String PKG_JAVA_LANG = "java.lang";

Expand Down Expand Up @@ -423,7 +424,8 @@ private void references(List<Reference> refs) {

if (ref.isStaticImport()
&& !Strings.isNullOrEmpty(currentClassName)
&& ref.enclosingClassName().equals(currentClassName)) {
&& !ref.enclosingClassNames().isEmpty()
&& ref.enclosingClassNames().contains(currentClassName)) {
continue;
}

Expand All @@ -432,7 +434,8 @@ private void references(List<Reference> refs) {
staticImports.add(ref.fullName());
} else {
if (ref.hasEnclosingClass()) {
imports.add(String.format("%s.%s", ref.pakkage(), ref.enclosingClassName()));
imports.add(
String.format("%s.%s", ref.pakkage(), String.join(DOT, ref.enclosingClassNames())));
} else {
imports.add(ref.fullName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static MethodDefinition createGetRequestBuilderMethod(
TypeNode builderType =
TypeNode.withReference(
VaporReference.builder()
.setEnclosingClassName(method.inputType().reference().name())
.setEnclosingClassNames(method.inputType().reference().name())
.setName("Builder")
.setPakkage(method.inputType().reference().pakkage())
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,22 @@ static Expr createDefaultValue(
}

static Expr createDefaultValue(Field f) {
return createDefaultValue(f, false);
}

static Expr createDefaultValue(Field f, boolean useExplicitInitTypeInGenerics) {
if (f.isRepeated()) {
TypeNode newType =
TypeNode.withReference(
ConcreteReference.withClazz(f.isMap() ? HashMap.class : ArrayList.class));
ConcreteReference.Builder refBuilder =
ConcreteReference.builder().setClazz(f.isMap() ? HashMap.class : ArrayList.class);
if (useExplicitInitTypeInGenerics) {
if (f.isMap()) {
refBuilder = refBuilder.setGenerics(f.type().reference().generics().subList(0, 2));
} else {
refBuilder = refBuilder.setGenerics(f.type().reference().generics().get(0));
}
}

TypeNode newType = TypeNode.withReference(refBuilder.build());
return NewObjectExpr.builder().setType(newType).setIsGeneric(true).build();
}

Expand Down Expand Up @@ -238,7 +250,7 @@ static Expr createSimpleMessageBuilderExpr(
.setReturnType(TypeNode.STRING)
.build();
} else {
defaultExpr = createDefaultValue(field);
defaultExpr = createDefaultValue(field, true);
}
builderExpr =
MethodInvocationExpr.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(String.format("%sClient", service.name()))
.setEnclosingClassNames(String.format("%sClient", service.name()))
.setIsStaticImport(true)
.build()))));
return types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1090,21 +1090,25 @@ private static MethodDefinition createToStringMethod(
List<List<String>> tokenHierarchies) {
boolean hasVariants = tokenHierarchies.size() > 1;
if (!hasVariants) {
String token = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList()).get(0);
String javaTokenVarName = JavaStyle.toLowerCamelCase(token);
Preconditions.checkNotNull(
patternTokenVarExprs.get(token),
String.format(
"No expression found for token %s amongst values %s",
javaTokenVarName, patternTokenVarExprs.toString()));

List<Expr> instantiateArgExprs = new ArrayList<>();
List<String> tokens = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList());
for (int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i);
Preconditions.checkNotNull(
patternTokenVarExprs.get(token),
String.format(
"No expression found for token %s amongst values %s",
token, patternTokenVarExprs.toString()));
instantiateArgExprs.add(ValueExpr.withValue(StringObjectValue.withValue(token)));
instantiateArgExprs.add(patternTokenVarExprs.get(token));
}

MethodInvocationExpr returnInstantiateExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(templateFinalVarExprs.get(0))
.setMethodName("instantiate")
.setArguments(
ValueExpr.withValue(StringObjectValue.withValue(token)),
patternTokenVarExprs.get(token))
.setArguments(instantiateArgExprs)
.setReturnType(TypeNode.STRING)
.build();
return MethodDefinition.builder()
Expand Down Expand Up @@ -1575,7 +1579,7 @@ private static Map<String, TypeNode> createDynamicTypes(
VaporReference.builder()
.setName("Builder")
.setPakkage(resourceName.pakkage())
.setEnclosingClassName(thisClassName)
.setEnclosingClassNames(thisClassName)
.setIsStaticImport(true)
.build()));

Expand All @@ -1591,7 +1595,7 @@ private static Map<String, TypeNode> createDynamicTypes(
VaporReference.builder()
.setName(s)
.setPakkage(resourceName.pakkage())
.setEnclosingClassName(thisClassName)
.setEnclosingClassNames(thisClassName)
.setIsStaticImport(true)
.build()))));
}
Expand Down
Loading