Move test configuration into plugin to have it run on all test tasks

Bug: b/270105162
Change-Id: I569ecf9665fa6828a17e890d09e5260df36ede9b
diff --git a/d8_r8/commonBuildSrc/src/main/kotlin/DependenciesPlugin.kt b/d8_r8/commonBuildSrc/src/main/kotlin/DependenciesPlugin.kt
index e11a8d9..55cb8df 100644
--- a/d8_r8/commonBuildSrc/src/main/kotlin/DependenciesPlugin.kt
+++ b/d8_r8/commonBuildSrc/src/main/kotlin/DependenciesPlugin.kt
@@ -14,6 +14,7 @@
 import org.gradle.api.plugins.JavaPluginExtension
 import org.gradle.api.tasks.JavaExec
 import org.gradle.api.tasks.SourceSet
+import org.gradle.api.tasks.testing.Test
 import org.gradle.jvm.tasks.Jar
 import org.gradle.kotlin.dsl.register
 import org.gradle.nativeplatform.platform.OperatingSystem
@@ -29,6 +30,12 @@
     val repositories = target.getRepositories()
     repositories.maven { name = "LOCAL_MAVEN_REPO";  url = URI(dependenciesPath) }
     repositories.maven { name = "LOCAL_MAVEN_REPO_NEW";  url = URI(dependenciesNewPath) }
+
+    // Setup all test tasks to listen after system properties passed in by test.py.
+    val testTask = target.tasks.findByName("test")
+    if (testTask != null) {
+      TestConfigurationHelper.setupTestTask(testTask as Test)
+    }
   }
 
   companion object {
@@ -543,7 +550,6 @@
 }
 
 fun getThirdPartyProguards() : List<ThirdPartyDependency> {
-  val os: OperatingSystem = DefaultNativePlatform.getCurrentOperatingSystem()
   return listOf("proguard5.2.1", "proguard6.0.1", "proguard-7.0.0")
     .map { ThirdPartyDependency(
       it,
diff --git a/d8_r8/commonBuildSrc/src/main/kotlin/TestConfigurationHelper.kt b/d8_r8/commonBuildSrc/src/main/kotlin/TestConfigurationHelper.kt
new file mode 100644
index 0000000..308bf71
--- /dev/null
+++ b/d8_r8/commonBuildSrc/src/main/kotlin/TestConfigurationHelper.kt
@@ -0,0 +1,87 @@
+// Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import org.gradle.api.tasks.testing.Test
+
+class TestConfigurationHelper {
+
+  companion object {
+
+    fun setupTestTask(test: Test) {
+      val project = test.project
+      test.systemProperty("USE_NEW_GRADLE_SETUP", "true")
+      if (project.hasProperty("kotlin_compiler_dev")) {
+        test.systemProperty("com.android.tools.r8.kotlincompilerdev", "1")
+      }
+
+      if (project.hasProperty("kotlin_compiler_old")) {
+        test.systemProperty("com.android.tools.r8.kotlincompilerold", "1")
+      }
+
+      if (project.hasProperty("dex_vm")
+          && project.property("dex_vm") != "default") {
+        println("NOTE: Running with non default vm: " + project.property("dex_vm"))
+        test.systemProperty("dex_vm", project.property("dex_vm")!!)
+      }
+
+      // Forward runtime configurations for test parameters.
+      if (project.hasProperty("runtimes")) {
+        println("NOTE: Running with runtimes: " + project.property("runtimes"))
+        test.systemProperty("runtimes", project.property("runtimes")!!)
+      }
+
+      if (project.hasProperty("art_profile_rewriting_completeness_check")) {
+        test.systemProperty(
+          "com.android.tools.r8.artprofilerewritingcompletenesscheck",
+          project.property("art_profile_rewriting_completeness_check")!!)
+      }
+
+      if (project.hasProperty("disable_assertions")) {
+        test.enableAssertions = false
+      }
+
+      // Forward project properties into system properties.
+      listOf(
+        "slow_tests",
+        "desugar_jdk_json_dir",
+        "desugar_jdk_libs",
+        "test_dir",
+        "command_cache_dir").forEach {
+        if (project.hasProperty(it)) {
+          project.property(it)?.let { v -> test.systemProperty("slow_tests", v) }
+        }
+      }
+
+      if (project.hasProperty("no_internal")) {
+        test.exclude("com/android/tools/r8/internal/**")
+      }
+      if (project.hasProperty("only_internal")) {
+        test.include("com/android/tools/r8/internal/**")
+      }
+      if (project.hasProperty("no_arttests")) {
+        test.exclude("com/android/tools/r8/art/**")
+      }
+
+      if (project.hasProperty("test_xmx")) {
+        test.maxHeapSize = project.property("test_xmx")!!.toString()
+      } else {
+        test.maxHeapSize = "4G"
+      }
+
+      val userDefinedCoresPerFork = System.getenv("R8_GRADLE_CORES_PER_FORK")
+      val processors = Runtime.getRuntime().availableProcessors()
+      // See https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html.
+      if (userDefinedCoresPerFork != null) {
+        test.maxParallelForks = processors.div(userDefinedCoresPerFork.toInt())
+      } else {
+        // On work machines this seems to give the best test execution time (without freezing).
+        test.maxParallelForks = processors.div(3)
+        // On low cpu count machines (bots) we under subscribe, so increase the count.
+        if (processors == 8) {
+          test.maxParallelForks = 3
+        }
+      }
+    }
+  }
+}
diff --git a/d8_r8/test/build.gradle.kts b/d8_r8/test/build.gradle.kts
index 3875f50..9040c8e 100644
--- a/d8_r8/test/build.gradle.kts
+++ b/d8_r8/test/build.gradle.kts
@@ -73,61 +73,13 @@
   }
 
   withType<Test> {
-    systemProperty("USE_NEW_GRADLE_SETUP", "true")
-    dependsOn(gradle.includedBuild("tests_java_8").task(":test"))
-
-    if (project.hasProperty("kotlin_compiler_dev")) {
-      systemProperty("com.android.tools.r8.kotlincompilerdev", "1")
-    }
-
-    if (project.hasProperty("kotlin_compiler_old")) {
-      systemProperty("com.android.tools.r8.kotlincompilerold", "1")
-    }
-
-    if (project.hasProperty("dex_vm") && project.property("dex_vm") != "default") {
-      println("NOTE: Running with non default vm: " + project.property("dex_vm"))
-      systemProperty("dex_vm", project.property("dex_vm")!!)
-    }
-
-    // Forward runtime configurations for test parameters.
-    if (project.hasProperty("runtimes")) {
-      println("NOTE: Running with runtimes: " + project.property("runtimes"))
-      systemProperty("runtimes", project.property("runtimes")!!)
-    }
-
-    if (project.hasProperty("art_profile_rewriting_completeness_check")) {
-      systemProperty(
-        "com.android.tools.r8.artprofilerewritingcompletenesscheck",
-        project.property("art_profile_rewriting_completeness_check")!!)
-    }
-
-    // Forward project properties into system properties.
-    listOf(
-      "slow_tests",
-      "desugar_jdk_json_dir",
-      "desugar_jdk_libs",
-      "test_dir",
-      "command_cache_dir").forEach {
-      if (project.hasProperty(it)) {
-        project.property(it)?.let { v -> systemProperty("slow_tests", v) }
-      }
-    }
-
-    if (project.hasProperty("no_internal")) {
-      exclude("com/android/tools/r8/internal/**")
-    }
-    if (project.hasProperty("only_internal")) {
-      include("com/android/tools/r8/internal/**")
-    }
-    if (project.hasProperty("no_arttests")) {
-      exclude("com/android/tools/r8/art/**")
-    }
-
+    println("NOTE: Number of processors " + Runtime.getRuntime().availableProcessors())
+    println("NOTE: Max parallel forks " + maxParallelForks)
     val os = DefaultNativePlatform.getCurrentOperatingSystem()
     if (os.isMacOsX) {
       logger.lifecycle(
         "WARNING: Testing in only partially supported on Mac OS. \n" +
-        "Art only runs on Linux and tests requiring Art runs in a Docker container, which must " +
+          "Art only runs on Linux and tests requiring Art runs in a Docker container, which must " +
           "be present. See tools/docker/README.md for details.")
     } else if (os.isWindows) {
       logger.lifecycle(
@@ -135,10 +87,11 @@
           "tests requiring Art will be skipped")
     } else if (!os.isLinux) {
       logger.log(
-        ERROR,
+        LogLevel.ERROR,
         "Testing in not supported on your platform. Testing is only fully supported on " +
           "Linux and partially supported on Mac OS and Windows. Art does not run on other " +
           "platforms.")
     }
+    dependsOn(gradle.includedBuild("tests_java_8").task(":test"))
   }
 }
\ No newline at end of file
diff --git a/d8_r8/test_modules/tests_java_8/build.gradle.kts b/d8_r8/test_modules/tests_java_8/build.gradle.kts
index 15a76f9..2e6a3f1 100644
--- a/d8_r8/test_modules/tests_java_8/build.gradle.kts
+++ b/d8_r8/test_modules/tests_java_8/build.gradle.kts
@@ -157,7 +157,9 @@
   withType<Test> {
     environment.put("USE_NEW_GRADLE_SETUP", "true")
     dependsOn(thirdPartyRuntimeDependenciesTask)
-    dependsOn(thirdPartyRuntimeInternalDependenciesTask)
+    if (!project.hasProperty("no_internal")) {
+      dependsOn(thirdPartyRuntimeInternalDependenciesTask)
+    }
     dependsOn(*sourceSetDependenciesTasks)
     println("NOTE: Number of processors " + Runtime.getRuntime().availableProcessors())
     val userDefinedCoresPerFork = System.getenv("R8_GRADLE_CORES_PER_FORK")
diff --git a/src/test/java/com/android/tools/r8/internal/D8FrameworkDeterministicTest.java b/src/test/java/com/android/tools/r8/internal/D8FrameworkDeterministicTest.java
index 0d6061a..1d06349 100644
--- a/src/test/java/com/android/tools/r8/internal/D8FrameworkDeterministicTest.java
+++ b/src/test/java/com/android/tools/r8/internal/D8FrameworkDeterministicTest.java
@@ -7,14 +7,17 @@
 import com.android.tools.r8.CompilationMode;
 import com.android.tools.r8.D8;
 import com.android.tools.r8.D8Command;
+import com.android.tools.r8.ToolHelper;
 import com.android.tools.r8.utils.AndroidApp;
 import com.android.tools.r8.utils.AndroidAppConsumers;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import org.junit.Test;
 
 public class D8FrameworkDeterministicTest extends CompilationTestBase {
   private static final int MIN_SDK = 24;
-  private static final String JAR = "third_party/framework/framework_160115954.jar";
+  private static final Path JAR =
+      Paths.get(ToolHelper.THIRD_PARTY_DIR).resolve("framework").resolve("framework_160115954.jar");
 
   private AndroidApp doRun(D8Command.Builder builder) throws CompilationFailedException {
     builder.setProgramConsumer(null);
@@ -27,7 +30,7 @@
   public void verifyDebugBuild() throws Exception {
     D8Command.Builder command =
         D8Command.builder()
-            .addProgramFiles(Paths.get(JAR))
+            .addProgramFiles(JAR)
             .setMode(CompilationMode.DEBUG)
             .setMinApiLevel(MIN_SDK);
     AndroidApp app1 = doRun(command);
@@ -39,7 +42,7 @@
   public void verifyReleaseBuild() throws Exception {
     D8Command.Builder command =
         D8Command.builder()
-            .addProgramFiles(Paths.get(JAR))
+            .addProgramFiles(JAR)
             .setMode(CompilationMode.RELEASE)
             .setMinApiLevel(MIN_SDK);
     AndroidApp app1 = doRun(command);