Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize testing frameworks and test fixtures #452

Merged
merged 50 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
722762a
Replace apply plugin with plugins { }
TWiStErRob Apr 4, 2023
3388c40
Modernise docs/debug and docs/examples projects.
TWiStErRob Apr 4, 2023
f620b81
Shorten the debug folder names
TWiStErRob Apr 4, 2023
39b61d1
Move repositories to settings in gradle/plugins
TWiStErRob Apr 4, 2023
acac51c
Move repositories to settings in fixtures
TWiStErRob Apr 4, 2023
722526f
Use generic assignment notation for fixtures
TWiStErRob Apr 4, 2023
71423cf
Use explicit plugin IDs
TWiStErRob Apr 4, 2023
5dc420a
Migrate some buildscript blocks
TWiStErRob Apr 4, 2023
5bb48fa
Remove hack, it works, I just misunderstood it.
TWiStErRob Apr 4, 2023
86a0e76
Repurpose hack for something useful.
TWiStErRob Apr 4, 2023
bbc4655
Make sure all relevant resources of the Gradle test fixtures are proc…
TWiStErRob Apr 4, 2023
842454d
Add ability to inject dependencies into the pluginManagement classpat…
TWiStErRob Apr 4, 2023
d979ef5
Merge plugins blocks instead of append.
TWiStErRob Apr 4, 2023
650585e
Move repositories to settings in fixtures
TWiStErRob Apr 4, 2023
5f95824
Merge everything!
TWiStErRob Apr 4, 2023
7d6eed2
Reduce code duplication in merging
TWiStErRob Apr 4, 2023
0230287
Fix classpaths of tests and some plugins block problems.
TWiStErRob Apr 4, 2023
5d9236d
Fix substitutions
TWiStErRob Apr 4, 2023
7e6fb11
Record TODOs
TWiStErRob Apr 5, 2023
16f950b
Not possible
TWiStErRob Apr 5, 2023
241e967
Added a note in Modernize plugin issue
TWiStErRob Apr 5, 2023
abe80ac
Clean up line endings in results
TWiStErRob Apr 5, 2023
8be7cec
Fix all tests
TWiStErRob Apr 5, 2023
1ae88e3
Fix some tests
TWiStErRob Apr 6, 2023
3db22d5
It works already
TWiStErRob Apr 6, 2023
528a10a
Remove unused buildscript blocks
TWiStErRob Apr 6, 2023
1558108
Code review
TWiStErRob Apr 6, 2023
fba4416
Move and rename enum
TWiStErRob Apr 6, 2023
767ec0c
Fix detekt and simplify
TWiStErRob Apr 6, 2023
3d65966
Remove unnecessary plugins
TWiStErRob Apr 6, 2023
565ffef
Replace lintOptions with lint
TWiStErRob Apr 6, 2023
acd51a3
Remove unnecessary plugins
TWiStErRob Apr 6, 2023
208a4f0
Replace targetSdkVersion and compileSdkVersion.
TWiStErRob Apr 6, 2023
d6d572b
Add support for imports
TWiStErRob Apr 6, 2023
8bf900c
Fix https://github.com/TWiStErRob/net.twisterrob.gradle/issues/437#is…
TWiStErRob Apr 6, 2023
3fa411c
Fix failing test
TWiStErRob Apr 6, 2023
1504e27
Replace !!.outcome with .assertX
TWiStErRob Apr 6, 2023
d4ac3f0
Replace lintOptions with lint
TWiStErRob Apr 6, 2023
f28174b
Increase timeout
TWiStErRob Apr 6, 2023
5b48002
Remove kotlin-test dependency
TWiStErRob Apr 6, 2023
6d8c81e
Replace kotlin.test.* with their Jupiter equivalents.
TWiStErRob Apr 6, 2023
fd88d72
Allow relocating temp directory for Gradle Test Kit
TWiStErRob Apr 6, 2023
5b18d56
Show test failure origin method on CI
TWiStErRob Apr 6, 2023
17357ea
Make tests pass on all AGP versions
TWiStErRob Apr 6, 2023
c7ef3eb
Hack memory 2
TWiStErRob Apr 6, 2023
a284ee5
Fix memory problems
TWiStErRob Apr 7, 2023
74adb10
Extract logic to separate function
TWiStErRob Apr 7, 2023
347fd33
Extract self-contained enum
TWiStErRob Apr 7, 2023
d5771e9
Fix detekt
TWiStErRob Apr 7, 2023
f2a026a
Fix copy-paste
TWiStErRob Apr 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
@Suppress("DSL_SCOPE_VIOLATION") // TODEL https://github.com/gradle/gradle/issues/22797
alias(libs.plugins.nexus)
id("net.twisterrob.gradle.build.module.root")
id("idea")
id("org.gradle.idea")
}

val projectVersion: String by project
Expand Down Expand Up @@ -109,6 +109,11 @@ subprojects {
properties["net.twisterrob.gradle.runner.clearAfterSuccess"] = "false"
properties["net.twisterrob.gradle.runner.clearAfterFailure"] = "false"
}
val tmpdir = project.property("net.twisterrob.test.java.io.tmpdir").toString()
if (tmpdir.isNotEmpty()) {
// Used in GradleTestKitDirRelocator.
properties["java.io.tmpdir"] = tmpdir
}
properties.forEach { (name, value) -> inputs.property(name, value) }
properties.forEach { (name, value) -> value?.let { jvmArgs("-D${name}=${value}") } }
}
Expand All @@ -121,15 +126,22 @@ subprojects {
)
val properties = propertyNamesToReplace.keysToMap { project.property(it) }
properties.forEach { (name, value) -> inputs.property(name, value) }
filesMatching(listOf("**/build.gradle", "**/settings.gradle")) {
val processedFiles = listOf(
"**/build.gradle",
"**/build.gradle.kts",
"**/settings.gradle",
"**/settings.gradle.kts",
"**/gradle.properties",
)
filesMatching(processedFiles) {
val replacements = properties + mapOf(
// custom replacements (`"name" to value`) would come here
)
filter(mapOf("tokens" to replacements), org.apache.tools.ant.filters.ReplaceTokens::class.java)
}
}

plugins.withId("kotlin") {
plugins.withId("org.jetbrains.kotlin.jvm") {
dependencies {
// Make sure we don't have many versions of Kotlin lying around.
add("compileOnly", enforcedPlatform(deps.kotlin.bom))
Expand All @@ -144,12 +156,10 @@ subprojects {
add("api", deps.kotlin.stdlib)
add("api", deps.kotlin.stdlib.jdk8)
add("api", deps.kotlin.reflect)

add("testImplementation", deps.kotlin.test)
}
}

plugins.withId("java") {
plugins.withId("org.gradle.java") {
val java = extensions.getByName<JavaPluginExtension>("java")
java.sourceCompatibility = JavaVersion.toVersion(deps.versions.java.get())
java.targetCompatibility = JavaVersion.toVersion(deps.versions.java.get())
Expand Down
3 changes: 3 additions & 0 deletions checkers/checkstyle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ dependencies {

testImplementation(projects.test.internal)
testImplementation(projects.compat.agpBase)
testInjectedPluginClasspath(libs.android.gradle) {
version { require(property("net.twisterrob.test.android.pluginVersion").toString()) }
}

testFixturesImplementation(projects.test.internal)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import net.twisterrob.gradle.checkstyle.test.CheckstyleTestResources
import net.twisterrob.gradle.common.TaskConfigurator
import net.twisterrob.gradle.test.GradleRunnerRule
import net.twisterrob.gradle.test.GradleRunnerRuleExtension
import net.twisterrob.gradle.test.assertFailed
import net.twisterrob.gradle.test.assertHasOutputLine
import net.twisterrob.gradle.test.assertNoOutputLine
import net.twisterrob.gradle.test.assertNoSource
import net.twisterrob.gradle.test.assertSuccess
import net.twisterrob.gradle.test.assertUpToDate
import net.twisterrob.gradle.test.failReason
import net.twisterrob.gradle.test.fixtures.ContentMergeMode
import net.twisterrob.gradle.test.minus
import net.twisterrob.gradle.test.runBuild
import net.twisterrob.gradle.test.runFailingBuild
Expand All @@ -24,7 +29,6 @@ import org.hamcrest.Matchers.startsWith
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import kotlin.test.assertEquals

/**
* @see CheckStylePlugin
Expand All @@ -38,12 +42,15 @@ class CheckStylePluginTest : BaseIntgTest() {
}

override lateinit var gradle: GradleRunnerRule

private val checkstyle = CheckstyleTestResources()

@Test fun `does not apply to empty project`() {
@Language("gradle")
val script = """
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
}
""".trimIndent()

val result = gradle.runFailingBuild {
Expand All @@ -56,8 +63,10 @@ class CheckStylePluginTest : BaseIntgTest() {
@Test fun `does not apply to a Java project`() {
@Language("gradle")
val script = """
apply plugin: 'java'
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
plugins {
id("org.gradle.java")
id("net.twisterrob.gradle.plugin.checkstyle")
}
""".trimIndent()

val result = gradle.runFailingBuild {
Expand All @@ -70,33 +79,39 @@ class CheckStylePluginTest : BaseIntgTest() {
@Test fun `applies without a hitch to an Android project`() {
@Language("gradle")
val script = """
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
}
""".trimIndent()

val result = gradle.runBuild {
basedOn("android-root_app")
run(script, "checkstyleEach")
}

assertEquals(TaskOutcome.UP_TO_DATE, result.task(":checkstyleEach")!!.outcome)
assertEquals(TaskOutcome.NO_SOURCE, result.task(":checkstyleDebug")!!.outcome)
assertEquals(TaskOutcome.NO_SOURCE, result.task(":checkstyleRelease")!!.outcome)
result.assertUpToDate(":checkstyleEach")
result.assertNoSource(":checkstyleDebug")
result.assertNoSource(":checkstyleRelease")
}

@Test fun `applies to all types of subprojects`() {
gradle.basedOn("android-all_kinds")
gradle.file(checkstyle.empty.config, "config", "checkstyle", "checkstyle.xml")
@Language("gradle")
val script = """
allprojects {
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
}
""".trimIndent()
// TODO add :dynamic-feature
val modules = arrayOf(":app", ":library", ":library:nested", ":test")
modules.forEach { modulePath ->
@Language("gradle")
val subProject = """
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
}
""".trimIndent()
val subPath = modulePath.substringAfter(':').split(":").toTypedArray()
gradle.file(subProject, ContentMergeMode.MERGE_GRADLE, *subPath, "build.gradle")
}

val result = gradle.runBuild {
basedOn("android-all_kinds")
run(script, "checkstyleEach")
run(null, "checkstyleEach")
}

val exceptions = arrayOf(
Expand Down Expand Up @@ -131,7 +146,9 @@ class CheckStylePluginTest : BaseIntgTest() {

@Language("gradle")
val subProject = """
apply plugin: 'com.android.library'
plugins {
id("com.android.library")
}
android.namespace = "project${modulePath.replace(":", ".")}"
""".trimIndent()

Expand All @@ -143,8 +160,11 @@ class CheckStylePluginTest : BaseIntgTest() {

@Language("gradle")
val rootProject = """
plugins {
id("net.twisterrob.gradle.plugin.checkstyle") apply false
}
allprojects {
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
apply plugin: "net.twisterrob.gradle.plugin.checkstyle"
}
""".trimIndent()

Expand All @@ -167,7 +187,6 @@ class CheckStylePluginTest : BaseIntgTest() {
}

@Test fun `applies to individual subprojects`() {

val modules = arrayOf(
":module1",
":module2",
Expand All @@ -184,13 +203,17 @@ class CheckStylePluginTest : BaseIntgTest() {
@Language("gradle")
val subProject = if (module in applyTo)
"""
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
apply plugin: 'com.android.library'
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
id("com.android.library")
}
android.namespace = "project${module.replace(":", ".")}"
""".trimIndent()
else
"""
apply plugin: 'com.android.library'
plugins {
id("com.android.library")
}
android.namespace = "project${module.replace(":", ".")}"
""".trimIndent()

Expand Down Expand Up @@ -227,7 +250,9 @@ class CheckStylePluginTest : BaseIntgTest() {

@Language("gradle")
val applyCheckstyle = """
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
}
tasks.withType(${Checkstyle::class.java.name}).configureEach {
// output all violations to the console so that we can parse the results
showViolations = true
Expand All @@ -238,7 +263,7 @@ class CheckStylePluginTest : BaseIntgTest() {
run(applyCheckstyle, ":checkstyleDebug")
}

assertEquals(TaskOutcome.FAILED, result.task(":checkstyleDebug")!!.outcome)
result.assertFailed(":checkstyleDebug")
assertThat(result.failReason, containsString("Checkstyle rule violations were found"))
result.assertHasOutputLine(Regex(""".*\[ERROR] src.main.java.Checkstyle\.java:1: .*? \[Header]"""))
}
Expand All @@ -250,7 +275,9 @@ class CheckStylePluginTest : BaseIntgTest() {

@Language("gradle")
val build = """
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
}
tasks.withType(${Checkstyle::class.java.name}).configureEach {
// output all violations to the console so that we can parse the results
showViolations = true
Expand All @@ -262,7 +289,7 @@ class CheckStylePluginTest : BaseIntgTest() {
run(build, ":checkstyleDebug")
}

assertEquals(TaskOutcome.FAILED, result.task(":checkstyleDebug")!!.outcome)
result.assertFailed(":checkstyleDebug")
assertThat(result.failReason, containsString("Checkstyle rule violations were found"))
result.assertHasOutputLine(Regex(""".*custom.Checkstyle\.java:1: .*? \[Header]"""))
}
Expand All @@ -279,7 +306,9 @@ class CheckStylePluginTest : BaseIntgTest() {

@Language("gradle")
val build = """
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
}
tasks.withType(${Checkstyle::class.java.name}).configureEach {
// output all violations to the console so that we can parse the results
showViolations = true
Expand All @@ -297,7 +326,7 @@ class CheckStylePluginTest : BaseIntgTest() {
run(build, ":checkstyleDebug")
}

assertEquals(TaskOutcome.FAILED, result.task(":checkstyleDebug")!!.outcome)
result.assertFailed(":checkstyleDebug")
assertThat(result.failReason, containsString("Checkstyle rule violations were found"))
result.assertHasOutputLine(Regex(""".*com.example.foo.Checkstyle\.java:1: .*? \[Header]"""))
result.assertNoOutputLine(Regex(""".*com.example.bar.Checkstyle\.java.*"""))
Expand All @@ -312,18 +341,26 @@ class CheckStylePluginTest : BaseIntgTest() {

@Language("gradle")
val applyCheckstyle = """
apply plugin: 'net.twisterrob.gradle.plugin.checkstyle'
plugins {
id("net.twisterrob.gradle.plugin.checkstyle")
}
""".trimIndent()

val result = gradle.runBuild {
run(applyCheckstyle, ":checkstyleDebug")
}

assertEquals(TaskOutcome.SUCCESS, result.task(":checkstyleDebug")!!.outcome)
result.assertSuccess(":checkstyleDebug")
}

@Test fun `applying by the old name is deprecated`() {
val result = gradle.run("apply plugin: 'net.twisterrob.checkstyle'").buildAndFail()
@Language("gradle")
val script = """
plugins {
id("net.twisterrob.checkstyle")
}
""".trimIndent()
val result = gradle.run(script).buildAndFail()
result.assertHasOutputLine(
Regex(
"""org\.gradle\.api\.GradleException: """ +
Expand Down
Loading