@@ -13,6 +13,8 @@ import org.assertj.core.api.Assertions
13
13
import org.gradle.testkit.runner.BuildResult
14
14
import org.jetbrains.kotlin.konan.target.HostManager
15
15
import org.jetbrains.kotlin.konan.target.KonanTarget
16
+ import org.jetbrains.kotlin.utils.addToStdlib.butIf
17
+ import org.junit.Assert
16
18
import org.junit.Assume
17
19
import org.junit.Test
18
20
import java.io.File
@@ -48,27 +50,32 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
48
50
resolve(" /examples/gradle/base/withNativePlugin.gradle.kts" )
49
51
}
50
52
}
53
+
51
54
private fun BaseKotlinScope.additionalBuildConfig (config : String ) {
52
55
buildGradleKts {
53
56
resolve(config)
54
57
}
55
58
}
59
+
56
60
private fun BaseKotlinScope.addToSrcSet (pathTestFile : String , sourceSet : String = "commonMain") {
57
61
val fileName = Paths .get(pathTestFile).fileName.toString()
58
62
kotlin(fileName, sourceSet) {
59
63
resolve(pathTestFile)
60
64
}
61
65
}
66
+
62
67
private fun BaseKotlinScope.runApiCheck () {
63
68
runner {
64
69
arguments.add(" :apiCheck" )
65
70
}
66
71
}
72
+
67
73
private fun BaseKotlinScope.runApiDump () {
68
74
runner {
69
75
arguments.add(" :apiDump" )
70
76
}
71
77
}
78
+
72
79
private fun assertApiCheckPassed (buildResult : BuildResult ) {
73
80
buildResult.assertTaskSuccess(" :apiCheck" )
74
81
}
@@ -451,7 +458,7 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
451
458
}
452
459
453
460
@Test
454
- fun `klibCheck if all klib-targets are unavailable` () {
461
+ fun `klibCheck should not fail if all klib-targets are unavailable` () {
455
462
val runner = test {
456
463
baseProjectSetting()
457
464
addToSrcSet(" /examples/classes/TopLevelDeclarations.kt" )
@@ -468,10 +475,32 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
468
475
}
469
476
}
470
477
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
+
471
502
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" )
475
504
}
476
505
}
477
506
@@ -580,8 +609,10 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
580
609
checkKlibDump(runner.build(), " /examples/classes/AnotherBuildConfig.klib.dump" )
581
610
582
611
// 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()
585
616
)
586
617
val existingSource = runner.projectDir.resolve(
587
618
" src/commonMain/kotlin/AnotherBuildConfig.kt"
@@ -601,8 +632,10 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
601
632
checkKlibDump(runner.build(), " /examples/classes/AnotherBuildConfig.klib.dump" )
602
633
603
634
// 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()
606
639
)
607
640
val existingSource = runner.projectDir.resolve(
608
641
" src/linuxArm64Main/kotlin/AnotherBuildConfigLinuxArm64.kt"
@@ -626,8 +659,10 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
626
659
assertApiCheckPassed(runner.build())
627
660
628
661
// 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()
631
666
)
632
667
val existingSource = runner.projectDir.resolve(
633
668
" src/commonMain/kotlin/AnotherBuildConfig.kt"
@@ -665,12 +700,29 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
665
700
runApiDump()
666
701
}
667
702
668
- runner.build().apply {
703
+ runner.withDebug( true ). build().apply {
669
704
assertTaskSkipped(" :klibApiDump" )
670
705
}
671
706
assertFalse(runner.projectDir.resolve(" api" ).exists())
672
707
}
673
708
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
+
674
726
@Test
675
727
fun `apiDump should not fail if there is only one target` () {
676
728
val runner = test {
@@ -683,22 +735,26 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
683
735
}
684
736
685
737
@Test
686
- fun `apiCheck should not fail for empty project` () {
738
+ fun `apiCheck should fail for empty project` () {
687
739
val runner = test {
688
740
baseProjectSetting()
689
741
addToSrcSet(" /examples/classes/AnotherBuildConfig.kt" , sourceSet = " commonTest" )
690
742
runApiCheck()
691
743
}
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
+ }
693
750
}
694
751
695
752
@Test
696
753
fun `apiDump for a project with generated sources only` () {
697
754
val runner = test {
698
755
baseProjectSetting()
699
756
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 {
702
758
arguments.add(" :apiDump" )
703
759
}
704
760
}
@@ -713,11 +769,26 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
713
769
abiFile(projectName = " testproject" ) {
714
770
resolve(" /examples/classes/GeneratedSources.klib.dump" )
715
771
}
716
- // TODO: enable configuration cache back when we start skipping tasks correctly
717
- runner(withConfigurationCache = false ) {
772
+ runner {
718
773
arguments.add(" :apiCheck" )
719
774
}
720
775
}
721
776
assertApiCheckPassed(runner.build())
722
777
}
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
+ }
723
794
}
0 commit comments