-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation is modified when using `useIncrementalValidation=true` to not fail when encountering additions, which are known to be backwards compatible. Instead spits out warnings to allow developers commiting the file early with the changes they have made. The use case is to pair it with CI-run :check (:apiDump) task and commit updated API after merge - considering all steps are automated. In cases where the API is not compatible, the task fails similarly to `useIncrementalValidation=false` or simply _the default_. It's acknowledged that using `useIncrementalValidation=true` might cause issues when building upon a feature (or PR) in which cases the .api file might be left out in its previous state, not commited to the VCS. In which case the developer might prefer leaving the option in the default state (false). To the naked eye it might be apparent that checking only removals (leading `-`) is naive, but since the api diff add two distinct lines for changes (one leading `-`, other `+`), this naive approach proves working for all considered use-cases.
- Loading branch information
Showing
15 changed files
with
322 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/functionalTest/kotlin/kotlinx/validation/test/IncrementalTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright 2016-2024 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.validation.test | ||
|
||
import kotlinx.validation.api.* | ||
import org.junit.* | ||
|
||
class IncrementalTest : BaseKotlinGradleTest() { | ||
|
||
@Test | ||
fun `fails when removing source lines`() { | ||
val runner = test { | ||
buildGradleKts { | ||
resolve("/examples/gradle/base/withPlugin.gradle.kts") | ||
resolve("/examples/gradle/configuration/incremental/incremental.gradle.kts") | ||
} | ||
kotlin("IncrementalBase.kt") { | ||
resolve("/examples/classes/IncrementalRemoval.kt") | ||
} | ||
apiFile(rootProjectDir.name) { | ||
resolve("/examples/classes/Incremental.dump") | ||
} | ||
runner { | ||
arguments.add(":apiCheck") | ||
} | ||
} | ||
runner.buildAndFail().apply { | ||
assertTaskFailure(":apiCheck") | ||
} | ||
} | ||
|
||
@Test | ||
fun `fails when modifying source lines`() { | ||
val runner = test { | ||
buildGradleKts { | ||
resolve("/examples/gradle/base/withPlugin.gradle.kts") | ||
resolve("/examples/gradle/configuration/incremental/incremental.gradle.kts") | ||
} | ||
kotlin("IncrementalBase.kt") { | ||
resolve("/examples/classes/IncrementalModification.kt") | ||
} | ||
apiFile(rootProjectDir.name) { | ||
resolve("/examples/classes/Incremental.dump") | ||
} | ||
runner { | ||
arguments.add(":apiCheck") | ||
} | ||
} | ||
runner.buildAndFail().apply { | ||
assertTaskFailure(":apiCheck") | ||
} | ||
} | ||
|
||
@Test | ||
fun `succeeds when adding source lines`() { | ||
val runner = test { | ||
buildGradleKts { | ||
resolve("/examples/gradle/base/withPlugin.gradle.kts") | ||
resolve("/examples/gradle/configuration/incremental/incremental.gradle.kts") | ||
} | ||
kotlin("IncrementalBase.kt") { | ||
resolve("/examples/classes/IncrementalAddition.kt") | ||
} | ||
apiFile(rootProjectDir.name) { | ||
resolve("/examples/classes/Incremental.dump") | ||
} | ||
runner { | ||
arguments.add(":apiCheck") | ||
} | ||
} | ||
runner.build().apply { | ||
assertTaskSuccess(":apiCheck") | ||
} | ||
} | ||
|
||
@Test | ||
fun `does not dump when removing source lines`() { | ||
val runner = test { | ||
buildGradleKts { | ||
resolve("/examples/gradle/base/withPlugin.gradle.kts") | ||
resolve("/examples/gradle/configuration/incremental/incremental.gradle.kts") | ||
} | ||
kotlin("IncrementalBase.kt") { | ||
resolve("/examples/classes/IncrementalRemoval.kt") | ||
} | ||
apiFile(rootProjectDir.name) { | ||
resolve("/examples/classes/Incremental.dump") | ||
} | ||
runner { | ||
arguments.add(":apiCheck") | ||
} | ||
} | ||
runner.buildAndFail().apply { | ||
assertTaskFailure(":apiCheck") | ||
assertTaskNotRun(":apiDump") | ||
} | ||
} | ||
|
||
@Test | ||
fun `does not dump when modifying source lines`() { | ||
val runner = test { | ||
buildGradleKts { | ||
resolve("/examples/gradle/base/withPlugin.gradle.kts") | ||
resolve("/examples/gradle/configuration/incremental/incremental.gradle.kts") | ||
} | ||
kotlin("IncrementalBase.kt") { | ||
resolve("/examples/classes/IncrementalModification.kt") | ||
} | ||
apiFile(rootProjectDir.name) { | ||
resolve("/examples/classes/Incremental.dump") | ||
} | ||
runner { | ||
arguments.add(":apiCheck") | ||
} | ||
} | ||
runner.buildAndFail().apply { | ||
assertTaskFailure(":apiCheck") | ||
assertTaskNotRun(":apiDump") | ||
} | ||
} | ||
|
||
@Ignore | ||
@Test | ||
fun `updates dump when adding source lines`() { | ||
val runner = test { | ||
buildGradleKts { | ||
resolve("/examples/gradle/base/withPlugin.gradle.kts") | ||
resolve("/examples/gradle/configuration/incremental/incremental.gradle.kts") | ||
} | ||
kotlin("IncrementalBase.kt") { | ||
resolve("/examples/classes/IncrementalAddition.kt") | ||
} | ||
apiFile(rootProjectDir.name) { | ||
resolve("/examples/classes/Incremental.dump") | ||
} | ||
runner { | ||
arguments.add(":apiCheck") | ||
} | ||
} | ||
runner.build().apply { | ||
assertTaskSuccess(":apiCheck") | ||
assertTaskSuccess(":apiDump") | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/functionalTest/resources/examples/classes/Incremental.dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public final class foo/Incremental { | ||
public fun <init> (Ljava/lang/Object;)V | ||
public final fun component1 ()Ljava/lang/Object; | ||
public final fun copy (Ljava/lang/Object;)Lfoo/Incremental; | ||
public static synthetic fun copy$default (Lfoo/Incremental;Ljava/lang/Object;ILjava/lang/Object;)Lfoo/Incremental; | ||
public fun equals (Ljava/lang/Object;)Z | ||
public final fun getId ()Ljava/lang/Object; | ||
public fun hashCode ()I | ||
public final fun integer ()I | ||
public fun toString ()Ljava/lang/String; | ||
} | ||
|
||
public final class foo/IncrementalBaseKt { | ||
public static final fun getId ()Ljava/lang/Object; | ||
public static final fun sum ([I)I | ||
} |
17 changes: 17 additions & 0 deletions
17
src/functionalTest/resources/examples/classes/IncrementalAddition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright 2016-2024 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package foo | ||
|
||
data class Incremental(val id: Any) { | ||
fun integer() = 42 | ||
fun double() = 4.2 | ||
} | ||
|
||
fun sum(vararg integers: Int) = integers.sum() | ||
fun mus(vararg integers: Int) = integers.sum() | ||
|
||
val id: Any = 42 | ||
val id2: Any = 24 |
14 changes: 14 additions & 0 deletions
14
src/functionalTest/resources/examples/classes/IncrementalBase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright 2016-2024 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package foo | ||
|
||
data class Incremental(val id: Any) { | ||
fun integer() = 42 | ||
} | ||
|
||
fun sum(vararg integers: Int) = integers.sum() | ||
|
||
val id: Any = 42 |
14 changes: 14 additions & 0 deletions
14
src/functionalTest/resources/examples/classes/IncrementalModification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright 2016-2024 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package foo | ||
|
||
data class Incremental(val id: String) { | ||
fun integer() = 42.0 | ||
} | ||
|
||
fun sumOfInts(vararg integers: Int) = integers.sum() | ||
|
||
val id: Int = 42 |
8 changes: 8 additions & 0 deletions
8
src/functionalTest/resources/examples/classes/IncrementalRemoval.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright 2016-2024 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package foo | ||
|
||
class Incremental |
8 changes: 8 additions & 0 deletions
8
...functionalTest/resources/examples/gradle/configuration/incremental/incremental.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright 2016-2024 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
configure<kotlinx.validation.ApiValidationExtension> { | ||
useIncrementalValidation = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2016-2024 JetBrains s.r.o. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.validation | ||
|
||
import org.gradle.api.logging.Logger | ||
|
||
internal class IncrementalVerification(private val subject: String, private val logger: Logger): Verification { | ||
override fun verify(diffSet: Collection<String>) { | ||
var containsAdditions = false | ||
var containsRemovals = false | ||
out@ for (diff in diffSet) { | ||
for (line in diff.split("\n")) { | ||
when { | ||
line.startsWith("+++") || line.startsWith("---") -> continue | ||
line.startsWith("-") -> containsRemovals = true | ||
line.startsWith("+") -> containsAdditions = true | ||
} | ||
if (containsRemovals) break@out | ||
} | ||
} | ||
check(!containsRemovals) { | ||
val diffText = diffSet.joinToString("\n\n") | ||
"Incremental API check failed for project $subject.\n$diffText\n\n You can run :$subject:apiDump task to overwrite API declarations. These changes likely break compatibility with existing consumers using library '$subject', consider incrementing major version code for your next release" | ||
} | ||
if (containsAdditions) { | ||
logger.warn("API is incrementally compatible with previous version, however is not identical to the API file provided.") | ||
} | ||
} | ||
} |
Oops, something went wrong.