From a6a9843ddb869d911795bd9a634e725d5952dfb2 Mon Sep 17 00:00:00 2001 From: cushon Date: Thu, 26 Apr 2018 19:48:14 -0700 Subject: For --checkHashMismatch=ERROR, emit all errors instead of stopping at the first one PiperOrigin-RevId: 194491274 --- .../devtools/build/android/ZipFilterActionTest.java | 12 +++++------- .../google/devtools/build/android/ZipFilterAction.java | 10 +++++++--- .../devtools/build/android/ZipFilterEntryFilter.java | 17 ++++++++++++----- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/test/java/com/google/devtools/build/android/ZipFilterActionTest.java b/src/test/java/com/google/devtools/build/android/ZipFilterActionTest.java index 7af59aebb0..22ce6339b3 100644 --- a/src/test/java/com/google/devtools/build/android/ZipFilterActionTest.java +++ b/src/test/java/com/google/devtools/build/android/ZipFilterActionTest.java @@ -132,7 +132,7 @@ public class ZipFilterActionTest { private List outputEntriesWithArgs(ImmutableList args, File output) throws IOException { - ZipFilterAction.main(args.toArray(new String[0])); + ZipFilterAction.run(args.toArray(new String[0])); List filteredEntries = new ArrayList<>(); try (ZipFile zip = new ZipFile(output)) { Enumeration entries = zip.entries(); @@ -216,9 +216,8 @@ public class ZipFilterActionTest { callback.assertOp(FilterOperation.COPY); filter.accept("res/R.class", callback); callback.assertOp(FilterOperation.SKIP); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("name matches but the hash does not."); filter.accept("baz.class", callback); + assertThat(filter.sawErrors()).isTrue(); } @Test public void testFlags() throws Exception { @@ -240,7 +239,7 @@ public class ZipFilterActionTest { "--checkHashMismatch", "IGNORE"); thrown.expect(ZipException.class); thrown.expectMessage("Zip file 'filter1' is malformed"); - ZipFilterAction.main(args.toArray(new String[0])); + ZipFilterAction.run(args.toArray(new String[0])); } @Test public void testFullIntegration() throws IOException { @@ -283,9 +282,8 @@ public class ZipFilterActionTest { "ERROR", "--outputMode", "DONT_CARE"); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("name matches but the hash does not"); - ZipFilterAction.main(args.toArray(new String[0])); + int exitCode = ZipFilterAction.run(args.toArray(new String[0])); + assertThat(exitCode).isEqualTo(1); } @Test diff --git a/src/tools/android/java/com/google/devtools/build/android/ZipFilterAction.java b/src/tools/android/java/com/google/devtools/build/android/ZipFilterAction.java index c774ec22f6..0a0744746d 100644 --- a/src/tools/android/java/com/google/devtools/build/android/ZipFilterAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/ZipFilterAction.java @@ -28,7 +28,6 @@ import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Multimap; import com.google.devtools.build.singlejar.ZipCombiner; import com.google.devtools.build.singlejar.ZipCombiner.OutputMode; -import com.google.devtools.build.singlejar.ZipEntryFilter; import com.google.devtools.build.zip.ZipFileEntry; import com.google.devtools.build.zip.ZipReader; import java.io.IOException; @@ -67,7 +66,7 @@ import java.util.regex.Pattern; * --filterTypes [fileExtension[,fileExtension]...] * --explicitFilters [fileRegex[,fileRegex]...] * --outputMode [DONT_CARE|FORCE_DEFLATE|FORCE_STORED] - * --errorOnHashMismatch + * --checkHashMismatch [IGNORE|WARN|ERROR] * */ public class ZipFilterAction { @@ -213,6 +212,10 @@ public class ZipFilterAction { } public static void main(String[] args) throws IOException { + System.exit(run(args)); + } + + static int run(String[] args) throws IOException { Options options = new Options(); new JCommander(options).parse(args); logger.fine( @@ -241,7 +244,7 @@ public class ZipFilterAction { if (options.errorOnHashMismatch) { options.hashMismatchCheckMode = HashMismatchCheckMode.ERROR; } - ZipEntryFilter entryFilter = + ZipFilterEntryFilter entryFilter = new ZipFilterEntryFilter( explicitFilter, entriesToOmit, inputEntries.build(), options.hashMismatchCheckMode); @@ -250,5 +253,6 @@ public class ZipFilterAction { combiner.addZip(options.inputZip.toFile()); } logger.fine(String.format("Filtering completed in %dms", timer.elapsed(TimeUnit.MILLISECONDS))); + return entryFilter.sawErrors() ? 1 : 0; } } diff --git a/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java b/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java index 95733396ca..2e801c350f 100644 --- a/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java +++ b/src/tools/android/java/com/google/devtools/build/android/ZipFilterEntryFilter.java @@ -29,6 +29,11 @@ class ZipFilterEntryFilter implements ZipEntryFilter { private final Multimap entriesToOmit; private final Map inputEntries; private final HashMismatchCheckMode hashMismatchCheckMode; + private boolean sawErrors = false; + + public boolean sawErrors() { + return sawErrors; + } /** * Creates a new filter. @@ -62,11 +67,12 @@ class ZipFilterEntryFilter implements ZipEntryFilter { callback.skip(); } else { if (hashMismatchCheckMode == HashMismatchCheckMode.ERROR) { - throw new IllegalStateException( - String.format( - "Requested to filter entries of name " - + "'%s'; name matches but the hash does not. Aborting", - filename)); + System.out.printf( + "\u001b[31mERROR:\u001b[0m Requested to filter entries of name " + + "'%s'; name matches but the hash does not.\n", + filename); + sawErrors = true; + callback.skip(); } else { System.out.printf( "\u001b[35mWARNING:\u001b[0m Requested to filter entries of name " @@ -80,4 +86,5 @@ class ZipFilterEntryFilter implements ZipEntryFilter { callback.copy(null); } } + } -- cgit v1.2.3