Skip to content

Commit 4d28c11

Browse files
authored
Merge pull request #11 from build-extensions-oss/rework-gradle-versions-test
Rework gradle versions test
2 parents 32d1a2d + af8bec7 commit 4d28c11

File tree

4 files changed

+105
-115
lines changed

4 files changed

+105
-115
lines changed

gradle-plugin-utils/build.gradle.kts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ plugins {
66
dependencies {
77
compileOnly(gradleApi())
88

9-
"testImplementation"(gradleApi())
10-
"testImplementation"(project(":gradle-plugin-test-utils"))
11-
"testImplementation"(libs.kotest.runner)
12-
"testImplementation"(libs.kotest.property)
13-
"testImplementation"(libs.assertk.core)
14-
"testImplementation"(libs.mockk)
9+
testImplementation(gradleApi())
10+
testImplementation(project(":gradle-plugin-test-utils"))
11+
testImplementation(libs.junit.jupiter.params)
12+
testImplementation(libs.kotest.runner)
13+
testImplementation(libs.kotest.property)
14+
testImplementation(libs.assertk.core)
15+
testImplementation(libs.mockk)
1516
}

gradle-plugin-utils/gradle.lockfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.7.0=testRuntimeClasspath
6363
org.jetbrains.kotlinx:kotlinx-coroutines-test-jvm:1.7.0=testRuntimeClasspath
6464
org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.0=testRuntimeClasspath
6565
org.jetbrains:annotations:23.0.0=testRuntimeClasspath
66-
org.junit.jupiter:junit-jupiter-api:5.8.2=testRuntimeClasspath
67-
org.junit.jupiter:junit-jupiter-engine:5.8.2=testRuntimeClasspath
68-
org.junit.jupiter:junit-jupiter-params:5.8.2=testRuntimeClasspath
69-
org.junit.jupiter:junit-jupiter:5.8.2=testRuntimeClasspath
70-
org.junit.platform:junit-platform-commons:1.8.2=testRuntimeClasspath
71-
org.junit.platform:junit-platform-engine:1.8.2=testRuntimeClasspath
72-
org.junit.platform:junit-platform-launcher:1.8.2=testRuntimeClasspath
73-
org.junit.platform:junit-platform-suite-api:1.8.2=testRuntimeClasspath
74-
org.junit:junit-bom:5.8.2=testRuntimeClasspath
66+
org.junit.jupiter:junit-jupiter-api:5.10.0=testRuntimeClasspath
67+
org.junit.jupiter:junit-jupiter-engine:5.10.0=testRuntimeClasspath
68+
org.junit.jupiter:junit-jupiter-params:5.10.0=testRuntimeClasspath
69+
org.junit.jupiter:junit-jupiter:5.10.0=testRuntimeClasspath
70+
org.junit.platform:junit-platform-commons:1.10.0=testRuntimeClasspath
71+
org.junit.platform:junit-platform-engine:1.10.0=testRuntimeClasspath
72+
org.junit.platform:junit-platform-launcher:1.10.0=testRuntimeClasspath
73+
org.junit.platform:junit-platform-suite-api:1.10.0=testRuntimeClasspath
74+
org.junit:junit-bom:5.10.0=testRuntimeClasspath
7575
org.objenesis:objenesis:3.3=testRuntimeClasspath
7676
org.opentest4j:opentest4j:1.3.0=testRuntimeClasspath
7777
org.reflections:reflections:0.10.2=testRuntimeClasspath
Lines changed: 88 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,126 @@
11
package org.unbrokendome.gradle.pluginutils
22

33
import assertk.assertThat
4-
import assertk.assertions.*
5-
import io.kotest.core.spec.style.DescribeSpec
6-
import io.kotest.data.Headers2
7-
import io.kotest.data.Row2
8-
import io.kotest.data.Table2
9-
import io.kotest.data.forAll
4+
import assertk.assertions.hasMessage
5+
import assertk.assertions.isEqualTo
6+
import assertk.assertions.isFailure
7+
import assertk.assertions.isInstanceOf
8+
import assertk.assertions.isNotNull
9+
import assertk.assertions.isSuccess
1010
import io.mockk.mockk
1111
import io.mockk.verify
1212
import org.gradle.util.GradleVersion
13+
import org.junit.jupiter.api.Test
14+
import org.junit.jupiter.params.ParameterizedTest
15+
import org.junit.jupiter.params.provider.MethodSource
1316
import kotlin.reflect.full.memberProperties
1417

18+
class GradleVersionsTest {
1519

16-
class GradleVersionsTest : DescribeSpec({
20+
data class VersionInput(val major: Int, val minor: Int)
1721

18-
describe("Version constant properties") {
22+
companion object {
23+
val veryHighVersion = GradleVersion.version("9999.99")!!
1924

20-
val allVersionProperties = GradleVersions::class.memberProperties.associateBy { it.name }
21-
22-
val rows = sequenceOf(
23-
4 to 0..10,
24-
5 to 0..6,
25-
6 to 0..8
26-
).flatMap { (majorVersion, minorRange) ->
27-
minorRange.asSequence()
28-
.map { minorVersion ->
29-
Row2(majorVersion, minorVersion)
30-
}
31-
}.toList()
32-
33-
val dataTable = Table2(Headers2("major", "minor"), rows)
34-
35-
forAll(dataTable) { major, minor ->
36-
37-
it("GradleVersions object should have a version property for $major.$minor") {
38-
39-
val versionPropertyName = "Version_${major}_${minor}"
40-
val version = GradleVersion.version("$major.$minor")
41-
42-
val versionProperty = allVersionProperties[versionPropertyName]
43-
44-
assertThat(versionProperty, name = "Version property").isNotNull()
45-
.transform { it.get(GradleVersions) }
46-
.isEqualTo(version)
25+
@JvmStatic
26+
fun versionsInput(): List<VersionInput> {
27+
return listOf(
28+
4 to 0..10,
29+
5 to 0..6,
30+
6 to 0..8
31+
).flatMap { (majorVersion, minorRange) ->
32+
minorRange
33+
.map { minorVersion ->
34+
VersionInput(majorVersion, minorVersion)
35+
}
4736
}
4837
}
4938
}
5039

40+
@ParameterizedTest
41+
@MethodSource("versionsInput")
42+
fun `GradleVersions object should have a version property`(input: VersionInput) {
43+
val allVersionProperties = GradleVersions::class.memberProperties.associateBy { it.name }
5144

52-
val veryHighVersion = GradleVersion.version("9999.99")
53-
54-
55-
describe("checkGradleVersion with message") {
45+
val versionPropertyName = "Version_${input.major}_${input.minor}"
46+
val version = GradleVersion.version("${input.major}.${input.minor}")
5647

57-
it("should return normally if the version is ok") {
58-
assertThat {
59-
checkGradleVersion(GradleVersions.Version_6_2) { "version too low" }
60-
}.isSuccess()
61-
}
48+
val versionProperty = allVersionProperties[versionPropertyName]
6249

63-
it("should throw an exception if the version is too low") {
64-
assertThat {
65-
checkGradleVersion(veryHighVersion) { "version too low" }
66-
}.isFailure()
67-
.isInstanceOf(IllegalStateException::class)
68-
.hasMessage("version too low")
69-
}
50+
assertThat(versionProperty, name = "Version property").isNotNull()
51+
.transform { it.get(GradleVersions) }
52+
.isEqualTo(version)
7053
}
7154

72-
73-
describe("checkGradleVersion with plugin ID") {
74-
75-
it("should return normally if the version is ok") {
76-
assertThat {
77-
checkGradleVersion(GradleVersions.Version_6_2, "test.plugin")
78-
}.isSuccess()
79-
}
80-
81-
it("should throw an exception if the version is too low") {
82-
assertThat {
83-
checkGradleVersion(veryHighVersion, "test.plugin")
84-
}.isFailure()
85-
.isInstanceOf(IllegalStateException::class)
86-
.hasMessage("The plugin \"test.plugin\" requires at least Gradle 9999.99")
87-
}
55+
@Test
56+
fun `checkGradleVersion with message should return normally if the version is ok`() {
57+
assertThat {
58+
checkGradleVersion(GradleVersions.Version_6_2) { "version too low" }
59+
}.isSuccess()
8860
}
8961

62+
@Test
63+
fun `should throw an exception if the version is too low`() {
64+
assertThat {
65+
checkGradleVersion(veryHighVersion) { "version too low" }
66+
}.isFailure()
67+
.isInstanceOf(IllegalStateException::class)
68+
.hasMessage("version too low")
69+
}
9070

91-
describe("withMinGradleVersion") {
92-
93-
it("should execute the block if the version is ok") {
94-
95-
val block = mockk<Runnable>(relaxed = true)
96-
97-
withMinGradleVersion(GradleVersions.Version_6_2, block::run)
98-
99-
verify(exactly = 1) { block.run() }
100-
}
101-
71+
@Test
72+
fun `checkGradleVersion with plugin ID should return normally if the version is ok`() {
73+
assertThat {
74+
checkGradleVersion(GradleVersions.Version_6_2, "test.plugin")
75+
}.isSuccess()
76+
}
10277

103-
it("should not execute the block if the version is too low") {
78+
@Test
79+
fun `checkGradleVersion with plugin ID should throw an exception if the version is too low`() {
80+
assertThat {
81+
checkGradleVersion(veryHighVersion, "test.plugin")
82+
}.isFailure()
83+
.isInstanceOf(IllegalStateException::class)
84+
.hasMessage("The plugin \"test.plugin\" requires at least Gradle 9999.99")
85+
}
10486

105-
val block = mockk<Runnable>(relaxed = true)
87+
@Test
88+
fun `withMinGradleVersion should execute the block if the version is ok`() {
89+
val block = mockk<Runnable>(relaxed = true)
10690

107-
withMinGradleVersion(veryHighVersion, block::run)
91+
withMinGradleVersion(GradleVersions.Version_6_2, block::run)
10892

109-
verify(exactly = 0) { block.run() }
110-
}
93+
verify(exactly = 1) { block.run() }
11194
}
11295

96+
@Test
97+
fun `withMinGradleVersion should not execute the block if the version is too low`() {
98+
val block = mockk<Runnable>(relaxed = true)
11399

114-
describe("withMinGradleVersion with fallback") {
115-
116-
it("should execute the block if the version is ok") {
100+
withMinGradleVersion(veryHighVersion, block::run)
117101

118-
val block = mockk<Runnable>("block", relaxed = true)
119-
val fallback = mockk<Runnable>("fallback", relaxed = true)
102+
verify(exactly = 0) { block.run() }
103+
}
120104

121-
withMinGradleVersion(GradleVersions.Version_6_2, block::run, fallback::run)
105+
@Test
106+
fun `withMinGradleVersion with fallback should execute the block if the version is ok`() {
107+
val block = mockk<Runnable>("block", relaxed = true)
108+
val fallback = mockk<Runnable>("fallback", relaxed = true)
122109

123-
verify(exactly = 1) { block.run() }
124-
verify(exactly = 0) { fallback.run() }
125-
}
110+
withMinGradleVersion(GradleVersions.Version_6_2, block::run, fallback::run)
126111

127-
it("should execute the fallback if the version is too low") {
112+
verify(exactly = 1) { block.run() }
113+
verify(exactly = 0) { fallback.run() }
114+
}
128115

129-
val block = mockk<Runnable>("block", relaxed = true)
130-
val fallback = mockk<Runnable>("fallback", relaxed = true)
116+
@Test
117+
fun `withMinGradleVersion with fallback should execute the fallback if the version is too low`() {
118+
val block = mockk<Runnable>("block", relaxed = true)
119+
val fallback = mockk<Runnable>("fallback", relaxed = true)
131120

132-
withMinGradleVersion(veryHighVersion, block::run, fallback::run)
121+
withMinGradleVersion(veryHighVersion, block::run, fallback::run)
133122

134-
verify(exactly = 0) { block.run() }
135-
verify(exactly = 1) { fallback.run() }
136-
}
123+
verify(exactly = 0) { block.run() }
124+
verify(exactly = 1) { fallback.run() }
137125
}
138-
})
126+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ spek2 = "2.0.19"
1010
assertk-core = { module = "com.willowtreeapps.assertk:assertk", version.ref = "assertk" }
1111

1212
junit-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit" }
13+
junit-jupiter-params= { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "junit" }
1314

1415
kotest-api = { module = "io.kotest:kotest-framework-api", version.ref = "kotest" }
1516
kotest-runner = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }

0 commit comments

Comments
 (0)