|
| 1 | +import org.gradle.api.plugins.jvm.JvmTestSuite |
| 2 | + |
| 3 | +fun Project.addTestSuiteExtendingForDir(testSuiteName: String, parentSuiteName: String, dirName: String) { |
| 4 | + testing { |
| 5 | + suites { |
| 6 | + create(testSuiteName, JvmTestSuite::class) { |
| 7 | + sources { |
| 8 | + java { |
| 9 | + srcDirs("src/$dirName/java") |
| 10 | + } |
| 11 | + resources { |
| 12 | + srcDirs("src/$dirName/resources") |
| 13 | + } |
| 14 | + if (project.plugins.hasPlugin("groovy")) { |
| 15 | + groovy { |
| 16 | + srcDirs("src/$dirName/groovy") |
| 17 | + } |
| 18 | + } |
| 19 | + if (project.plugins.hasPlugin("org.jetbrains.kotlin.jvm")) { |
| 20 | + kotlin { |
| 21 | + srcDirs("src/$dirName/kotlin") |
| 22 | + } |
| 23 | + } |
| 24 | + if (project.plugins.hasPlugin("scala")) { |
| 25 | + scala { |
| 26 | + srcDirs("src/$dirName/scala") |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + configurations { |
| 35 | + val extendConf = { suffix: String -> |
| 36 | + val config = named("${testSuiteName}${suffix}") |
| 37 | + val parentConfig = named("${parentSuiteName}${suffix}") |
| 38 | + if (parentConfig.isPresent) { |
| 39 | + config.configure { |
| 40 | + extendsFrom(parentConfig.get()) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (testSuiteName.matches(Regex(".*ForkedTest$"))) { |
| 45 | + val nonForkedBaseConfName = testSuiteName.replaceFirst("Forked", "") |
| 46 | + val nonForkedConfig = maybeCreate("${nonForkedBaseConfName}${suffix}") |
| 47 | + config.configure { |
| 48 | + extendsFrom(nonForkedConfig) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + extendConf("CompileOnly") |
| 54 | + extendConf("Implementation") |
| 55 | + extendConf("RuntimeOnly") |
| 56 | + extendConf("AnnotationProcessor") |
| 57 | + } |
| 58 | + |
| 59 | + tasks.register("${testSuiteName}Jar", Jar::class) { |
| 60 | + dependsOn(tasks.named("${testSuiteName}Classes")) |
| 61 | + from(sourceSets.named(testSuiteName).get().output) |
| 62 | + archiveClassifier.set(testSuiteName) |
| 63 | + } |
| 64 | + |
| 65 | + // The project dependency definition cannot sit inside previous blocks. As of Gradle 8.8, configurations cannot be mutated after they have been used as part of the dependency graph. |
| 66 | + // See: https://docs.gradle.org/current/userguide/upgrading_version_8.html#mutate_configuration_after_locking |
| 67 | + // And related issue: https://github.com/gradle/gradle/issues/28867 |
| 68 | + dependencies { |
| 69 | + add("${testSuiteName}Implementation", project(project.path)) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fun Project.addTestSuiteForDir(testSuiteName: String, dirName: String) { |
| 74 | + addTestSuiteExtendingForDir(testSuiteName, "test", dirName) |
| 75 | +} |
| 76 | + |
| 77 | +fun Project.addTestSuite(testSuiteName: String) { |
| 78 | + addTestSuiteForDir(testSuiteName, testSuiteName) |
| 79 | +} |
0 commit comments