Skip to content

Commit

Permalink
Add support for --experimental_java_coverage in android_local_test.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 218496779
  • Loading branch information
iirina authored and Copybara-Service committed Oct 24, 2018
1 parent 8c280bd commit 118c16b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,19 @@ protected ImmutableList<String> getJvmFlags(RuleContext ruleContext, String test
}

@Override
protected String getMainClass(
protected String addCoverageSupport(
RuleContext ruleContext,
JavaSemantics javaSemantics,
JavaCompilationHelper helper,
Artifact executable,
Artifact instrumentationMetadata,
JavaCompilationArtifacts.Builder javaArtifactsBuilder,
JavaTargetAttributes.Builder attributesBuilder)
throws InterruptedException, RuleErrorException {
JavaTargetAttributes.Builder attributesBuilder,
String mainClass)
throws RuleErrorException {
// coverage does not yet work with android_local_test
if (ruleContext.getConfiguration().isCodeCoverageEnabled()) {
ruleContext.throwWithRuleError("android_local_test does not yet support coverage");
}
return "com.google.testing.junit.runner.BazelTestRunner";
ruleContext.throwWithRuleError("android_local_test does not yet support coverage");
return "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public void checkForProtoLibraryAndJavaProtoLibraryOnSameProto(

private static final String JUNIT4_RUNNER = "org.junit.runner.JUnitCore";

@Override
public String getTestRunnerMainClass() {
return BAZEL_TEST_RUNNER_MAIN_CLASS;
}

@Nullable
private String getMainClassInternal(RuleContext ruleContext, ImmutableList<Artifact> sources) {
if (!ruleContext.attributes().get("create_executable", Type.BOOLEAN)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,19 @@ public ConfiguredTarget create(RuleContext ruleContext)
javaRuleOutputJarsProviderBuilder,
javaSourceJarsProviderBuilder);

String mainClass =
getMainClass(
ruleContext,
javaSemantics,
helper,
executable,
instrumentationMetadata,
javaArtifactsBuilder,
attributesBuilder);
String mainClass = javaSemantics.getTestRunnerMainClass();
String originalMainClass = mainClass;
if (ruleContext.getConfiguration().isCodeCoverageEnabled()) {
mainClass = addCoverageSupport(
ruleContext,
javaSemantics,
helper,
executable,
instrumentationMetadata,
javaArtifactsBuilder,
attributesBuilder,
mainClass);
}

// JavaCompilationHelper.getAttributes() builds the JavaTargetAttributes, after which the
// JavaTargetAttributes becomes immutable. This is an extra safety check to avoid inconsistent
Expand Down Expand Up @@ -259,6 +263,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
getJvmFlags(ruleContext, testClass),
executable,
mainClass,
originalMainClass,
filesToBuildBuilder,
javaExecutable);

Artifact oneVersionOutputArtifact = null;
Expand Down Expand Up @@ -550,15 +556,21 @@ private static NestedSet<Artifact> getLibraryResourceJars(RuleContext ruleContex
protected abstract ImmutableList<String> getJvmFlags(RuleContext ruleContext, String testClass)
throws RuleErrorException;

/** Return the testrunner main class */
protected abstract String getMainClass(
/**
* Enables coverage support for Android and Java targets: adds instrumented jar to the classpath
* and modifies main class.
*
* @return new main class
*/
protected abstract String addCoverageSupport(
RuleContext ruleContext,
JavaSemantics javaSemantics,
JavaCompilationHelper helper,
Artifact executable,
Artifact instrumentationMetadata,
JavaCompilationArtifacts.Builder javaArtifactsBuilder,
JavaTargetAttributes.Builder attributesBuilder)
JavaTargetAttributes.Builder attributesBuilder,
String mainClass)
throws InterruptedException, RuleErrorException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ void checkForProtoLibraryAndJavaProtoLibraryOnSameProto(
*/
ImmutableList<Artifact> collectResources(RuleContext ruleContext);

String getTestRunnerMainClass();

/**
* Constructs the command line to call SingleJar to join all artifacts from {@code classpath}
* (java code) and {@code resources} into {@code output}.
Expand Down

0 comments on commit 118c16b

Please sign in to comment.