Browse Source

[Gradle] Fix and simplify disabling assertions in test tasks (#123038) (#123969)

Fixes misleading assertion configuration for test tasks from the command
line. 

We support the following command line args:

1. `-Dtests.jvm.argline=-disableassertions`

2. `-Dtests.jvm.argline=-da`

3. `-Dtests.asserts=false`
Rene Groeschke 7 tháng trước cách đây
mục cha
commit
ec188b8297

+ 49 - 3
build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/ElasticsearchTestBasePluginFuncTest.groovy

@@ -14,9 +14,55 @@ import org.gradle.testkit.runner.TaskOutcome
 
 class ElasticsearchTestBasePluginFuncTest extends AbstractGradleFuncTest {
 
-    def setup() {
-        // see https://github.com/gradle/gradle/issues/24172
-        configurationCacheCompatible = false
+    def "can disable assertions via cmdline param"() {
+        given:
+        file("src/test/java/acme/SomeTests.java").text = """
+        public class SomeTests {
+            @org.junit.Test
+            public void testAsserts() {
+                assert false;
+            }
+        }
+        """
+        buildFile.text = """
+            plugins {
+             id 'java'
+             id 'elasticsearch.test-base'
+            }
+
+            repositories {
+                mavenCentral()
+            }
+
+            dependencies {
+                testImplementation 'junit:junit:4.12'
+            }
+        """
+
+        when:
+        def result = gradleRunner("test").buildAndFail()
+        then:
+        result.task(':test').outcome == TaskOutcome.FAILED
+
+        when:
+        result = gradleRunner("test", "-Dtests.asserts=false").build()
+        then:
+        result.task(':test').outcome == TaskOutcome.SUCCESS
+
+        when:
+        result = gradleRunner("test", "-Dtests.jvm.argline=-da").build()
+        then:
+        result.task(':test').outcome == TaskOutcome.SUCCESS
+
+        when:
+        result = gradleRunner("test", "-Dtests.jvm.argline=-disableassertions").build()
+        then:
+        result.task(':test').outcome == TaskOutcome.SUCCESS
+
+        when:
+        result = gradleRunner("test", "-Dtests.asserts=false", "-Dtests.jvm.argline=-da").build()
+        then:
+        result.task(':test').outcome == TaskOutcome.SUCCESS
     }
 
     def "can configure nonInputProperties for test tasks"() {

+ 8 - 3
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchTestBasePlugin.java

@@ -137,10 +137,15 @@ public abstract class ElasticsearchTestBasePlugin implements Plugin<Project> {
                 test.jvmArgs((Object[]) argline.split(" "));
             }
 
-            if (Util.getBooleanProperty("tests.asserts", true)) {
-                test.jvmArgs("-ea", "-esa");
+            // Check if "tests.asserts" is false or "tests.jvm.argline" contains the "-da" flag.
+            boolean disableAssertions = Util.getBooleanProperty("tests.asserts", true) == false
+                || (argline != null && (argline.contains("-da")))
+                || (argline != null && (argline.contains("-disableassertions")));
+
+            if (disableAssertions) {
+                System.out.println("disable assertions");
+                test.setEnableAssertions(false);
             }
-
             Map<String, String> sysprops = Map.of(
                 "java.awt.headless",
                 "true",