Skip to content

Commit

Permalink
Push AbstractProject functionality into TestKit.
Browse files Browse the repository at this point in the history
  • Loading branch information
autonomousapps committed Oct 27, 2023
1 parent 56b69c3 commit 78c7490
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 68 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* ```
* <PWD>/build/functionalTest/<ConcreteClassSimpleName>-<UUID>[-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"
}
}

0 comments on commit 78c7490

Please sign in to comment.