diff --git a/src/com/facebook/buck/cli/AuditRulesCommand.java b/src/com/facebook/buck/cli/AuditRulesCommand.java index 4e39c83f941..241c37e86e5 100644 --- a/src/com/facebook/buck/cli/AuditRulesCommand.java +++ b/src/com/facebook/buck/cli/AuditRulesCommand.java @@ -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; @@ -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; @@ -163,13 +166,16 @@ public boolean isReadOnly() { private void printRulesToStdout( PrintStream stdOut, List> rawRules, Predicate includeType) throws IOException { - Iterable> filteredRules = - FluentIterable.from(rawRules) + ImmutableList> 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 rulesKeyedByName = new HashMap<>(); @@ -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); } @@ -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(": ") diff --git a/test/com/facebook/buck/cli/AuditRulesCommandIntegrationTest.java b/test/com/facebook/buck/cli/AuditRulesCommandIntegrationTest.java index a75519a663f..baf4434db7a 100644 --- a/test/com/facebook/buck/cli/AuditRulesCommandIntegrationTest.java +++ b/test/com/facebook/buck/cli/AuditRulesCommandIntegrationTest.java @@ -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"))); + } } diff --git a/test/com/facebook/buck/cli/testdata/audit_rules/stdout.all b/test/com/facebook/buck/cli/testdata/audit_rules/stdout.all index 239935a7bc7..31f556665ea 100644 --- a/test/com/facebook/buck/cli/testdata/audit_rules/stdout.all +++ b/test/com/facebook/buck/cli/testdata/audit_rules/stdout.all @@ -1,5 +1,11 @@ # example/BUCK +keystore( + name = "debug_keystore", + properties = "debug.keystore.properties", + store = "debug.keystore", +) + genrule( name = "example", bash = "cat $SRCS > $OUT", @@ -11,9 +17,3 @@ genrule( ], ) -keystore( - name = "debug_keystore", - properties = "debug.keystore.properties", - store = "debug.keystore", -) - diff --git a/test/com/facebook/buck/cli/testdata/audit_rules/stdout.sorted b/test/com/facebook/buck/cli/testdata/audit_rules/stdout.sorted new file mode 100644 index 00000000000..8a1c70de9c6 --- /dev/null +++ b/test/com/facebook/buck/cli/testdata/audit_rules/stdout.sorted @@ -0,0 +1,14 @@ +# unsorted/BUCK + +export_file( + name = "0.txt", +) + +export_file( + name = "a.txt", +) + +export_file( + name = "z.txt", +) + diff --git a/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/0.txt b/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/0.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/BUCK.fixture b/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/BUCK.fixture new file mode 100644 index 00000000000..88ab7a11305 --- /dev/null +++ b/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/BUCK.fixture @@ -0,0 +1,5 @@ +export_file(name = "z.txt") + +export_file(name = "0.txt") + +export_file(name = "a.txt") diff --git a/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/a.txt b/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/a.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/z.txt b/test/com/facebook/buck/cli/testdata/audit_rules/unsorted/z.txt new file mode 100644 index 00000000000..e69de29bb2d