@@ -13,6 +13,8 @@ import org.assertj.core.api.Assertions
1313import org.gradle.testkit.runner.BuildResult
1414import org.jetbrains.kotlin.konan.target.HostManager
1515import org.jetbrains.kotlin.konan.target.KonanTarget
16+ import org.jetbrains.kotlin.utils.addToStdlib.butIf
17+ import org.junit.Assert
1618import org.junit.Assume
1719import org.junit.Test
1820import java.io.File
@@ -48,27 +50,32 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
4850 resolve(" /examples/gradle/base/withNativePlugin.gradle.kts" )
4951 }
5052 }
53+
5154 private fun BaseKotlinScope.additionalBuildConfig (config : String ) {
5255 buildGradleKts {
5356 resolve(config)
5457 }
5558 }
59+
5660 private fun BaseKotlinScope.addToSrcSet (pathTestFile : String , sourceSet : String = "commonMain") {
5761 val fileName = Paths .get(pathTestFile).fileName.toString()
5862 kotlin(fileName, sourceSet) {
5963 resolve(pathTestFile)
6064 }
6165 }
66+
6267 private fun BaseKotlinScope.runApiCheck () {
6368 runner {
6469 arguments.add(" :apiCheck" )
6570 }
6671 }
72+
6773 private fun BaseKotlinScope.runApiDump () {
6874 runner {
6975 arguments.add(" :apiDump" )
7076 }
7177 }
78+
7279 private fun assertApiCheckPassed (buildResult : BuildResult ) {
7380 buildResult.assertTaskSuccess(" :apiCheck" )
7481 }
@@ -451,7 +458,7 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
451458 }
452459
453460 @Test
454- fun `klibCheck if all klib-targets are unavailable` () {
461+ fun `klibCheck should not fail if all klib-targets are unavailable` () {
455462 val runner = test {
456463 baseProjectSetting()
457464 addToSrcSet(" /examples/classes/TopLevelDeclarations.kt" )
@@ -468,10 +475,32 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
468475 }
469476 }
470477
478+ runner.build().apply {
479+ assertTaskSuccess(" :klibApiCheck" )
480+ }
481+ }
482+
483+ @Test
484+ fun `klibCheck should fail with strict validation if all klib-targets are unavailable` () {
485+ val runner = test {
486+ baseProjectSetting()
487+ additionalBuildConfig(" /examples/gradle/configuration/unsupported/enforce.gradle.kts" )
488+ addToSrcSet(" /examples/classes/TopLevelDeclarations.kt" )
489+ abiFile(projectName = " testproject" ) {
490+ // note that the regular dump is used, where linuxArm64 is presented
491+ resolve(" /examples/classes/TopLevelDeclarations.klib.dump" )
492+ }
493+ runner {
494+ arguments.add(
495+ " -P$BANNED_TARGETS_PROPERTY_NAME =linuxArm64,linuxX64,mingwX64," +
496+ " androidNativeArm32,androidNativeArm64,androidNativeX64,androidNativeX86"
497+ )
498+ arguments.add(" :klibApiCheck" )
499+ }
500+ }
501+
471502 runner.buildAndFail().apply {
472- Assertions .assertThat(output).contains(
473- " KLib ABI dump/validation requires at least one enabled klib target, but none were found."
474- )
503+ assertTaskFailure(" :klibApiExtractForValidation" )
475504 }
476505 }
477506
@@ -580,8 +609,10 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
580609 checkKlibDump(runner.build(), " /examples/classes/AnotherBuildConfig.klib.dump" )
581610
582611 // Update the source file by adding a declaration
583- val updatedSourceFile = File (this ::class .java.getResource(
584- " /examples/classes/AnotherBuildConfigModified.kt" )!! .toURI()
612+ val updatedSourceFile = File (
613+ this ::class .java.getResource(
614+ " /examples/classes/AnotherBuildConfigModified.kt"
615+ )!! .toURI()
585616 )
586617 val existingSource = runner.projectDir.resolve(
587618 " src/commonMain/kotlin/AnotherBuildConfig.kt"
@@ -601,8 +632,10 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
601632 checkKlibDump(runner.build(), " /examples/classes/AnotherBuildConfig.klib.dump" )
602633
603634 // Update the source file by adding a declaration
604- val updatedSourceFile = File (this ::class .java.getResource(
605- " /examples/classes/AnotherBuildConfigLinuxArm64.kt" )!! .toURI()
635+ val updatedSourceFile = File (
636+ this ::class .java.getResource(
637+ " /examples/classes/AnotherBuildConfigLinuxArm64.kt"
638+ )!! .toURI()
606639 )
607640 val existingSource = runner.projectDir.resolve(
608641 " src/linuxArm64Main/kotlin/AnotherBuildConfigLinuxArm64.kt"
@@ -626,8 +659,10 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
626659 assertApiCheckPassed(runner.build())
627660
628661 // Update the source file by adding a declaration
629- val updatedSourceFile = File (this ::class .java.getResource(
630- " /examples/classes/AnotherBuildConfigModified.kt" )!! .toURI()
662+ val updatedSourceFile = File (
663+ this ::class .java.getResource(
664+ " /examples/classes/AnotherBuildConfigModified.kt"
665+ )!! .toURI()
631666 )
632667 val existingSource = runner.projectDir.resolve(
633668 " src/commonMain/kotlin/AnotherBuildConfig.kt"
@@ -665,12 +700,29 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
665700 runApiDump()
666701 }
667702
668- runner.build().apply {
703+ runner.withDebug( true ). build().apply {
669704 assertTaskSkipped(" :klibApiDump" )
670705 }
671706 assertFalse(runner.projectDir.resolve(" api" ).exists())
672707 }
673708
709+ @Test
710+ fun `apiDump should remove dump file if the project does not contain sources anymore` () {
711+ val runner = test {
712+ baseProjectSetting()
713+ addToSrcSet(" /examples/classes/AnotherBuildConfig.kt" , sourceSet = " commonTest" )
714+ abiFile(projectName = " testproject" ) {
715+ resolve(" /examples/classes/AnotherBuildConfig.klib.dump" )
716+ }
717+ runApiDump()
718+ }
719+
720+ runner.build().apply {
721+ assertTaskSuccess(" :klibApiDump" )
722+ }
723+ assertFalse(runner.projectDir.resolve(" api" ).resolve(" testproject.klib.api" ).exists())
724+ }
725+
674726 @Test
675727 fun `apiDump should not fail if there is only one target` () {
676728 val runner = test {
@@ -683,22 +735,26 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
683735 }
684736
685737 @Test
686- fun `apiCheck should not fail for empty project` () {
738+ fun `apiCheck should fail for empty project` () {
687739 val runner = test {
688740 baseProjectSetting()
689741 addToSrcSet(" /examples/classes/AnotherBuildConfig.kt" , sourceSet = " commonTest" )
690742 runApiCheck()
691743 }
692- runner.build()
744+ runner.buildAndFail().apply {
745+ assertTaskFailure(" :klibApiExtractForValidation" )
746+ Assertions .assertThat(output).contains(
747+ " File with project's API declarations 'api/testproject.klib.api' does not exist."
748+ )
749+ }
693750 }
694751
695752 @Test
696753 fun `apiDump for a project with generated sources only` () {
697754 val runner = test {
698755 baseProjectSetting()
699756 additionalBuildConfig(" /examples/gradle/configuration/generatedSources/generatedSources.gradle.kts" )
700- // TODO: enable configuration cache back when we start skipping tasks correctly
701- runner(withConfigurationCache = false ) {
757+ runner {
702758 arguments.add(" :apiDump" )
703759 }
704760 }
@@ -713,11 +769,26 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
713769 abiFile(projectName = " testproject" ) {
714770 resolve(" /examples/classes/GeneratedSources.klib.dump" )
715771 }
716- // TODO: enable configuration cache back when we start skipping tasks correctly
717- runner(withConfigurationCache = false ) {
772+ runner {
718773 arguments.add(" :apiCheck" )
719774 }
720775 }
721776 assertApiCheckPassed(runner.build())
722777 }
778+
779+ @Test
780+ fun `apiCheck should fail after a source set was removed` () {
781+ val runner = test {
782+ baseProjectSetting()
783+ addToSrcSet(" /examples/classes/AnotherBuildConfig.kt" , " linuxX64Main" )
784+ addToSrcSet(" /examples/classes/AnotherBuildConfig.kt" , " linuxArm64Main" )
785+ abiFile(projectName = " testproject" ) {
786+ resolve(" /examples/classes/AnotherBuildConfig.klib.dump" )
787+ }
788+ runApiCheck()
789+ }
790+ runner.buildAndFail().apply {
791+ assertTaskFailure(" :klibApiCheck" )
792+ }
793+ }
723794}
0 commit comments