Skip to content

Commit

Permalink
feat: Treat transport parameter as preserved (not overriden by defa…
Browse files Browse the repository at this point in the history
…ult) parameter (#100)

This is needed in the context of REGAPIC rollout
  • Loading branch information
vam-google committed Jun 1, 2022
1 parent 4dc51e9 commit e3a1f12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ApiVersionedDir {
private static final String[] PRESERVED_PROTO_LIBRARY_STRING_ATTRIBUTES = {
// Multiple languages:
"package_name",
"transport",
// TypeScript:
"main_service",
"bundle_config",
Expand Down Expand Up @@ -404,13 +405,13 @@ void parseBazelBuildFile(Path file) {
} else if (kind.endsWith("_gapic_library")) {
this.overriddenStringAttributes.put(name, new HashMap<>());
this.overriddenListAttributes.put(name, new HashMap<>());
for (String attr : ApiVersionedDir.PRESERVED_PROTO_LIBRARY_STRING_ATTRIBUTES) {
for (String attr : PRESERVED_PROTO_LIBRARY_STRING_ATTRIBUTES) {
String value = buildozer.getAttribute(file, name, attr);
if (value != null) {
this.overriddenStringAttributes.get(name).put(attr, value);
}
}
for (String attr : ApiVersionedDir.PRESERVED_PROTO_LIBRARY_LIST_ATTRIBUTES) {
for (String attr : PRESERVED_PROTO_LIBRARY_LIST_ATTRIBUTES) {
String value = buildozer.getAttribute(file, name, attr);
if (value != null && value.startsWith("[") && value.endsWith("]")) {
value = value.substring(1, value.length() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@
import org.junit.Test;

public class BuildFileGeneratorTest {

private static final String SRC_DIR = Paths.get("googleapis").toString();
private static final String PATH_PREFIX = Paths.get("bazel", "src", "test", "data").toString();

@Test
public void testGenerateBuildFiles() throws IOException {
String buildozerPath = getBuildozerPath();
ArgsParser args =
new ArgsParser(new String[] {"--buildozer=" + buildozerPath, "--src=" + SRC_DIR});
new ArgsParser(new String[]{"--buildozer=" + buildozerPath, "--src=" + SRC_DIR});
FileWriter fw = new FileWriter();
new BuildFileGenerator().generateBuildFiles(args.createApisVisitor(fw, PATH_PREFIX));

Path fileBodyPathPrefix = Paths.get(PATH_PREFIX, SRC_DIR, "google", "example", "library");
String gapicBuildFilePath =
Paths.get(fileBodyPathPrefix.toString(), "v1", "BUILD.bazel").toString();
String rawBuildFilePath = Paths.get(fileBodyPathPrefix.toString(), "type", "BUILD.bazel").toString();
String rawBuildFilePath = Paths.get(fileBodyPathPrefix.toString(), "type", "BUILD.bazel")
.toString();
String rootBuildFilePath = Paths.get(fileBodyPathPrefix.toString(), "BUILD.bazel").toString();

Assert.assertEquals(4, fw.files.size());
Expand All @@ -55,7 +57,7 @@ public void testGenerateBuildFiles() throws IOException {
public void testGenerateBuildFiles_legacyJavaLanguageOverrides() throws IOException {
String buildozerPath = getBuildozerPath();
ArgsParser args =
new ArgsParser(new String[] {"--buildozer=" + buildozerPath, "--src=" + SRC_DIR});
new ArgsParser(new String[]{"--buildozer=" + buildozerPath, "--src=" + SRC_DIR});
FileWriter fw = new FileWriter();
new BuildFileGenerator().generateBuildFiles(args.createApisVisitor(fw, PATH_PREFIX));

Expand Down Expand Up @@ -84,14 +86,14 @@ public void testRegeneration() throws IOException, InterruptedException {

// I'm lazy, so let's just "cp -r" stuff.
Path fixturesPath = Paths.get(PATH_PREFIX, SRC_DIR);
new ProcessBuilder(new String[] {"cp", "-r", fixturesPath.toString(), tempDirPath.toString()})
new ProcessBuilder(new String[]{"cp", "-r", fixturesPath.toString(), tempDirPath.toString()})
.start()
.waitFor();

String buildozerPath = getBuildozerPath();
Path copiedGoogleapis = Paths.get(tempDirPath.toString(), "googleapis");
ArgsParser args =
new ArgsParser(new String[] {"--buildozer=" + buildozerPath, "--src=" + copiedGoogleapis});
new ArgsParser(new String[]{"--buildozer=" + buildozerPath, "--src=" + copiedGoogleapis});
new BuildFileGenerator()
.generateBuildFiles(args.createApisVisitor(null, tempDirPath.toString()));

Expand Down Expand Up @@ -128,6 +130,8 @@ public void testRegeneration() throws IOException, InterruptedException {
gapicBuildFilePath, "google-cloud-example-library-v1-java", "name", "renamed_java_rule");
buildozer.batchSetAttribute(
gapicBuildFilePath, "library_ruby_gapic", "ruby_cloud_title", "Title with spaces");
buildozer.batchSetAttribute(
gapicBuildFilePath, "library_java_gapic", "transport", "grpc+rest");

// The following values should NOT be preserved:
buildozer.batchSetAttribute(
Expand Down Expand Up @@ -162,21 +166,25 @@ public void testRegeneration() throws IOException, InterruptedException {
buildozer.getAttribute(gapicBuildFilePath, "%java_gapic_assembly_gradle_pkg", "name"));
Assert.assertEquals(
"Title with spaces",
buildozer.getAttribute(gapicBuildFilePath, "%ruby_cloud_gapic_library", "ruby_cloud_title"));
buildozer.getAttribute(gapicBuildFilePath, "%ruby_cloud_gapic_library",
"ruby_cloud_title"));
Assert.assertEquals(
"grpc+rest",
buildozer.getAttribute(gapicBuildFilePath, "%java_gapic_library", "transport"));

// Check that grpc_service_config value is not preserved:
Assert.assertEquals(
"library_example_grpc_service_config.json",
buildozer.getAttribute(gapicBuildFilePath, "library_nodejs_gapic", "grpc_service_config"));

// Check that the changed root file is preserved
Assert.assertEquals(changedRootContent, ApisVisitor.readFile(rootBuildFilePath));

// Now run with overwrite and verify it actually ignores all the changes
ArgsParser argsOverwrite =
new ArgsParser(
new String[] {
"--overwrite", "--buildozer=" + buildozerPath, "--src=" + copiedGoogleapis
new String[]{
"--overwrite", "--buildozer=" + buildozerPath, "--src=" + copiedGoogleapis
});
new BuildFileGenerator()
.generateBuildFiles(argsOverwrite.createApisVisitor(null, tempDirPath.toString()));
Expand Down Expand Up @@ -240,6 +248,7 @@ public void testBuildozer() throws IOException {
// Not using mocking libraries to keep this tool as simple as possible (currently it does not use
// any dependencies). Tests depend only on JUnit.
private static class FileWriter implements ApisVisitor.FileWriter {

private final Map<String, String> files = new HashMap<>();

@Override
Expand Down

0 comments on commit e3a1f12

Please sign in to comment.