diff options
author | Dmitry Lomov <dslomov@google.com> | 2016-11-04 12:28:50 +0000 |
---|---|---|
committer | Laszlo Csomor <laszlocsomor@google.com> | 2016-11-04 13:02:01 +0000 |
commit | d34ed226e1ef69f9590cf63d7ae00345d4bd937f (patch) | |
tree | 517c55eb08be5d35d2266051e7d9131901829f38 /src/main/java/com/google/devtools | |
parent | 6a8424ff374b17706f2ac7fa77eaf1a03b75c538 (diff) |
Use main_class as specified in java_test even if use_testrunner is true.
--
MOS_MIGRATED_REVID=138180229
Diffstat (limited to 'src/main/java/com/google/devtools')
3 files changed, 23 insertions, 12 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java index 2dd4172ab2..857e83a362 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaRuleClasses.java @@ -299,12 +299,14 @@ public class BazelJavaRuleClasses { <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ .add(attr("jvm_flags", STRING_LIST)) /* <!-- #BLAZE_RULE($base_java_binary).ATTRIBUTE(use_testrunner) --> - Use the - <code>com.google.testing.junit.runner.BazelTestRunner</code> class as the - main entry point for a Java program. + Use the test runner (by default + <code>com.google.testing.junit.runner.BazelTestRunner</code>) class as the + main entry point for a Java program, and provide the test class + to the test runner as a value of <code>bazel.test_suite</code> + system property. You can use this to override the default - behavior, which is to use <code>BazelTestRunner</code> for + behavior, which is to use test runner for <code>java_test</code> rules, and not use it for <code>java_binary</code> rules. It is unlikely you will want to do this. One use is for <code>AllTest</code> @@ -312,6 +314,8 @@ public class BazelJavaRuleClasses { before running the tests, for example). The <code>AllTest</code> rule must be declared as a <code>java_binary</code>, but should still use the test runner as its main entry point. + + The name of a test runner class can be overriden with <code>main_class</code> attribute. <!-- #END_BLAZE_RULE.ATTRIBUTE --> */ .add(attr("use_testrunner", BOOLEAN).value(false)) /* <!-- #BLAZE_RULE($base_java_binary).ATTRIBUTE(main_class) --> 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 9fe7b81d71..0d7217f1a0 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 @@ -95,16 +95,27 @@ public class BazelJavaSemantics implements JavaSemantics { public void checkForProtoLibraryAndJavaProtoLibraryOnSameProto( RuleContext ruleContext, JavaCommon javaCommon) {} + private static final String JUNIT4_RUNNER = "org.junit.runner.JUnitCore"; + private String getMainClassInternal(RuleContext ruleContext, ImmutableList<Artifact> sources) { if (!ruleContext.attributes().get("create_executable", Type.BOOLEAN)) { return null; } - if (ruleContext.attributes().get("use_testrunner", Type.BOOLEAN) - && !useLegacyJavaTest(ruleContext)) { - return "com.google.testing.junit.runner.BazelTestRunner"; - } String mainClass = ruleContext.attributes().get("main_class", Type.STRING); + + // Legacy behavior for java_test rules: main_class defaulted to JUnit4 runner. + // TODO(dmarting): remove once we drop the legacy bazel java_test behavior. + if (mainClass.isEmpty() + && useLegacyJavaTest(ruleContext) + && "java_test".equals(ruleContext.getRule().getRuleClass())) { + mainClass = JUNIT4_RUNNER; + } + if (mainClass.isEmpty()) { + if (ruleContext.attributes().get("use_testrunner", Type.BOOLEAN) + && !useLegacyJavaTest(ruleContext)) { + return "com.google.testing.junit.runner.BazelTestRunner"; + } mainClass = JavaCommon.determinePrimaryClass(ruleContext, sources); } return mainClass; diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java index 0de34ae90c..fb990f448f 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java @@ -39,8 +39,6 @@ import com.google.devtools.build.lib.rules.java.Jvm; */ public final class BazelJavaTestRule implements RuleDefinition { - private static final String JUNIT4_RUNNER = "org.junit.runner.JUnitCore"; - @Override public RuleClass build(Builder builder, RuleDefinitionEnvironment env) { /* <!-- #BLAZE_RULE(java_test).IMPLICIT_OUTPUTS --> @@ -60,8 +58,6 @@ public final class BazelJavaTestRule implements RuleDefinition { .override(attr("stamp", TRISTATE).value(TriState.NO)) .override(attr("use_testrunner", BOOLEAN).value(true)) .override(attr(":java_launcher", LABEL).value(JavaSemantics.JAVA_LAUNCHER)) - // TODO(dmarting): remove once we drop the legacy bazel java_test behavior. - .override(attr("main_class", STRING).value(JUNIT4_RUNNER)) /* <!-- #BLAZE_RULE(java_test).ATTRIBUTE(test_class) --> The Java class to be loaded by the test runner.<br/> <p> |