Skip to content

Commit

Permalink
User-friendly error message when the plugin is applied the first time… (
Browse files Browse the repository at this point in the history
Kotlin#48)

* User-friendly error message when the plugin is applied the first time and 'api' folder does not yet exist

Fixes Kotlin#21
  • Loading branch information
qwwdfsad authored Mar 9, 2021
1 parent 47d0b0d commit 2defc0b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,9 @@
package kotlinx.validation.test

import kotlinx.validation.api.*
import kotlinx.validation.api.BaseKotlinGradleTest
import kotlinx.validation.api.assertTaskFailure
import kotlinx.validation.api.assertTaskSuccess
import kotlinx.validation.api.buildGradleKts
import kotlinx.validation.api.kotlin
import kotlinx.validation.api.readFileList
import kotlinx.validation.api.resolve
import kotlinx.validation.api.runner
import kotlinx.validation.api.test
import org.assertj.core.api.Assertions
import org.assertj.core.api.*
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.test.*

internal class DefaultConfigTests : BaseKotlinGradleTest() {

Expand All @@ -28,13 +18,13 @@ internal class DefaultConfigTests : BaseKotlinGradleTest() {
buildGradleKts {
resolve("examples/gradle/base/withPlugin.gradle.kts")
}

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

runner.buildAndFail().apply {
assertTrue { output.contains("Please ensure that ':apiDump' was executed") }
assertTaskFailure(":apiCheck")
}
}
Expand Down
27 changes: 20 additions & 7 deletions src/main/kotlin/ApiCompareCompareTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ import org.gradle.api.tasks.*
import java.io.*

open class ApiCompareCompareTask : DefaultTask() {

/*
* Nullability and optionality is a workaround for
* https://github.com/gradle/gradle/issues/2016
*
* Unfortunately, there is no way to skip validation apart from setting 'null'
*/
@Optional
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
lateinit var projectApiDir: File
var projectApiDir: File? = null

// Used for diagnostic error message when projectApiDir doesn't exist
@Input
@Optional
var nonExistingProjectApiDir: String? = null

@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
Expand All @@ -26,10 +39,11 @@ open class ApiCompareCompareTask : DefaultTask() {

@TaskAction
fun verify() {
if (!projectApiDir.exists())
error("Expected folder with API declarations '$projectApiDir' does not exist")
if (!apiBuildDir.exists())
error("Folder with built API declarations '$apiBuildDir' does not exist")
val projectApiDir = projectApiDir
if (projectApiDir == null) {
error("Expected folder with API declarations '$nonExistingProjectApiDir' does not exist.\n" +
"Please ensure that ':apiDump' was executed in order to get API dump to compare the build against")
}

val subject = project.name
val apiBuildDirFiles = mutableSetOf<RelativePath>()
Expand All @@ -55,8 +69,7 @@ open class ApiCompareCompareTask : DefaultTask() {
val expectedFile = expectedApiDeclaration.getFile(projectApiDir)
val actualFile = expectedApiDeclaration.getFile(apiBuildDir)
val diff = compareFiles(expectedFile, actualFile)
if (diff != null)
diffSet.add(diff)
if (diff != null) diffSet.add(diff)
if (diffSet.isNotEmpty()) {
val diffText = diffSet.joinToString("\n\n")
error("API check failed for project $subject.\n$diffText\n\n You can run :$subject:apiDump task to overwrite API declarations")
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ private fun Project.configureCheckTasks(
isEnabled = apiCheckEnabled(extension) && apiBuild.map { it.enabled }.getOrElse(true)
group = "verification"
description = "Checks signatures of public API against the golden value in API folder for $projectName"
projectApiDir = apiCheckDir
projectApiDir = if (apiCheckDir.exists()) {
apiCheckDir
} else {
nonExistingProjectApiDir = apiCheckDir.toString()
null
}
this.apiBuildDir = apiBuildDir
dependsOn(apiBuild)
}
Expand Down

0 comments on commit 2defc0b

Please sign in to comment.