From 05418b33dd87d63e2653e594d462b2aedb0e22e5 Mon Sep 17 00:00:00 2001 From: elenairina Date: Mon, 14 Aug 2017 11:16:44 +0200 Subject: [PATCH] Update JacocoCoverageRunner new blaze Java implementation sends the runtime jars encapsulated in a metadata txt file, instead of listing them in the environment variable. The previous approach led to blaze crashing with an "Argument list too long" error. PiperOrigin-RevId: 165155269 --- .../coverage/JacocoCoverageRunner.java | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/java_tools/junitrunner/java/com/google/testing/coverage/JacocoCoverageRunner.java b/src/java_tools/junitrunner/java/com/google/testing/coverage/JacocoCoverageRunner.java index f88e168f5e962c..166c114d47adb2 100644 --- a/src/java_tools/junitrunner/java/com/google/testing/coverage/JacocoCoverageRunner.java +++ b/src/java_tools/junitrunner/java/com/google/testing/coverage/JacocoCoverageRunner.java @@ -17,11 +17,9 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; -import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; +import com.google.common.io.Files; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; @@ -33,6 +31,7 @@ import java.io.Reader; import java.lang.reflect.Method; import java.net.URL; +import java.util.ArrayList; import java.util.Enumeration; import java.util.HashSet; import java.util.List; @@ -293,10 +292,9 @@ private static String getUniquePath(String pathTemplate, String suffix) throws I } public static void main(String[] args) throws Exception { - final String metadataJar = System.getenv("JACOCO_METADATA_JAR"); - String newMetadataJarsString = System.getenv("JACOCO_METADATA_JARS"); - final List newMetadataJars = - newMetadataJarsString == null ? null : Splitter.on(':').splitToList(newMetadataJarsString); + final String metadataFile = System.getenv("JACOCO_METADATA_JAR"); + final boolean isNewImplementation = metadataFile.endsWith(".txt"); + final String javaRunfilesRoot = System.getenv("JACOCO_JAVA_RUNFILES_ROOT"); final String coverageReportBase = System.getenv("JAVA_COVERAGE_FILE"); @@ -354,22 +352,20 @@ public void run() { dataInputStream = new ByteArrayInputStream(new byte[0]); } - if (metadataJar != null) { - // Disable coverage in this case. The build system should report an error or - // warning if this happens. It's too late at this point. - new JacocoCoverageRunner(dataInputStream, coverageReport, new File(metadataJar)) - .create(); - } else if (newMetadataJars != null){ - File[] metadataJars = Iterables.toArray( - Iterables.transform(newMetadataJars, new Function() { - @Override - public File apply(String input) { - return new File(input); - } - }), File.class); - new JacocoCoverageRunner(true, dataInputStream, coverageReport, metadataJars) - .create(); + File[] metadataJars; + if (isNewImplementation) { + List metadataFiles = Files.readLines(new File(metadataFile), UTF_8); + List convertedMetadataFiles = new ArrayList<>(); + for (String metadataFile : metadataFiles) { + convertedMetadataFiles.add(new File(javaRunfilesRoot + metadataFile)); + } + metadataJars = convertedMetadataFiles.toArray(new File[0]); + } else { + metadataJars = new File[] {new File(metadataFile)}; } + + new JacocoCoverageRunner( + isNewImplementation, dataInputStream, coverageReport, metadataJars).create(); } catch (IOException e) { e.printStackTrace(); Runtime.getRuntime().halt(1); @@ -384,7 +380,7 @@ public File apply(String input) { // We'd share the same limitation if the system under test uses shutdown hooks internally, as // there's no way to collect coverage data on that code. String mainClass = - newMetadataJars == null ? getMainClass(metadataJar) : System.getenv("JACOCO_MAIN_CLASS"); + isNewImplementation ? System.getenv("JACOCO_MAIN_CLASS") : getMainClass(metadataFile); Method main = Class.forName(mainClass).getMethod("main", String[].class); main.invoke(null, new Object[] {args}); }