Skip to content

Commit 3789ed7

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(). 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(). 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(). Avoid using project.name for ignored projects check > 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 18cd0df commit 3789ed7

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 path 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 (t: Throwable) {
348+
assertContains(
349+
t.stackTraceToString(),
350+
"""
351+
Cannot find excluded project subsub1 in all projects: [:, :sub1, :sub2, :sub1:subsub1, :sub1:subsub2]
352+
""".trimIndent()
353+
)
354+
}
355+
}
320356
}

src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt

+16-16
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
3535
private fun Project.validateExtension(extension: ApiValidationExtension) {
3636
afterEvaluate {
3737
val ignored = extension.ignoredProjects
38-
val all = allprojects.map { it.name }
38+
val all = allprojects.map { it.path }
3939
for (project in ignored) {
4040
require(project in all) { "Cannot find excluded project $project in all projects: $all" }
4141
}
@@ -55,7 +55,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
5555
extension: ApiValidationExtension,
5656
action: Action<AppliedPlugin>
5757
) = project.pluginManager.withPlugin(name) {
58-
if (project.name in extension.ignoredProjects) return@withPlugin
58+
if (project.path in extension.ignoredProjects) return@withPlugin
5959
action.execute(it)
6060
}
6161

@@ -64,7 +64,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
6464
extension: ApiValidationExtension,
6565
jvmRuntimeClasspath: NamedDomainObjectProvider<Configuration>
6666
) = configurePlugin("kotlin-multiplatform", project, extension) {
67-
if (project.name in extension.ignoredProjects) return@configurePlugin
67+
if (project.path in extension.ignoredProjects) return@configurePlugin
6868
val kotlin = project.kotlinMultiplatform
6969

7070
// Create common tasks for multiplatform
@@ -208,7 +208,7 @@ private fun Project.configureKotlinCompilation(
208208
commonApiCheck: TaskProvider<Task>? = null,
209209
useOutput: Boolean = false,
210210
) {
211-
val projectName = project.name
211+
val projectName = project.path
212212
val dumpFileName = project.jvmDumpFileName
213213
val apiDirProvider = targetConfig.apiDir
214214
val apiBuildDir = apiDirProvider.flatMap { f -> layout.buildDirectory.asFile.map { it.resolve(f) } }
@@ -252,7 +252,7 @@ private fun Project.configureApiTasks(
252252
targetConfig: TargetConfig = TargetConfig(this, extension),
253253
jvmRuntimeClasspath: NamedDomainObjectProvider<Configuration>,
254254
) {
255-
val projectName = project.name
255+
val projectName = project.path
256256
val dumpFileName = project.jvmDumpFileName
257257
val apiBuildDir = targetConfig.apiDir.flatMap { f -> layout.buildDirectory.asFile.map { it.resolve(f) } }
258258
val sourceSetsOutputsProvider = project.provider {
@@ -281,7 +281,7 @@ private fun Project.configureCheckTasks(
281281
commonApiDump: TaskProvider<Task>? = null,
282282
commonApiCheck: TaskProvider<Task>? = null,
283283
) {
284-
val projectName = project.name
284+
val projectName = project.path
285285
val apiCheckDir = targetConfig.apiDir.map {
286286
projectDir.resolve(it).also { r ->
287287
logger.debug("Configuring api for ${targetConfig.targetName ?: "jvm"} to $r")
@@ -398,16 +398,16 @@ private class KlibValidationPipelineBuilder(
398398

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

407407
private fun Project.dumpKlibsTask(klibDumpConfig: TargetConfig) =
408408
project.task<SyncFile>(klibDumpConfig.apiTaskName("Dump")) {
409-
isEnabled = klibAbiCheckEnabled(project.name, extension)
410-
description = "Syncs the KLib ABI file for ${project.name}"
409+
isEnabled = klibAbiCheckEnabled(project.path, extension)
410+
description = "Syncs the KLib ABI file for ${project.path}"
411411
group = "other"
412412
onlyIf {
413413
it as SyncFile
@@ -424,7 +424,7 @@ private class KlibValidationPipelineBuilder(
424424
klibDumpConfig.apiTaskName("ExtractForValidation")
425425
)
426426
{
427-
isEnabled = klibAbiCheckEnabled(project.name, extension)
427+
isEnabled = klibAbiCheckEnabled(project.path, extension)
428428
description = "Prepare a reference KLib ABI file by removing all unsupported targets from " +
429429
"the golden file stored in the project"
430430
group = "other"
@@ -443,7 +443,7 @@ private class KlibValidationPipelineBuilder(
443443
klibDumpConfig.apiTaskName("MergeInferred")
444444
)
445445
{
446-
isEnabled = klibAbiCheckEnabled(project.name, extension)
446+
isEnabled = klibAbiCheckEnabled(project.path, extension)
447447
description = "Merges multiple KLib ABI dump files generated for " +
448448
"different targets (including inferred dumps for unsupported targets) " +
449449
"into a single merged KLib ABI dump"
@@ -456,7 +456,7 @@ private class KlibValidationPipelineBuilder(
456456
klibMergeDir: Provider<File>,
457457
runtimeClasspath: NamedDomainObjectProvider<Configuration>
458458
) = project.task<KotlinKlibMergeAbiTask>(klibDumpConfig.apiTaskName("Merge")) {
459-
isEnabled = klibAbiCheckEnabled(project.name, extension)
459+
isEnabled = klibAbiCheckEnabled(project.path, extension)
460460
description = "Merges multiple KLib ABI dump files generated for " +
461461
"different targets into a single merged KLib ABI dump"
462462
mergedApiFile.fileProvider(klibMergeDir.map { it.resolve(klibDumpFileName) })
@@ -577,7 +577,7 @@ private class KlibValidationPipelineBuilder(
577577
apiBuildDir: Provider<File>,
578578
runtimeClasspath: NamedDomainObjectProvider<Configuration>
579579
): TaskProvider<KotlinKlibAbiBuildTask> {
580-
val projectName = project.name
580+
val projectName = project.path
581581
val buildTask = project.task<KotlinKlibAbiBuildTask>(targetConfig.apiTaskName("Build")) {
582582
isEnabled = klibAbiCheckEnabled(projectName, extension)
583583
// 'group' is not specified deliberately, so it will be hidden from ./gradlew tasks
@@ -594,7 +594,7 @@ private class KlibValidationPipelineBuilder(
594594

595595
private fun Project.mergeDependencyForUnsupportedTarget(targetConfig: TargetConfig): TaskProvider<DefaultTask> {
596596
return project.task<DefaultTask>(targetConfig.apiTaskName("Build")) {
597-
isEnabled = apiCheckEnabled(project.name, extension)
597+
isEnabled = apiCheckEnabled(project.path, extension)
598598

599599
doLast {
600600
logger.warn(
@@ -613,7 +613,7 @@ private class KlibValidationPipelineBuilder(
613613
): TaskProvider<KotlinKlibInferAbiTask> {
614614
val targetName = targetConfig.targetName!!
615615
return project.task<KotlinKlibInferAbiTask>(targetConfig.apiTaskName("Infer")) {
616-
isEnabled = klibAbiCheckEnabled(project.name, extension)
616+
isEnabled = klibAbiCheckEnabled(project.path, extension)
617617
description = "Try to infer the dump for unsupported target $targetName using dumps " +
618618
"generated for supported targets."
619619
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.path
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.path
2727

2828
private val rootDir = project.rootDir
2929

0 commit comments

Comments
 (0)