Skip to content

Commit b049d97

Browse files
committed
add some auto-config for java-test-fixtures
1 parent 932e68c commit b049d97

File tree

3 files changed

+175
-9
lines changed

3 files changed

+175
-9
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package kotlinx.validation.test
2+
3+
import dev.adamko.kotlin.binary_compatibility_validator.test.utils.*
4+
import io.kotest.assertions.asClue
5+
import io.kotest.assertions.withClue
6+
import io.kotest.core.spec.style.FunSpec
7+
import io.kotest.engine.spec.tempdir
8+
import io.kotest.matchers.paths.shouldBeAFile
9+
import io.kotest.matchers.paths.shouldNotExist
10+
import io.kotest.matchers.shouldBe
11+
import java.nio.file.Path
12+
import kotlin.io.path.readText
13+
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
14+
15+
class JavaTestFixturesTest : FunSpec({
16+
17+
context("when a project has the java-test-fixtures plugin applied") {
18+
19+
context("and the testFixtures BCV target is enabled") {
20+
val project = createTestFixturesProject(bcvTestFixturesTargetEnabled = true)
21+
22+
test("expect :apiDump task passes") {
23+
project.runner.withArguments("apiDump").build {
24+
withClue(output) {
25+
task(":apiDump") shouldHaveOutcome SUCCESS
26+
}
27+
}
28+
}
29+
30+
test("expect :apiCheck task passes") {
31+
project.runner.withArguments("apiCheck").build {
32+
withClue(output) {
33+
task(":apiCheck") shouldHaveOutcome SUCCESS
34+
}
35+
}
36+
}
37+
38+
test("expect correct kotlinJvm api declaration is generated") {
39+
project.projectDir.resolve("api/kotlinJvm/java-test-fixtures-test.api").asClue {
40+
it.shouldBeAFile()
41+
it.readText().invariantNewlines() shouldBe /* language=TEXT */ """
42+
|public final class Hello {
43+
| public fun <init> (Ljava/lang/String;)V
44+
| public final fun greeting ()Ljava/lang/String;
45+
|}
46+
|
47+
|
48+
""".trimMargin()
49+
}
50+
}
51+
52+
test("expect correct testFixtures api declaration is generated") {
53+
project.projectDir.resolve("api/testFixtures/java-test-fixtures-test.api").asClue {
54+
it.shouldBeAFile()
55+
it.readText().invariantNewlines() shouldBe /* language=TEXT */ """
56+
|public final class HelloHelperKt {
57+
| public static final fun standardHello ()LHello;
58+
|}
59+
|
60+
|
61+
""".trimMargin()
62+
}
63+
}
64+
}
65+
context("and the testFixtures BCV target is disabled") {
66+
val project = createTestFixturesProject(bcvTestFixturesTargetEnabled = false)
67+
68+
test("expect :apiDump task passes") {
69+
project.runner.withArguments("apiDump").build {
70+
withClue(output) {
71+
task(":apiDump") shouldHaveOutcome SUCCESS
72+
}
73+
}
74+
}
75+
76+
test("expect :apiCheck task passes") {
77+
project.runner.withArguments("apiCheck").build {
78+
withClue(output) {
79+
task(":apiCheck") shouldHaveOutcome SUCCESS
80+
}
81+
}
82+
}
83+
84+
test("expect correct kotlinJvm api declaration is generated") {
85+
project.projectDir.resolve("api/java-test-fixtures-test.api").asClue {
86+
it.shouldBeAFile()
87+
it.readText().invariantNewlines() shouldBe /* language=TEXT */ """
88+
|public final class Hello {
89+
| public fun <init> (Ljava/lang/String;)V
90+
| public final fun greeting ()Ljava/lang/String;
91+
|}
92+
|
93+
|
94+
""".trimMargin()
95+
}
96+
}
97+
98+
test("expect testFixtures api declaration is not generated") {
99+
project.projectDir.resolve("api/testFixtures/java-test-fixtures-test.api")
100+
.shouldNotExist()
101+
}
102+
}
103+
}
104+
})
105+
106+
107+
private fun FunSpec.createTestFixturesProject(
108+
bcvTestFixturesTargetEnabled: Boolean,
109+
projectDir: Path = tempdir().toPath()
110+
): GradleProjectTest {
111+
return gradleKtsProjectTest("java-test-fixtures-test", projectDir) {
112+
113+
buildGradleKts = """
114+
|plugins {
115+
| kotlin("jvm") version "1.7.10"
116+
| id("dev.adamko.kotlin.binary-compatibility-validator") version "0.0.1-SNAPSHOT"
117+
| `java-test-fixtures`
118+
|}
119+
|
120+
|binaryCompatibilityValidator {
121+
| testFixtures {
122+
| enabled.set($bcvTestFixturesTargetEnabled)
123+
| }
124+
|}
125+
|
126+
""".trimMargin()
127+
128+
dir("src/main/kotlin") {
129+
createKotlinFile(
130+
"Hello.kt",
131+
"""
132+
|class Hello(private val response: String) {
133+
| fun greeting() = response
134+
|}
135+
|
136+
""".trimMargin()
137+
)
138+
}
139+
dir("src/testFixtures/kotlin") {
140+
createKotlinFile(
141+
"HelloHelper.kt",
142+
"""
143+
|fun standardHello() = Hello("standard")
144+
|
145+
""".trimMargin()
146+
)
147+
}
148+
}
149+
}

modules/bcv-gradle-plugin-functional-tests/src/testFixtures/kotlin/GradleTestKitUtils.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package dev.adamko.kotlin.binary_compatibility_validator.test.utils
44

5+
import dev.adamko.kotlin.binary_compatibility_validator.test.utils.GradleProjectTest.Companion.testMavenRepoDir
56
import java.io.File
67
import java.nio.file.Path
78
import java.nio.file.Paths
@@ -27,10 +28,6 @@ class GradleProjectTest(
2728

2829
val runner: GradleRunner = GradleRunner.create().withProjectDir(projectDir.toFile())
2930

30-
val testMavenRepoRelativePath: String =
31-
projectDir.relativize(testMavenRepoDir).toFile().invariantSeparatorsPath
32-
33-
3431
companion object {
3532

3633
/** file-based Maven Repo that contains the published plugin */
@@ -63,7 +60,7 @@ class GradleProjectTest(
6360
* Builder for testing a Gradle project that uses Kotlin script DSL and creates default
6461
* `settings.gradle.kts` and `gradle.properties` files.
6562
*
66-
* @param[testProjectName] the path of the project directory, relative to [baseDir
63+
* @param[testProjectName] the path of the project directory
6764
*/
6865
fun gradleKtsProjectTest(
6966
testProjectName: String,
@@ -79,15 +76,15 @@ fun gradleKtsProjectTest(
7976
|dependencyResolutionManagement {
8077
| repositories {
8178
| mavenCentral()
82-
| maven(file("$testMavenRepoRelativePath"))
79+
| maven(file("$testMavenRepoDir"))
8380
| }
8481
|}
8582
|
8683
|pluginManagement {
8784
| repositories {
8885
| gradlePluginPortal()
8986
| mavenCentral()
90-
| maven(file("$testMavenRepoRelativePath"))
87+
| maven(file("$testMavenRepoDir"))
9188
| }
9289
|}
9390
|
@@ -123,15 +120,15 @@ fun gradleGroovyProjectTest(
123120
|dependencyResolutionManagement {
124121
| repositories {
125122
| mavenCentral()
126-
| maven { url = file("$testMavenRepoRelativePath") }
123+
| maven { url = file("$testMavenRepoDir") }
127124
| }
128125
|}
129126
|
130127
|pluginManagement {
131128
| repositories {
132129
| gradlePluginPortal()
133130
| mavenCentral()
134-
| maven { url = file("$testMavenRepoRelativePath") }
131+
| maven { url = file("$testMavenRepoDir") }
135132
| }
136133
|}
137134
|

modules/bcv-gradle-plugin/src/main/kotlin/BCVProjectPlugin.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.gradle.api.Project
1414
import org.gradle.api.file.ProjectLayout
1515
import org.gradle.api.provider.ProviderFactory
1616
import org.gradle.api.tasks.SourceSet
17+
import org.gradle.internal.component.external.model.TestFixturesSupport.TEST_FIXTURE_SOURCESET_NAME
1718
import org.gradle.kotlin.dsl.*
1819
import org.gradle.language.base.plugins.LifecycleBasePlugin
1920
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
@@ -88,6 +89,7 @@ abstract class BCVProjectPlugin @Inject constructor(
8889
createKotlinJvmTargets(project, extension)
8990
createKotlinAndroidTargets(project, extension)
9091
createKotlinMultiplatformTargets(project, extension)
92+
createJavaTestFixtureTargets(project, extension)
9193
}
9294

9395
private fun createExtension(project: Project): BCVProjectExtension {
@@ -169,4 +171,22 @@ abstract class BCVProjectPlugin @Inject constructor(
169171
}
170172
}
171173
}
174+
175+
private fun createJavaTestFixtureTargets(
176+
project: Project,
177+
extension: BCVProjectExtension,
178+
) {
179+
project.pluginManager.withPlugin("java-test-fixtures") {
180+
extension.targets.create(TEST_FIXTURE_SOURCESET_NAME) {
181+
// don't enable by default - requiring an API spec for test-fixtures is pretty unusual
182+
enabled.set(false)
183+
project
184+
.sourceSets
185+
.matching { it.name == TEST_FIXTURE_SOURCESET_NAME }
186+
.all {
187+
inputClasses.from(output.classesDirs)
188+
}
189+
}
190+
}
191+
}
172192
}

0 commit comments

Comments
 (0)