Skip to content

Commit 2defc0b

Browse files
authored
User-friendly error message when the plugin is applied the first time… (#48)
* User-friendly error message when the plugin is applied the first time and 'api' folder does not yet exist Fixes #21
1 parent 47d0b0d commit 2defc0b

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,9 @@
66
package kotlinx.validation.test
77

88
import kotlinx.validation.api.*
9-
import kotlinx.validation.api.BaseKotlinGradleTest
10-
import kotlinx.validation.api.assertTaskFailure
11-
import kotlinx.validation.api.assertTaskSuccess
12-
import kotlinx.validation.api.buildGradleKts
13-
import kotlinx.validation.api.kotlin
14-
import kotlinx.validation.api.readFileList
15-
import kotlinx.validation.api.resolve
16-
import kotlinx.validation.api.runner
17-
import kotlinx.validation.api.test
18-
import org.assertj.core.api.Assertions
9+
import org.assertj.core.api.*
1910
import org.junit.Test
20-
import kotlin.test.assertFalse
21-
import kotlin.test.assertTrue
11+
import kotlin.test.*
2212

2313
internal class DefaultConfigTests : BaseKotlinGradleTest() {
2414

@@ -28,13 +18,13 @@ internal class DefaultConfigTests : BaseKotlinGradleTest() {
2818
buildGradleKts {
2919
resolve("examples/gradle/base/withPlugin.gradle.kts")
3020
}
31-
3221
runner {
3322
arguments.add(":apiCheck")
3423
}
3524
}
3625

3726
runner.buildAndFail().apply {
27+
assertTrue { output.contains("Please ensure that ':apiDump' was executed") }
3828
assertTaskFailure(":apiCheck")
3929
}
4030
}

src/main/kotlin/ApiCompareCompareTask.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,22 @@ import org.gradle.api.tasks.*
1212
import java.io.*
1313

1414
open class ApiCompareCompareTask : DefaultTask() {
15+
16+
/*
17+
* Nullability and optionality is a workaround for
18+
* https://github.com/gradle/gradle/issues/2016
19+
*
20+
* Unfortunately, there is no way to skip validation apart from setting 'null'
21+
*/
22+
@Optional
1523
@InputDirectory
1624
@PathSensitive(PathSensitivity.RELATIVE)
17-
lateinit var projectApiDir: File
25+
var projectApiDir: File? = null
26+
27+
// Used for diagnostic error message when projectApiDir doesn't exist
28+
@Input
29+
@Optional
30+
var nonExistingProjectApiDir: String? = null
1831

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

2740
@TaskAction
2841
fun verify() {
29-
if (!projectApiDir.exists())
30-
error("Expected folder with API declarations '$projectApiDir' does not exist")
31-
if (!apiBuildDir.exists())
32-
error("Folder with built API declarations '$apiBuildDir' does not exist")
42+
val projectApiDir = projectApiDir
43+
if (projectApiDir == null) {
44+
error("Expected folder with API declarations '$nonExistingProjectApiDir' does not exist.\n" +
45+
"Please ensure that ':apiDump' was executed in order to get API dump to compare the build against")
46+
}
3347

3448
val subject = project.name
3549
val apiBuildDirFiles = mutableSetOf<RelativePath>()
@@ -55,8 +69,7 @@ open class ApiCompareCompareTask : DefaultTask() {
5569
val expectedFile = expectedApiDeclaration.getFile(projectApiDir)
5670
val actualFile = expectedApiDeclaration.getFile(apiBuildDir)
5771
val diff = compareFiles(expectedFile, actualFile)
58-
if (diff != null)
59-
diffSet.add(diff)
72+
if (diff != null) diffSet.add(diff)
6073
if (diffSet.isNotEmpty()) {
6174
val diffText = diffSet.joinToString("\n\n")
6275
error("API check failed for project $subject.\n$diffText\n\n You can run :$subject:apiDump task to overwrite API declarations")

src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ private fun Project.configureCheckTasks(
134134
isEnabled = apiCheckEnabled(extension) && apiBuild.map { it.enabled }.getOrElse(true)
135135
group = "verification"
136136
description = "Checks signatures of public API against the golden value in API folder for $projectName"
137-
projectApiDir = apiCheckDir
137+
projectApiDir = if (apiCheckDir.exists()) {
138+
apiCheckDir
139+
} else {
140+
nonExistingProjectApiDir = apiCheckDir.toString()
141+
null
142+
}
138143
this.apiBuildDir = apiBuildDir
139144
dependsOn(apiBuild)
140145
}

0 commit comments

Comments
 (0)