From 78c7490742bd6d19d7bc26e0982d24ef725a35f6 Mon Sep 17 00:00:00 2001 From: Tony Robalik Date: Thu, 26 Oct 2023 20:01:31 -0700 Subject: [PATCH] Push AbstractProject functionality into TestKit. --- build.gradle.kts | 3 +- .../AbstractFunctionalSpec.groovy | 18 ---------- .../com/autonomousapps/AbstractProject.groovy | 23 ++----------- .../android/AbstractAndroidSpec.groovy | 10 ------ .../autonomousapps/jvm/AbstractJvmSpec.groovy | 12 ------- .../com/autonomousapps/jvm/JvmSpec.groovy | 7 ---- .../kit/AbstractGradleProject.kt | 34 +++++++++++++++++++ 7 files changed, 39 insertions(+), 68 deletions(-) create mode 100644 testkit/src/main/kotlin/com/autonomousapps/kit/AbstractGradleProject.kt diff --git a/build.gradle.kts b/build.gradle.kts index c80e665c7..fa0434395 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -154,7 +154,8 @@ dependencies { // This might go away with Kotlin 2.0. functionalTestApi(project(":testkit")) functionalTestImplementation(project(":testkit-truth")) - functionalTestImplementation(libs.commons.io) { + + "smokeTestImplementation"(libs.commons.io) { because("For FileUtils.deleteDirectory()") } } diff --git a/src/functionalTest/groovy/com/autonomousapps/AbstractFunctionalSpec.groovy b/src/functionalTest/groovy/com/autonomousapps/AbstractFunctionalSpec.groovy index cec4dbf95..3a502bedc 100644 --- a/src/functionalTest/groovy/com/autonomousapps/AbstractFunctionalSpec.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/AbstractFunctionalSpec.groovy @@ -1,16 +1,12 @@ package com.autonomousapps -import com.autonomousapps.fixtures.ProjectDirProvider import com.autonomousapps.internal.GradleVersions import com.autonomousapps.internal.android.AgpVersion import com.autonomousapps.kit.GradleProject import com.autonomousapps.model.ProjectAdvice -import org.apache.commons.io.FileUtils import org.gradle.util.GradleVersion import spock.lang.Specification -import static com.autonomousapps.utils.DebugAware.debug - abstract class AbstractFunctionalSpec extends Specification { protected static final GRADLE_7_5 = GradleVersion.version('7.5.1') @@ -34,20 +30,6 @@ abstract class AbstractFunctionalSpec extends Specification { return System.getProperty('com.autonomousapps.quick').toBoolean() } - protected static void clean(ProjectDirProvider projectDirProvider) { - clean(projectDirProvider.projectDir) - } - - protected static void clean(File rootDir) { - if (!isDebug()) { - try { - FileUtils.deleteDirectory(rootDir) - } catch (FileNotFoundException e) { - println("FileNotFoundException: ${e.localizedMessage}") - } - } - } - ProjectAdvice actualProjectAdvice(String projectName) { return AdviceHelper.actualProjectAdviceForProject(gradleProject, projectName) } diff --git a/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy b/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy index 3442ce737..e8dae71fa 100644 --- a/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/AbstractProject.groovy @@ -1,36 +1,19 @@ package com.autonomousapps +import com.autonomousapps.kit.AbstractGradleProject import com.autonomousapps.kit.GradleProject import com.autonomousapps.kit.gradle.Plugin @SuppressWarnings('GrMethodMayBeStatic') -abstract class AbstractProject { +abstract class AbstractProject extends AbstractGradleProject { - private String className = getClass().simpleName protected final androidAppPlugin = [Plugin.androidAppPlugin] protected final androidLibPlugin = [Plugin.androidLibPlugin] - protected GradleProject.Builder newGradleProjectBuilder() { - return new GradleProject.Builder(defaultFile(), GradleProject.DslKind.GROOVY) - } - protected GradleProject.Builder minimalAndroidProjectBuilder(String agpVersion) { return GradleProject.minimalAndroidProject( - defaultFile(), + rootDir.toFile(), agpVersion ) } - - private File defaultFile() { - return new File("build/functionalTest/${newSlug()}") - } - - // Very similar to what is in RootProject - private String newSlug() { - def worker = System.getProperty('org.gradle.test.worker') ?: '' - if (!worker.isEmpty()) { - worker = "-$worker" - } - return "$className-${UUID.randomUUID().toString().take(16)}$worker" - } } diff --git a/src/functionalTest/groovy/com/autonomousapps/android/AbstractAndroidSpec.groovy b/src/functionalTest/groovy/com/autonomousapps/android/AbstractAndroidSpec.groovy index bab38e75a..9dbbd4605 100644 --- a/src/functionalTest/groovy/com/autonomousapps/android/AbstractAndroidSpec.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/android/AbstractAndroidSpec.groovy @@ -9,16 +9,6 @@ abstract class AbstractAndroidSpec extends AbstractFunctionalSpec { protected ProjectDirProvider androidProject = null - @SuppressWarnings('unused') - def cleanup() { - if (androidProject != null) { - clean(androidProject) - } - if (gradleProject != null) { - clean(gradleProject.rootDir) - } - } - protected static final AGP_7_3 = AgpVersion.version('7.3.1') protected static final AGP_7_4 = AgpVersion.version('7.4.2') protected static final AGP_8_0 = AgpVersion.version('8.0.2') diff --git a/src/functionalTest/groovy/com/autonomousapps/jvm/AbstractJvmSpec.groovy b/src/functionalTest/groovy/com/autonomousapps/jvm/AbstractJvmSpec.groovy index d971949cb..e2321b084 100644 --- a/src/functionalTest/groovy/com/autonomousapps/jvm/AbstractJvmSpec.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/jvm/AbstractJvmSpec.groovy @@ -3,16 +3,4 @@ package com.autonomousapps.jvm import com.autonomousapps.AbstractFunctionalSpec abstract class AbstractJvmSpec extends AbstractFunctionalSpec { - - /** - * Set to `false` in a concrete class temporarily if you want to inspect the build output. - */ - protected boolean shouldClean = true - - @SuppressWarnings('unused') - def cleanup() { - if (gradleProject != null && shouldClean) { - clean(gradleProject.rootDir) - } - } } diff --git a/src/functionalTest/groovy/com/autonomousapps/jvm/JvmSpec.groovy b/src/functionalTest/groovy/com/autonomousapps/jvm/JvmSpec.groovy index b83babfe4..8da93ee99 100644 --- a/src/functionalTest/groovy/com/autonomousapps/jvm/JvmSpec.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/jvm/JvmSpec.groovy @@ -14,13 +14,6 @@ final class JvmSpec extends AbstractFunctionalSpec { private ProjectDirProvider javaLibraryProject = null - @SuppressWarnings('unused') - def cleanup() { - if (javaLibraryProject != null) { - clean(javaLibraryProject) - } - } - def "reports redundant kotlin-jvm and kapt plugins applied (#gradleVersion)"() { given: javaLibraryProject = new RedundantKotlinJvmAndKaptPluginsProject() diff --git a/testkit/src/main/kotlin/com/autonomousapps/kit/AbstractGradleProject.kt b/testkit/src/main/kotlin/com/autonomousapps/kit/AbstractGradleProject.kt new file mode 100644 index 000000000..c8b028032 --- /dev/null +++ b/testkit/src/main/kotlin/com/autonomousapps/kit/AbstractGradleProject.kt @@ -0,0 +1,34 @@ +package com.autonomousapps.kit + +import java.io.File +import java.nio.file.Path +import java.util.UUID +import kotlin.io.path.createDirectories + +/** + * Provides some common functionality for Gradle functional tests. + */ +public abstract class AbstractGradleProject @JvmOverloads constructor( + buildPath: String = "build/functionalTest", +) { + + protected fun newGradleProjectBuilder(): GradleProject.Builder { + return GradleProject.Builder(rootDir.toFile(), GradleProject.DslKind.GROOVY) + } + + /** + * The root directory of a Gradle build. The default value is + * ``` + * /build/functionalTest/-[-Gradle worker ID (if present)] + * ``` + */ + public val rootDir: Path = File("$buildPath/${newSlug()}").toPath().createDirectories() + + private fun newSlug(): String { + var worker = System.getProperty("org.gradle.test.worker", "") + if (worker.isNotEmpty()) { + worker = "-$worker" + } + return "${javaClass.simpleName}-${UUID.randomUUID().toString().take(8)}$worker" + } +}