Skip to content

Commit

Permalink
Sort buck audit rules output
Browse files Browse the repository at this point in the history
Summary: It's hard to use buck audit rules for integration tests because it's not deterministic. Make it deterministic based on name

Reviewed By: ttsugriy

fbshipit-source-id: ab0b6be
  • Loading branch information
nataliejameson authored and facebook-github-bot committed Jun 6, 2018
1 parent a455983 commit a96c071
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/com/facebook/buck/cli/AuditRulesCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
import com.facebook.buck.util.MoreStrings;
import com.facebook.buck.util.json.ObjectMappers;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonGenerator.Feature;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand All @@ -43,10 +44,12 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.SortedSet;
import java.util.TreeSet;
Expand Down Expand Up @@ -163,13 +166,16 @@ public boolean isReadOnly() {
private void printRulesToStdout(
PrintStream stdOut, List<Map<String, Object>> rawRules, Predicate<String> includeType)
throws IOException {
Iterable<Map<String, Object>> filteredRules =
FluentIterable.from(rawRules)
ImmutableList<Map<String, Object>> filteredRules =
rawRules
.stream()
.filter(
rawRule -> {
String type = (String) rawRule.get(BuckPyFunction.TYPE_PROPERTY_NAME);
return includeType.test(type);
});
})
.sorted(Comparator.comparing(rule -> ((String) rule.getOrDefault("name", ""))))
.collect(ImmutableList.toImmutableList());

if (json) {
Map<String, Object> rulesKeyedByName = new HashMap<>();
Expand All @@ -182,7 +188,7 @@ private void printRulesToStdout(
// We create a new JsonGenerator that does not close the stream.
try (JsonGenerator generator =
ObjectMappers.createGenerator(stdOut)
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET)
.disable(Feature.AUTO_CLOSE_TARGET)
.useDefaultPrettyPrinter()) {
ObjectMappers.WRITER.writeValue(generator, rulesKeyedByName);
}
Expand Down Expand Up @@ -273,7 +279,7 @@ private static String createDisplayString(String indent, @Nullable Object value)
StringBuilder out = new StringBuilder("{\n");

String indentPlus1 = indent + INDENT;
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
out.append(indentPlus1)
.append(createDisplayString(indentPlus1, entry.getKey()))
.append(": ")
Expand Down
14 changes: 14 additions & 0 deletions test/com/facebook/buck/cli/AuditRulesCommandIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,18 @@ public void auditRulesRespectConfigs() throws IOException {
MoreStringsForTests.equalToIgnoringPlatformNewlines(
workspace.getFileContents("stdout.all")));
}

@Test
public void testBuckAuditRulesSortsBasedOnName() throws IOException {
ProjectWorkspace workspace =
TestDataHelper.createProjectWorkspaceForScenario(this, "audit_rules", tmp);
workspace.setUp();

ProcessResult result = workspace.runBuckCommand("audit", "rules", "unsorted/BUCK");
result.assertSuccess();
assertThat(
result.getStdout(),
MoreStringsForTests.equalToIgnoringPlatformNewlines(
workspace.getFileContents("stdout.sorted")));
}
}
12 changes: 6 additions & 6 deletions test/com/facebook/buck/cli/testdata/audit_rules/stdout.all
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# example/BUCK

keystore(
name = "debug_keystore",
properties = "debug.keystore.properties",
store = "debug.keystore",
)

genrule(
name = "example",
bash = "cat $SRCS > $OUT",
Expand All @@ -11,9 +17,3 @@ genrule(
],
)

keystore(
name = "debug_keystore",
properties = "debug.keystore.properties",
store = "debug.keystore",
)

14 changes: 14 additions & 0 deletions test/com/facebook/buck/cli/testdata/audit_rules/stdout.sorted
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# unsorted/BUCK

export_file(
name = "0.txt",
)

export_file(
name = "a.txt",
)

export_file(
name = "z.txt",
)

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export_file(name = "z.txt")

export_file(name = "0.txt")

export_file(name = "a.txt")
Empty file.
Empty file.

0 comments on commit a96c071

Please sign in to comment.