Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User-friendly error message when the plugin is applied the first time… #48

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
User-friendly error message when the plugin is applied the first time…
… and api folder does not yet exist

Fixes #21
  • Loading branch information
qwwdfsad committed Mar 9, 2021
commit 9089fafcf977a68d7f159682a847ec790d3d3478
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,14 @@ internal class DefaultConfigTests : BaseKotlinGradleTest() {
buildGradleKts {
resolve("examples/gradle/base/withPlugin.gradle.kts")
}

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

runner.buildAndFail().apply {
println(output)
assertTrue { output.contains("Please ensure that ':apiDump' was executed") }
assertTaskFailure(":apiCheck")
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/main/kotlin/ApiCompareCompareTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ 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
var nonExistingProjectApiDir: String? = null

@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
Expand All @@ -26,10 +37,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 +67,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