Introduce GradleProject class.
This allows to split concepts of TestProject(whole build and options) and actual Gradle project, which could be one of the main project subprojects. ^KT-45745 In Progress
This commit is contained in:
+3
-3
@@ -77,7 +77,7 @@ class BuildCacheIT : KGPBaseTest() {
|
||||
assertTasksPackedToCache(":compileKotlin")
|
||||
}
|
||||
|
||||
val sourceFile = projectPath.resolve("src/main/kotlin/helloWorld.kt")
|
||||
val sourceFile = kotlinSourcesDir().resolve("helloWorld.kt")
|
||||
val originalSource: String = sourceFile.readText()
|
||||
val modifiedSource: String = originalSource.replace(" and ", " + ")
|
||||
sourceFile.writeText(modifiedSource)
|
||||
@@ -187,7 +187,7 @@ class BuildCacheIT : KGPBaseTest() {
|
||||
}
|
||||
|
||||
// Change the return type of foo() from Int to String in foo.kt, and check that fooUsage.kt is recompiled as well:
|
||||
val fooKtSourceFile = secondProject.projectPath.resolve("src/main/kotlin/foo.kt")
|
||||
val fooKtSourceFile = secondProject.kotlinSourcesDir().resolve("foo.kt")
|
||||
fooKtSourceFile.modify { it.replace("Int = 1", "String = \"abc\"") }
|
||||
secondProject.build("assemble") {
|
||||
assertIncrementalCompilation(modifiedFiles = setOf(fooKtSourceFile))
|
||||
@@ -224,7 +224,7 @@ class BuildCacheIT : KGPBaseTest() {
|
||||
}
|
||||
|
||||
// Make changes to annotated class and check kapt tasks are re-executed
|
||||
val appClassKtSourceFile = secondProject.projectPath.resolve("app/src/main/kotlin/AppClass.kt")
|
||||
val appClassKtSourceFile = secondProject.subProject("app").kotlinSourcesDir().resolve("AppClass.kt")
|
||||
appClassKtSourceFile.modify {
|
||||
it.replace("val testVal: String = \"text\"", "val testVal: Int = 1")
|
||||
}
|
||||
|
||||
+7
-12
@@ -8,12 +8,9 @@ package org.jetbrains.kotlin.gradle
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.testbase.*
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import kotlin.io.path.ExperimentalPathApi
|
||||
import kotlin.io.path.appendText
|
||||
|
||||
@DisplayName("Tasks configuration avoidance")
|
||||
@SimpleGradlePluginTests
|
||||
@OptIn(ExperimentalPathApi::class)
|
||||
class ConfigurationAvoidanceIT : KGPBaseTest() {
|
||||
|
||||
@DisplayName("Unrelated tasks are not configured")
|
||||
@@ -24,7 +21,7 @@ class ConfigurationAvoidanceIT : KGPBaseTest() {
|
||||
val expensivelyConfiguredTaskName = "expensivelyConfiguredTask"
|
||||
|
||||
@Suppress("GroovyAssignabilityCheck")
|
||||
rootBuildGradle.appendText(
|
||||
buildGradle.append(
|
||||
//language=Groovy
|
||||
"""
|
||||
|
||||
@@ -48,10 +45,9 @@ class ConfigurationAvoidanceIT : KGPBaseTest() {
|
||||
) {
|
||||
|
||||
listOf("Android", "Test").forEach { subproject ->
|
||||
projectPath
|
||||
.resolve(subproject)
|
||||
.resolve("build.gradle")
|
||||
.appendText(
|
||||
subProject(subproject)
|
||||
.buildGradle
|
||||
.append(
|
||||
//language=Groovy
|
||||
"""
|
||||
|
||||
@@ -66,10 +62,9 @@ class ConfigurationAvoidanceIT : KGPBaseTest() {
|
||||
)
|
||||
}
|
||||
|
||||
projectPath
|
||||
.resolve("Lib")
|
||||
.resolve("build.gradle")
|
||||
.appendText(
|
||||
subProject("Lib")
|
||||
.buildGradle
|
||||
.append(
|
||||
//language=Groovy
|
||||
"""
|
||||
|
||||
|
||||
+3
-3
@@ -25,7 +25,7 @@ class JavaUpToDateIT : KGPBaseTest() {
|
||||
)
|
||||
}
|
||||
|
||||
projectPath.resolve("src/main/kotlin/foo/MainKotlinClass.kt").modify {
|
||||
kotlinSourcesDir().resolve("foo/MainKotlinClass.kt").modify {
|
||||
it.replace(
|
||||
"fun number(): Int = 0",
|
||||
"fun number(): Int = 1"
|
||||
@@ -52,7 +52,7 @@ class JavaUpToDateIT : KGPBaseTest() {
|
||||
)
|
||||
}
|
||||
|
||||
projectPath.resolve("src/main/kotlin/foo/MainKotlinClass.kt").modify { "\n$it" }
|
||||
kotlinSourcesDir().resolve("foo/MainKotlinClass.kt").modify { "\n$it" }
|
||||
|
||||
build("build") {
|
||||
assertTasksExecuted(":compileKotlin", ":compileTestKotlin")
|
||||
@@ -74,7 +74,7 @@ class JavaUpToDateIT : KGPBaseTest() {
|
||||
)
|
||||
}
|
||||
|
||||
projectPath.resolve("src/main/kotlin/foo/MainKotlinClass.kt").modify {
|
||||
kotlinSourcesDir().resolve("foo/MainKotlinClass.kt").modify {
|
||||
it.replace(
|
||||
"private fun privateMethod() = 0",
|
||||
"private fun privateMethod() = \"0\""
|
||||
|
||||
+6
-6
@@ -131,7 +131,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
|
||||
JavaVersion.VERSION_1_8
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
tasks
|
||||
@@ -168,7 +168,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
gradleVersion = gradleVersion
|
||||
) {
|
||||
//language=groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
java {
|
||||
@@ -208,7 +208,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
projectPath.resolve("src/main/kotlin").toFile().deleteRecursively()
|
||||
kotlinSourcesDir().toFile().deleteRecursively()
|
||||
|
||||
buildAndFail("assemble") {
|
||||
assertOutputContains(
|
||||
@@ -269,7 +269,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
)
|
||||
}
|
||||
|
||||
projectPath.resolve("src/main/java/demo").run {
|
||||
javaSourcesDir().resolve("demo").run {
|
||||
createDirectories()
|
||||
resolve("HelloWorld.java").writeText(
|
||||
//language=Java
|
||||
@@ -297,7 +297,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
target: JavaVersion
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
tasks.withType(JavaCompile.class).configureEach {
|
||||
@@ -313,7 +313,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
jdkVersion: Int
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
import org.jetbrains.kotlin.gradle.tasks.UsesKotlinJavaToolchain
|
||||
|
||||
|
||||
+3
-4
@@ -12,7 +12,6 @@ import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.testbase.*
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@DaemonsGradlePluginTests
|
||||
@DisplayName("Kotlin daemon JVM args")
|
||||
@@ -92,7 +91,7 @@ class KotlinDaemonJvmArgsTest : KGPBaseTest() {
|
||||
)
|
||||
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
kotlin {
|
||||
@@ -125,7 +124,7 @@ class KotlinDaemonJvmArgsTest : KGPBaseTest() {
|
||||
)
|
||||
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
import org.jetbrains.kotlin.gradle.tasks.CompileUsingKotlinDaemon
|
||||
@@ -162,7 +161,7 @@ class KotlinDaemonJvmArgsTest : KGPBaseTest() {
|
||||
)
|
||||
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
kotlin {
|
||||
|
||||
+9
-9
@@ -312,7 +312,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
gradleVersion = gradleVersion
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
|
||||
|
||||
@@ -464,7 +464,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
gradleVersion = gradleVersion
|
||||
) {
|
||||
//language=groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
java {
|
||||
@@ -498,7 +498,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
useToolchainToCompile(11)
|
||||
|
||||
//language=groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
@@ -568,7 +568,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
buildJdk = getJdk11().javaHome
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
tasks.named("compileKotlin") {
|
||||
doLast {
|
||||
@@ -659,7 +659,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
jvmTarget: String
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
@@ -676,7 +676,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
target: JavaVersion
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
tasks.withType(JavaCompile.class).configureEach {
|
||||
@@ -693,7 +693,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
jdkVersion: JavaVersion
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
import org.gradle.api.JavaVersion
|
||||
import org.jetbrains.kotlin.gradle.tasks.UsesKotlinJavaToolchain
|
||||
@@ -714,7 +714,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
jdkVersion: Int
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
import org.jetbrains.kotlin.gradle.tasks.UsesKotlinJavaToolchain
|
||||
|
||||
@@ -742,7 +742,7 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
jdkVersion: Int
|
||||
) {
|
||||
//language=Groovy
|
||||
rootBuildGradle.append(
|
||||
buildGradle.append(
|
||||
"""
|
||||
import org.gradle.api.plugins.JavaPluginExtension
|
||||
import org.gradle.jvm.toolchain.JavaLanguageVersion
|
||||
|
||||
+1
-1
@@ -121,7 +121,7 @@ class SimpleKotlinGradleIT : KGPBaseTest() {
|
||||
project("kotlinProject", gradleVersion) {
|
||||
// Change the build directory in the end of the build script:
|
||||
val customBuildDirName = "customBuild"
|
||||
appendToBuildFile(
|
||||
buildGradle.append(
|
||||
"buildDir = '$customBuildDirName'"
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ class SourceFileIsModifiedIT : KGPBaseTest() {
|
||||
assertTasksNoSource(":compileTestKotlin")
|
||||
}
|
||||
|
||||
val dummyFile = projectPath.resolve("src/main/java/kotlinPackage/Dummy.kt")
|
||||
val dummyFile = javaSourcesDir().resolve("kotlinPackage/Dummy.kt")
|
||||
transformDummy(dummyFile)
|
||||
|
||||
build("build", buildOptions = buildOptions) {
|
||||
|
||||
+4
-4
@@ -11,7 +11,7 @@ import java.nio.file.Path
|
||||
/**
|
||||
* Asserts file under [file] path exists and is a regular file.
|
||||
*/
|
||||
fun TestProject.assertFileExists(
|
||||
fun GradleProject.assertFileExists(
|
||||
file: Path
|
||||
) {
|
||||
assert(Files.exists(file)) {
|
||||
@@ -26,7 +26,7 @@ fun TestProject.assertFileExists(
|
||||
/**
|
||||
* Asserts file under [pathToFile] relative to the test project exists and is a regular file.
|
||||
*/
|
||||
fun TestProject.assertFileInProjectExists(
|
||||
fun GradleProject.assertFileInProjectExists(
|
||||
pathToFile: String
|
||||
) {
|
||||
assertFileExists(projectPath.resolve(pathToFile))
|
||||
@@ -35,7 +35,7 @@ fun TestProject.assertFileInProjectExists(
|
||||
/**
|
||||
* Asserts file under [pathToFile] relative to the test project does not exist.
|
||||
*/
|
||||
fun TestProject.assertFileNotExists(
|
||||
fun GradleProject.assertFileNotExists(
|
||||
pathToFile: String
|
||||
) {
|
||||
val filePath = projectPath.resolve(pathToFile)
|
||||
@@ -47,7 +47,7 @@ fun TestProject.assertFileNotExists(
|
||||
/**
|
||||
* Asserts directory under [pathToDir] relative to the test project exists and is a directory.
|
||||
*/
|
||||
fun TestProject.assertDirectoryExists(
|
||||
fun GradleProject.assertDirectoryExists(
|
||||
pathToDir: String
|
||||
) {
|
||||
val dirPath = projectPath.resolve(pathToDir)
|
||||
|
||||
+28
-13
@@ -57,8 +57,8 @@ fun KGPBaseTest.project(
|
||||
val testProject = TestProject(
|
||||
gradleRunner,
|
||||
projectName,
|
||||
buildOptions,
|
||||
projectPath,
|
||||
buildOptions,
|
||||
gradleVersion,
|
||||
enableGradleDebug,
|
||||
forceOutput,
|
||||
@@ -151,20 +151,15 @@ fun TestProject.enableLocalBuildCache(
|
||||
)
|
||||
}
|
||||
|
||||
class TestProject(
|
||||
val gradleRunner: GradleRunner,
|
||||
open class GradleProject(
|
||||
val projectName: String,
|
||||
val buildOptions: KGPBaseTest.BuildOptions,
|
||||
val projectPath: Path,
|
||||
val gradleVersion: GradleVersion,
|
||||
val enableGradleDebug: Boolean,
|
||||
val forceOutput: Boolean,
|
||||
val enableBuildScan: Boolean
|
||||
val projectPath: Path
|
||||
) {
|
||||
val rootBuildGradle: Path get() = projectPath.resolve("build.gradle")
|
||||
val buildGradle: Path get() = projectPath.resolve("build.gradle")
|
||||
val buildGradleKts: Path get() = projectPath.resolve("build.gradle.kts")
|
||||
val settingsGradle: Path get() = projectPath.resolve("settings.gradle")
|
||||
val settingsGradleKts: Path get() = projectPath.resolve("settings.gradle.kts")
|
||||
val gradleProperties: Path get() = projectPath.resolve("gradle.properties")
|
||||
val localProperties: Path get() = projectPath.resolve("local.properties")
|
||||
|
||||
fun classesDir(
|
||||
sourceSet: String = "main",
|
||||
@@ -174,6 +169,27 @@ class TestProject(
|
||||
fun kotlinClassesDir(
|
||||
sourceSet: String = "main"
|
||||
): Path = classesDir(sourceSet, language = "kotlin")
|
||||
|
||||
fun kotlinSourcesDir(
|
||||
sourceSet: String = "main"
|
||||
): Path = projectPath.resolve("src/$sourceSet/kotlin")
|
||||
|
||||
fun javaSourcesDir(
|
||||
sourceSet: String = "main"
|
||||
): Path = projectPath.resolve("src/$sourceSet/java")
|
||||
}
|
||||
|
||||
class TestProject(
|
||||
val gradleRunner: GradleRunner,
|
||||
projectName: String,
|
||||
projectPath: Path,
|
||||
val buildOptions: KGPBaseTest.BuildOptions,
|
||||
val gradleVersion: GradleVersion,
|
||||
val enableGradleDebug: Boolean,
|
||||
val forceOutput: Boolean,
|
||||
val enableBuildScan: Boolean
|
||||
) : GradleProject(projectName, projectPath) {
|
||||
fun subProject(name: String) = GradleProject(name, projectPath.resolve(name))
|
||||
}
|
||||
|
||||
private fun commonBuildSetup(
|
||||
@@ -264,9 +280,8 @@ private fun Path.addDefaultBuildFiles() {
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalPathApi::class)
|
||||
private fun TestProject.agreeToBuildScanService() {
|
||||
settingsGradle.appendText(
|
||||
settingsGradle.append(
|
||||
"""
|
||||
|
||||
gradleEnterprise {
|
||||
|
||||
-9
@@ -34,12 +34,3 @@ fun Path.append(
|
||||
""".trimIndent()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Append [textToAppend] to the current [TestProject] 'build.gradle' file.
|
||||
*/
|
||||
fun TestProject.appendToBuildFile(
|
||||
textToAppend: String
|
||||
) {
|
||||
projectPath.resolve("build.gradle").append(textToAppend)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user