Skip to content

Commit

Permalink
fix: properly support spaces in string values (#27)
Browse files Browse the repository at this point in the history
* fix: properly support spaces in string values

* fix: pr feedback and refactor
  • Loading branch information
alexander-fenster committed Feb 24, 2021
1 parent f8c7868 commit ff1d244
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
42 changes: 19 additions & 23 deletions bazel/src/main/java/com/google/api/codegen/bazel/Buildozer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,52 +82,48 @@ public String getAttribute(Path bazelBuildFile, String target, String attribute)
if (value.equals("(missing)")) {
return null;
}
// if value has spaces, `buildozer print` will return it in quotes. Remove the quotes
if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
value = value.substring(1, value.length() - 1);
}
return value;
} catch (IndexOutOfBoundsException ignored) {
return null;
}
}

// Set the value to the given attribute of the given target. Apply changes
// immediately.
public void setAttribute(Path bazelBuildFile, String target, String attribute, String value)
throws IOException {
execute(bazelBuildFile, String.format("set %s \"%s\"", attribute, value), target);
}

// Remove the given attribute of the given target. Apply changes immediately.
public void removeAttribute(Path bazelBuildFile, String target, String attribute)
throws IOException {
execute(bazelBuildFile, String.format("remove %s", attribute), target);
}

// Add the value to the given list attribute of the given target. Apply changes
// immediately.
public void addAttribute(Path bazelBuildFile, String target, String attribute, String value)
throws IOException {
execute(bazelBuildFile, String.format("add %s \"%s\"", attribute, value), target);
}

// Set the value to the given attribute of the given target.
// The changes will be applied when the whole batch is committed with .commit().
public void batchSetAttribute(Path bazelBuildFile, String target, String attribute, String value)
throws IOException {
batch.add(
String.format("set %s \"%s\"|%s:%s", attribute, value, bazelBuildFile.toString(), target));
String.format(
"set %s \"%s\"|%s:%s",
attribute,
value.replace(" ", "\\ "),
bazelBuildFile.toString(), target));
}

// Remove the given attribute of the given target. Apply changes immediately.
public void batchRemoveAttribute(Path bazelBuildFile, String target, String attribute)
throws IOException {
batch.add(String.format("remove %s|%s:%s", attribute, bazelBuildFile.toString(), target));
batch.add(
String.format("remove %s|%s:%s",
attribute,
bazelBuildFile.toString(),
target));
}

// Add the value to the given list attribute of the given target.
// The changes will be applied when the whole batch is committed with .commit().
public void batchAddAttribute(Path bazelBuildFile, String target, String attribute, String value)
throws IOException {
batch.add(
String.format("add %s \"%s\"|%s:%s", attribute, value, bazelBuildFile.toString(), target));
String.format(
"add %s \"%s\"|%s:%s",
attribute,
value.replace(" ", "\\ "),
bazelBuildFile.toString(), target));
}

// Make all changes that are waiting in the batch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public void testRegeneration() throws IOException, InterruptedException {
"renamed_csharp_rule");
buildozer.batchSetAttribute(
gapicBuildFilePath, "google-cloud-example-library-v1-java", "name", "renamed_java_rule");
buildozer.batchSetAttribute(
gapicBuildFilePath, "library_ruby_gapic", "ruby_cloud_title", "Title with spaces");

// The following values should NOT be preserved:
buildozer.batchSetAttribute(
Expand Down Expand Up @@ -147,6 +149,10 @@ public void testRegeneration() throws IOException, InterruptedException {
Assert.assertEquals(
"renamed_java_rule",
buildozer.getAttribute(gapicBuildFilePath, "%java_gapic_assembly_gradle_pkg", "name"));
Assert.assertEquals(
"Title with spaces",
buildozer.getAttribute(gapicBuildFilePath, "%ruby_cloud_gapic_library", "ruby_cloud_title"));

// Check that grpc_service_config value is not preserved:
Assert.assertEquals(
"library_example_grpc_service_config.json",
Expand Down Expand Up @@ -186,15 +192,17 @@ public void testBuildozer() throws IOException {
"[value1 value2]", buildozer.getAttribute(buildBazel, "rule2", "list_attr"));

// Set some attributes and get the result
buildozer.setAttribute(buildBazel, "rule1", "attr", "new_attr_value");
buildozer.addAttribute(buildBazel, "rule2", "list_attr", "value3");
buildozer.batchSetAttribute(buildBazel, "rule1", "attr", "new_attr_value");
buildozer.batchAddAttribute(buildBazel, "rule2", "list_attr", "value3");
buildozer.commit();
Assert.assertEquals("new_attr_value", buildozer.getAttribute(buildBazel, "rule1", "attr"));
Assert.assertEquals(
"[value1 value2 value3]", buildozer.getAttribute(buildBazel, "rule2", "list_attr"));

// Remove attribute
Assert.assertEquals("remove_a", buildozer.getAttribute(buildBazel, "rule1", "to_be_removed_a"));
buildozer.removeAttribute(buildBazel, "rule1", "to_be_removed_a");
buildozer.batchRemoveAttribute(buildBazel, "rule1", "to_be_removed_a");
buildozer.commit();
Assert.assertEquals(null, buildozer.getAttribute(buildBazel, "rule1", "to_be_removed_a"));

// Test batch operations
Expand Down

0 comments on commit ff1d244

Please sign in to comment.