Skip to content

Commit

Permalink
Avoid using project.name for ignored projects check
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
Goooler committed Aug 14, 2024
1 parent 18cd0df commit 3789ed7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.validation.api.runner
import kotlinx.validation.api.test
import org.assertj.core.api.Assertions
import org.junit.Test
import kotlin.test.assertContains
import kotlin.test.assertTrue

internal class SubprojectsWithPluginOnRootTests : BaseKotlinGradleTest() {
Expand Down Expand Up @@ -317,4 +318,39 @@ internal class SubprojectsWithPluginOnRootTests : BaseKotlinGradleTest() {
Assertions.assertThat(apiSub2.readText()).isEqualToIgnoringNewLines("")
}
}

/**
* https://github.com/Kotlin/binary-compatibility-validator/issues/257
*/
@Test
fun `using project name instead of path should not be ignored`() {
val runner = test {
createProjectHierarchyWithPluginOnRoot()
rootProjectDir.resolve("build.gradle.kts").writeText(
"""
apiValidation {
ignoredProjects += listOf(
"subsub1"
)
}
""".trimIndent()
)

runner {
arguments.add(":apiCheck")
}
}

try {
runner.build()
error("Should have failed.")
} catch (t: Throwable) {
assertContains(
t.stackTraceToString(),
"""
Cannot find excluded project subsub1 in all projects: [:, :sub1, :sub2, :sub1:subsub1, :sub1:subsub2]
""".trimIndent()
)
}
}
}
32 changes: 16 additions & 16 deletions src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
private fun Project.validateExtension(extension: ApiValidationExtension) {
afterEvaluate {
val ignored = extension.ignoredProjects
val all = allprojects.map { it.name }
val all = allprojects.map { it.path }
for (project in ignored) {
require(project in all) { "Cannot find excluded project $project in all projects: $all" }
}
Expand All @@ -55,7 +55,7 @@ public class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
extension: ApiValidationExtension,
action: Action<AppliedPlugin>
) = project.pluginManager.withPlugin(name) {
if (project.name in extension.ignoredProjects) return@withPlugin
if (project.path in extension.ignoredProjects) return@withPlugin
action.execute(it)
}

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

// Create common tasks for multiplatform
Expand Down Expand Up @@ -208,7 +208,7 @@ private fun Project.configureKotlinCompilation(
commonApiCheck: TaskProvider<Task>? = null,
useOutput: Boolean = false,
) {
val projectName = project.name
val projectName = project.path
val dumpFileName = project.jvmDumpFileName
val apiDirProvider = targetConfig.apiDir
val apiBuildDir = apiDirProvider.flatMap { f -> layout.buildDirectory.asFile.map { it.resolve(f) } }
Expand Down Expand Up @@ -252,7 +252,7 @@ private fun Project.configureApiTasks(
targetConfig: TargetConfig = TargetConfig(this, extension),
jvmRuntimeClasspath: NamedDomainObjectProvider<Configuration>,
) {
val projectName = project.name
val projectName = project.path
val dumpFileName = project.jvmDumpFileName
val apiBuildDir = targetConfig.apiDir.flatMap { f -> layout.buildDirectory.asFile.map { it.resolve(f) } }
val sourceSetsOutputsProvider = project.provider {
Expand Down Expand Up @@ -281,7 +281,7 @@ private fun Project.configureCheckTasks(
commonApiDump: TaskProvider<Task>? = null,
commonApiCheck: TaskProvider<Task>? = null,
) {
val projectName = project.name
val projectName = project.path
val apiCheckDir = targetConfig.apiDir.map {
projectDir.resolve(it).also { r ->
logger.debug("Configuring api for ${targetConfig.targetName ?: "jvm"} to $r")
Expand Down Expand Up @@ -398,16 +398,16 @@ private class KlibValidationPipelineBuilder(

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

private fun Project.dumpKlibsTask(klibDumpConfig: TargetConfig) =
project.task<SyncFile>(klibDumpConfig.apiTaskName("Dump")) {
isEnabled = klibAbiCheckEnabled(project.name, extension)
description = "Syncs the KLib ABI file for ${project.name}"
isEnabled = klibAbiCheckEnabled(project.path, extension)
description = "Syncs the KLib ABI file for ${project.path}"
group = "other"
onlyIf {
it as SyncFile
Expand All @@ -424,7 +424,7 @@ private class KlibValidationPipelineBuilder(
klibDumpConfig.apiTaskName("ExtractForValidation")
)
{
isEnabled = klibAbiCheckEnabled(project.name, extension)
isEnabled = klibAbiCheckEnabled(project.path, extension)
description = "Prepare a reference KLib ABI file by removing all unsupported targets from " +
"the golden file stored in the project"
group = "other"
Expand All @@ -443,7 +443,7 @@ private class KlibValidationPipelineBuilder(
klibDumpConfig.apiTaskName("MergeInferred")
)
{
isEnabled = klibAbiCheckEnabled(project.name, extension)
isEnabled = klibAbiCheckEnabled(project.path, extension)
description = "Merges multiple KLib ABI dump files generated for " +
"different targets (including inferred dumps for unsupported targets) " +
"into a single merged KLib ABI dump"
Expand All @@ -456,7 +456,7 @@ private class KlibValidationPipelineBuilder(
klibMergeDir: Provider<File>,
runtimeClasspath: NamedDomainObjectProvider<Configuration>
) = project.task<KotlinKlibMergeAbiTask>(klibDumpConfig.apiTaskName("Merge")) {
isEnabled = klibAbiCheckEnabled(project.name, extension)
isEnabled = klibAbiCheckEnabled(project.path, extension)
description = "Merges multiple KLib ABI dump files generated for " +
"different targets into a single merged KLib ABI dump"
mergedApiFile.fileProvider(klibMergeDir.map { it.resolve(klibDumpFileName) })
Expand Down Expand Up @@ -577,7 +577,7 @@ private class KlibValidationPipelineBuilder(
apiBuildDir: Provider<File>,
runtimeClasspath: NamedDomainObjectProvider<Configuration>
): TaskProvider<KotlinKlibAbiBuildTask> {
val projectName = project.name
val projectName = project.path
val buildTask = project.task<KotlinKlibAbiBuildTask>(targetConfig.apiTaskName("Build")) {
isEnabled = klibAbiCheckEnabled(projectName, extension)
// 'group' is not specified deliberately, so it will be hidden from ./gradlew tasks
Expand All @@ -594,7 +594,7 @@ private class KlibValidationPipelineBuilder(

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

doLast {
logger.warn(
Expand All @@ -613,7 +613,7 @@ private class KlibValidationPipelineBuilder(
): TaskProvider<KotlinKlibInferAbiTask> {
val targetName = targetConfig.targetName!!
return project.task<KotlinKlibInferAbiTask>(targetConfig.apiTaskName("Infer")) {
isEnabled = klibAbiCheckEnabled(project.name, extension)
isEnabled = klibAbiCheckEnabled(project.path, extension)
description = "Try to infer the dump for unsupported target $targetName using dumps " +
"generated for supported targets."
group = "other"
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/BuildTaskBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class BuildTaskBase : WorkerAwareTaskBase() {
public val publicClasses: SetProperty<String> = stringSetProperty { publicClasses }

@get:Internal
internal val projectName = project.name
internal val projectName = project.path

internal fun fillCommonParams(params: BuildParametersBase) {
params.ignoredPackages.set(ignoredPackages)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/KotlinApiCompareTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public open class KotlinApiCompareTask : DefaultTask() {
@get:PathSensitive(PathSensitivity.RELATIVE)
public val generatedApiFile: RegularFileProperty = project.objects.fileProperty()

private val projectName = project.name
private val projectName = project.path

private val rootDir = project.rootDir

Expand Down

0 comments on commit 3789ed7

Please sign in to comment.