|
| 1 | +package python.multiplatform.gradle |
| 2 | + |
| 3 | +import org.gradle.api.Project |
| 4 | +import org.gradle.testfixtures.ProjectBuilder |
| 5 | +import kotlin.test.Test |
| 6 | +import kotlin.test.assertEquals |
| 7 | +import kotlin.test.assertFalse |
| 8 | +import kotlin.test.assertNull |
| 9 | +import kotlin.test.assertTrue |
| 10 | + |
| 11 | +/** |
| 12 | + * SPEC B-9 (python-multiplatform#27): the stubs task output reaches toolchain's `typedpythonStubs` |
| 13 | + * configuration by name, with the task dependency attached, whichever side appears first. |
| 14 | + */ |
| 15 | +class TypedPythonStubsWiringTest { |
| 16 | + |
| 17 | + private val configurationName = "typedpythonStubs" |
| 18 | + |
| 19 | + private fun projectWith(sourceSets: List<String>): Project { |
| 20 | + val project = ProjectBuilder.builder().build() |
| 21 | + val container = project.container(DummySourceSet::class.java) { name -> DummySourceSet(name, project) } |
| 22 | + sourceSets.forEach { container.create(it) } |
| 23 | + project.extensions.add("kotlin", DummyKotlinExt(container)) |
| 24 | + sourceSets.forEach { project.configurations.create("${it}Classpath") } |
| 25 | + return project |
| 26 | + } |
| 27 | + |
| 28 | + private fun wire(project: Project, sourceSets: List<String>) { |
| 29 | + val extension = project.extensions.create("pythonBindingsUnderTest", PythonBindingsExtension::class.java) |
| 30 | + extension.artifactIncludePackages.set(listOf("junit.runner")) |
| 31 | + extension.artifactTargets.set(sourceSets.associate { "${it}Classpath" to it }) |
| 32 | + PythonBindingsPlugin().configureArtifactBindings(project, extension) |
| 33 | + } |
| 34 | + |
| 35 | + private fun stubsDir(project: Project, sourceSet: String) = |
| 36 | + project.layout.buildDirectory.dir("generated/pythonStubs/$sourceSet").get().asFile |
| 37 | + |
| 38 | + @Test |
| 39 | + fun aConfigurationThatExistsBeforeTheWiringReceivesEverySourceSetsStubs() { |
| 40 | + val sets = listOf("desktopMain", "androidNativeArm64Main") |
| 41 | + val project = projectWith(sets) |
| 42 | + project.configurations.create(configurationName) |
| 43 | + wire(project, sets) |
| 44 | + |
| 45 | + val files = project.configurations.getByName(configurationName).files |
| 46 | + sets.forEach { assertTrue(stubsDir(project, it) in files, "$it missing from $files") } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun aConfigurationCreatedAfterTheWiringReceivesTheStubsToo() { |
| 51 | + val sets = listOf("desktopMain", "androidNativeArm64Main") |
| 52 | + val project = projectWith(sets) |
| 53 | + wire(project, sets) |
| 54 | + project.configurations.create(configurationName) |
| 55 | + |
| 56 | + val files = project.configurations.getByName(configurationName).files |
| 57 | + sets.forEach { assertTrue(stubsDir(project, it) in files, "$it missing from $files") } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun resolvingTheConfigurationDependsOnTheStubsTask() { |
| 62 | + val project = projectWith(listOf("desktopMain")) |
| 63 | + val configuration = project.configurations.create(configurationName) |
| 64 | + wire(project, listOf("desktopMain")) |
| 65 | + |
| 66 | + val deps = configuration.buildDependencies.getDependencies(null) |
| 67 | + val names = deps.map { it.name } |
| 68 | + assertTrue( |
| 69 | + names.any { it.startsWith("generatePythonStubs") }, |
| 70 | + "the task must travel with the files; dependencies were $names", |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun withoutTheConfigurationNothingIsCreatedAndNothingFails() { |
| 76 | + val project = projectWith(listOf("desktopMain")) |
| 77 | + wire(project, listOf("desktopMain")) |
| 78 | + |
| 79 | + assertNull(project.configurations.findByName(configurationName)) |
| 80 | + assertFalse(project.tasks.names.isEmpty()) |
| 81 | + } |
| 82 | +} |
0 commit comments