Skip to content

Commit f7a243a

Browse files
committed
Avoid using project.name for ignored projects check
We can also use `getPath()` to replace `getName()` here. > The project's name is not necessarily unique within a project hierarchy. You should use the getPath() method for a unique identifier for the project. See https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#getName().
1 parent 14ebd02 commit f7a243a

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

src/functionalTest/kotlin/kotlinx/validation/test/SubprojectsWithPluginOnRootTests.kt

+36
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlinx.validation.api.runner
1414
import kotlinx.validation.api.test
1515
import org.assertj.core.api.Assertions
1616
import org.junit.Test
17+
import kotlin.test.assertContains
1718
import kotlin.test.assertTrue
1819

1920
internal class SubprojectsWithPluginOnRootTests : BaseKotlinGradleTest() {
@@ -317,4 +318,39 @@ internal class SubprojectsWithPluginOnRootTests : BaseKotlinGradleTest() {
317318
Assertions.assertThat(apiSub2.readText()).isEqualToIgnoringNewLines("")
318319
}
319320
}
321+
322+
/**
323+
* https://github.com/Kotlin/binary-compatibility-validator/issues/257
324+
*/
325+
@Test
326+
fun `using project name instead of displayName should not be ignored`() {
327+
val runner = test {
328+
createProjectHierarchyWithPluginOnRoot()
329+
rootProjectDir.resolve("build.gradle.kts").writeText(
330+
"""
331+
apiValidation {
332+
ignoredProjects += listOf(
333+
"subsub1"
334+
)
335+
}
336+
""".trimIndent()
337+
)
338+
339+
runner {
340+
arguments.add(":apiCheck")
341+
}
342+
}
343+
344+
try {
345+
runner.build()
346+
error("Should have failed")
347+
} catch (e: Throwable) {
348+
assertContains(
349+
e.stackTraceToString(),
350+
"""
351+
Cannot find excluded project subsub1 in all projects: [root project '${rootProjectDir.name}', project ':sub1', project ':sub2', project ':sub1:subsub1', project ':sub1:subsub2']
352+
""".trimIndent()
353+
)
354+
}
355+
}
320356
}

src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt

+16-16
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
3434
private fun Project.validateExtension(extension: ApiValidationExtension) {
3535
afterEvaluate {
3636
val ignored = extension.ignoredProjects
37-
val all = allprojects.map { it.name }
37+
val all = allprojects.map { it.displayName }
3838
for (project in ignored) {
3939
require(project in all) { "Cannot find excluded project $project in all projects: $all" }
4040
}
@@ -54,7 +54,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
5454
extension: ApiValidationExtension,
5555
action: Action<AppliedPlugin>
5656
) = project.pluginManager.withPlugin(name) {
57-
if (project.name in extension.ignoredProjects) return@withPlugin
57+
if (project.displayName in extension.ignoredProjects) return@withPlugin
5858
action.execute(it)
5959
}
6060

@@ -63,7 +63,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
6363
extension: ApiValidationExtension,
6464
jvmRuntimeClasspath: NamedDomainObjectProvider<Configuration>
6565
) = configurePlugin("kotlin-multiplatform", project, extension) {
66-
if (project.name in extension.ignoredProjects) return@configurePlugin
66+
if (project.displayName in extension.ignoredProjects) return@configurePlugin
6767
val kotlin = project.kotlinMultiplatform
6868

6969
// Create common tasks for multiplatform
@@ -207,7 +207,7 @@ private fun Project.configureKotlinCompilation(
207207
commonApiCheck: TaskProvider<Task>? = null,
208208
useOutput: Boolean = false,
209209
) {
210-
val projectName = project.name
210+
val projectName = project.displayName
211211
val dumpFileName = project.jvmDumpFileName
212212
val apiDirProvider = targetConfig.apiDir
213213
val apiBuildDir = apiDirProvider.flatMap { f -> layout.buildDirectory.asFile.map { it.resolve(f) } }
@@ -251,7 +251,7 @@ private fun Project.configureApiTasks(
251251
targetConfig: TargetConfig = TargetConfig(this, extension),
252252
jvmRuntimeClasspath: NamedDomainObjectProvider<Configuration>,
253253
) {
254-
val projectName = project.name
254+
val projectName = project.displayName
255255
val dumpFileName = project.jvmDumpFileName
256256
val apiBuildDir = targetConfig.apiDir.flatMap { f -> layout.buildDirectory.asFile.map { it.resolve(f) } }
257257
val sourceSetsOutputsProvider = project.provider {
@@ -280,7 +280,7 @@ private fun Project.configureCheckTasks(
280280
commonApiDump: TaskProvider<Task>? = null,
281281
commonApiCheck: TaskProvider<Task>? = null,
282282
) {
283-
val projectName = project.name
283+
val projectName = project.displayName
284284
val apiCheckDir = targetConfig.apiDir.map {
285285
projectDir.resolve(it).also { r ->
286286
logger.debug("Configuring api for ${targetConfig.targetName ?: "jvm"} to $r")
@@ -397,16 +397,16 @@ private class KlibValidationPipelineBuilder(
397397

398398
private fun Project.checkKlibsTask(klibDumpConfig: TargetConfig) =
399399
project.task<KotlinApiCompareTask>(klibDumpConfig.apiTaskName("Check")) {
400-
isEnabled = klibAbiCheckEnabled(project.name, extension)
400+
isEnabled = klibAbiCheckEnabled(project.displayName, extension)
401401
group = "verification"
402402
description =
403-
"Checks signatures of a public KLib ABI against the golden value in ABI folder for ${project.name}"
403+
"Checks signatures of a public KLib ABI against the golden value in ABI folder for ${project.displayName}"
404404
}
405405

406406
private fun Project.dumpKlibsTask(klibDumpConfig: TargetConfig) =
407407
project.task<SyncFile>(klibDumpConfig.apiTaskName("Dump")) {
408-
isEnabled = klibAbiCheckEnabled(project.name, extension)
409-
description = "Syncs the KLib ABI file for ${project.name}"
408+
isEnabled = klibAbiCheckEnabled(project.displayName, extension)
409+
description = "Syncs the KLib ABI file for ${project.displayName}"
410410
group = "other"
411411
onlyIf {
412412
it as SyncFile
@@ -423,7 +423,7 @@ private class KlibValidationPipelineBuilder(
423423
klibDumpConfig.apiTaskName("ExtractForValidation")
424424
)
425425
{
426-
isEnabled = klibAbiCheckEnabled(project.name, extension)
426+
isEnabled = klibAbiCheckEnabled(project.displayName, extension)
427427
description = "Prepare a reference KLib ABI file by removing all unsupported targets from " +
428428
"the golden file stored in the project"
429429
group = "other"
@@ -442,7 +442,7 @@ private class KlibValidationPipelineBuilder(
442442
klibDumpConfig.apiTaskName("MergeInferred")
443443
)
444444
{
445-
isEnabled = klibAbiCheckEnabled(project.name, extension)
445+
isEnabled = klibAbiCheckEnabled(project.displayName, extension)
446446
description = "Merges multiple KLib ABI dump files generated for " +
447447
"different targets (including inferred dumps for unsupported targets) " +
448448
"into a single merged KLib ABI dump"
@@ -455,7 +455,7 @@ private class KlibValidationPipelineBuilder(
455455
klibMergeDir: Provider<File>,
456456
runtimeClasspath: NamedDomainObjectProvider<Configuration>
457457
) = project.task<KotlinKlibMergeAbiTask>(klibDumpConfig.apiTaskName("Merge")) {
458-
isEnabled = klibAbiCheckEnabled(project.name, extension)
458+
isEnabled = klibAbiCheckEnabled(project.displayName, extension)
459459
description = "Merges multiple KLib ABI dump files generated for " +
460460
"different targets into a single merged KLib ABI dump"
461461
mergedApiFile.fileProvider(klibMergeDir.map { it.resolve(klibDumpFileName) })
@@ -576,7 +576,7 @@ private class KlibValidationPipelineBuilder(
576576
apiBuildDir: Provider<File>,
577577
runtimeClasspath: NamedDomainObjectProvider<Configuration>
578578
): TaskProvider<KotlinKlibAbiBuildTask> {
579-
val projectName = project.name
579+
val projectName = project.displayName
580580
val buildTask = project.task<KotlinKlibAbiBuildTask>(targetConfig.apiTaskName("Build")) {
581581
isEnabled = klibAbiCheckEnabled(projectName, extension)
582582
// 'group' is not specified deliberately, so it will be hidden from ./gradlew tasks
@@ -593,7 +593,7 @@ private class KlibValidationPipelineBuilder(
593593

594594
private fun Project.mergeDependencyForUnsupportedTarget(targetConfig: TargetConfig): TaskProvider<DefaultTask> {
595595
return project.task<DefaultTask>(targetConfig.apiTaskName("Build")) {
596-
isEnabled = apiCheckEnabled(project.name, extension)
596+
isEnabled = apiCheckEnabled(project.displayName, extension)
597597

598598
doLast {
599599
logger.warn(
@@ -612,7 +612,7 @@ private class KlibValidationPipelineBuilder(
612612
): TaskProvider<KotlinKlibInferAbiTask> {
613613
val targetName = targetConfig.targetName!!
614614
return project.task<KotlinKlibInferAbiTask>(targetConfig.apiTaskName("Infer")) {
615-
isEnabled = klibAbiCheckEnabled(project.name, extension)
615+
isEnabled = klibAbiCheckEnabled(project.displayName, extension)
616616
description = "Try to infer the dump for unsupported target $targetName using dumps " +
617617
"generated for supported targets."
618618
group = "other"

src/main/kotlin/BuildTaskBase.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public abstract class BuildTaskBase : WorkerAwareTaskBase() {
4949
public val publicClasses: SetProperty<String> = stringSetProperty { publicClasses }
5050

5151
@get:Internal
52-
internal val projectName = project.name
52+
internal val projectName = project.displayName
5353

5454
internal fun fillCommonParams(params: BuildParametersBase) {
5555
params.ignoredPackages.set(ignoredPackages)

src/main/kotlin/KotlinApiCompareTask.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public open class KotlinApiCompareTask : DefaultTask() {
2323
@get:PathSensitive(PathSensitivity.RELATIVE)
2424
public val generatedApiFile: RegularFileProperty = project.objects.fileProperty()
2525

26-
private val projectName = project.name
26+
private val projectName = project.displayName
2727

2828
private val rootDir = project.rootDir
2929

0 commit comments

Comments
 (0)