diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidLocalTest.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidLocalTest.java index c191eb26615589..97d1a0712944eb 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidLocalTest.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidLocalTest.java @@ -70,20 +70,19 @@ protected ImmutableList 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 diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java index 863116be285e43..644705ad3da144 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java @@ -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 sources) { if (!ruleContext.attributes().get("create_executable", Type.BOOLEAN)) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java index de41ca368db3c8..528160fbb3ece5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java @@ -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 @@ -259,6 +263,8 @@ public ConfiguredTarget create(RuleContext ruleContext) getJvmFlags(ruleContext, testClass), executable, mainClass, + originalMainClass, + filesToBuildBuilder, javaExecutable); Artifact oneVersionOutputArtifact = null; @@ -550,15 +556,21 @@ private static NestedSet getLibraryResourceJars(RuleContext ruleContex protected abstract ImmutableList 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; /** diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java index 5397f119a6d68b..f2e444e4e4ab7f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSemantics.java @@ -277,6 +277,8 @@ void checkForProtoLibraryAndJavaProtoLibraryOnSameProto( */ ImmutableList 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}.