Skip to content

Commit

Permalink
Update JacocoCoverageRunner
Browse files Browse the repository at this point in the history
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
  • Loading branch information
iirina committed Aug 14, 2017
1 parent 695320a commit 05418b3
Showing 1 changed file with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> 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");

Expand Down Expand Up @@ -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<String, File>() {
@Override
public File apply(String input) {
return new File(input);
}
}), File.class);
new JacocoCoverageRunner(true, dataInputStream, coverageReport, metadataJars)
.create();
File[] metadataJars;
if (isNewImplementation) {
List<String> metadataFiles = Files.readLines(new File(metadataFile), UTF_8);
List<File> 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);
Expand All @@ -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});
}
Expand Down

0 comments on commit 05418b3

Please sign in to comment.