Skip to content

Commit

Permalink
Fix issues with upgrade to Gradle 7.0 and Spock 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ghale committed Apr 15, 2021
1 parent ceb101c commit 950870d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
55 changes: 35 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,29 @@ repositories {
mavenCentral()
}

configurations {
groovyClasspath
}

dependencies {
def agpVersion = "3.5.4"
compileOnly "com.android.tools.build:gradle:$agpVersion"
def versions = [
'agp': '3.5.4',
'spock': '2.0-M5-groovy-3.0'
]

// Used for compiling the main classes
groovyClasspath 'org.codehaus.groovy:groovy-all:2.5.12'

compileOnly "com.android.tools.build:gradle:${versions.agp}"

implementation gradleApi()
testImplementation "com.android.tools.build:gradle:$agpVersion"

testImplementation gradleTestKit()
testImplementation "junit:junit:4.13.2"
testImplementation "org.spockframework:spock-core:2.0-M5-groovy-3.0@jar"
testImplementation "org.opentest4j:opentest4j:1.2.0"
testRuntimeOnly 'cglib:cglib-nodep:3.3.0'
testRuntimeOnly 'org.objenesis:objenesis:3.2'
testImplementation "com.android.tools.build:gradle:${versions.agp}"
testImplementation platform("org.spockframework:spock-bom:${versions.spock}")
testImplementation("org.spockframework:spock-core") { exclude group: 'org.codehaus.groovy' }
testImplementation("org.spockframework:spock-junit4") { exclude group: 'org.codehaus.groovy' }
testImplementation "org.junit.jupiter:junit-jupiter-api"
}

java {
Expand All @@ -49,8 +61,15 @@ java {
}
}

def generatedResources = "$buildDir/generated-resources/main"
// We do this only for the the main source set as the test source set needs to use
// Groovy 3 for Spock 2.0 compatibility. The main classes need to be compiled with
// Groovy 2 so that they will work with older versions of Gradle.
tasks.named('compileGroovy').configure {
groovyClasspath = configurations.groovyClasspath
}

// Generate a json file that contains the matrix of Gradle and AGP versions to test against.
def generatedResources = "$buildDir/generated-resources/main"
tasks.register('generateVersions') {
def outputFile = file("$generatedResources/versions.json")
inputs.property "version", version
Expand All @@ -70,6 +89,7 @@ sourceSets {
}
}

// Main plugin publishing metadata
gradlePlugin {
plugins {
androidCacheFixPlugin {
Expand All @@ -92,8 +112,9 @@ pluginBundle {
}
}

// A local repo we publish our library to for testing in order to workaround limitations
// in the TestKit plugin classpath.
def localRepo = file("$buildDir/local-repo")

publishing {
repositories {
maven {
Expand All @@ -115,22 +136,17 @@ tasks.register('generateTestTasksJson') {
}
}

// Configuration common to all test tasks
tasks.withType(Test).configureEach {
dependsOn publish
systemProperty "local.repo", localRepo.toURI()
useJUnitPlatform()
retry {
maxRetries = isCI ? 1 : 0
maxFailures = 20
}
}

tasks.named("test").configure {
useJUnit {
// The main test task runs everything not annotated with the MultiVersionTest category
excludeCategories 'org.gradle.android.MultiVersionTest'
}
}

// Generate a test task for each Android version and run the tests annotated with the MultiVersionTest category
supportedVersions.keySet().each { androidVersion ->
def versionSpecificTest = tasks.register(androidTestTaskName(androidVersion), Test) {
Expand All @@ -140,9 +156,7 @@ supportedVersions.keySet().each { androidVersion ->
javaLauncher = javaToolchains.launcherFor {
languageVersion = jdkVersionFor(androidVersion)
}
useJUnit {
includeCategories 'org.gradle.android.MultiVersionTest'
}

systemProperty 'org.gradle.android.testVersion', androidVersion
}

Expand All @@ -163,6 +177,7 @@ static def jdkVersionFor(String version) {
return JavaLanguageVersion.of(VersionNumber.parse(version) > VersionNumber.parse("7.0.0-alpha01") ? 11 : 8)
}

// A basic sanity check to run before running all test tasks
tasks.register("sanityCheck") {
dependsOn tasks.withType(CodeNarc), validatePlugins
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package org.gradle.android
import org.gradle.testkit.runner.BuildResult
import org.gradle.util.VersionNumber
import org.junit.Assume
import org.junit.experimental.categories.Category

@Category(MultiVersionTest)
@MultiVersionTest
class ConfigurationCachingTest extends AbstractTest {
private static final VersionNumber SUPPORTED_KOTLIN_VERSION = VersionNumber.parse("1.4.30")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.GradleVersion
import org.gradle.util.VersionNumber
import org.junit.experimental.categories.Category
import spock.lang.Unroll

import static org.gradle.android.Versions.android
Expand All @@ -14,7 +13,7 @@ import static org.gradle.testkit.runner.TaskOutcome.NO_SOURCE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE

@Category(MultiVersionTest)
@MultiVersionTest
class CrossVersionOutcomeAndRelocationTest extends AbstractTest {

@Unroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package org.gradle.android

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.junit.experimental.categories.Category
import spock.lang.Issue
import spock.lang.Unroll

@Category(MultiVersionTest)
@MultiVersionTest
class MergeJavaResourcesWorkaroundTest extends AbstractTest {
@Unroll
@Issue('https://github.com/gradle/android-cache-fix-gradle-plugin/issues/78')
Expand Down
13 changes: 10 additions & 3 deletions src/test/groovy/org/gradle/android/MultiVersionTest.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package org.gradle.android

import java.lang.annotation.ElementType
import java.lang.annotation.Inherited
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target

/**
* Represents tests that span multiple versions of Android Gradle Plugin and need to be executed
* with multiple versions of the JDK.
*/
interface MultiVersionTest {

}
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MultiVersionTest { }
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.VersionNumber
import org.junit.Assume
import org.junit.experimental.categories.Category
import spock.lang.Unroll

import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

@Category(MultiVersionTest)
@MultiVersionTest
class RoomSchemaLocationWorkaroundTest extends AbstractTest {
private static final String[] CLEAN_BUILD = ["clean", "testDebug", "testRelease", "assembleAndroidTest", "--build-cache", "--stacktrace"]
private static final List<String> ALL_PROJECTS = ["app", "library"]
Expand Down
3 changes: 1 addition & 2 deletions src/test/groovy/org/gradle/android/TaskAvoidanceTest.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.gradle.android

import org.junit.experimental.categories.Category
import spock.lang.Unroll

@Category(MultiVersionTest)
@MultiVersionTest
class TaskAvoidanceTest extends AbstractTest {
@Unroll
def "Source Tasks are avoided with #gradleVersion and Android plugin #androidVersion"() {
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/SpockConfig.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.gradle.android.MultiVersionTest

def testAndroidVersion = System.getProperty('org.gradle.android.testVersion')

runner {
if (testAndroidVersion) {
include MultiVersionTest
} else {
exclude MultiVersionTest
}
}

0 comments on commit 950870d

Please sign in to comment.