Skip to content

Commit f124477

Browse files
committed
Include generated source files in list of generated files, not only class files and resources
1 parent 115bd64 commit f124477

File tree

2 files changed

+113
-8
lines changed

2 files changed

+113
-8
lines changed

src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,18 @@ class KotlinCompilation {
314314
}
315315

316316
/** Result of the compilation */
317-
class Result(val exitCode: ExitCode, val outputDirectory: File,
318-
/** Messages that were printed by the compilation */
319-
val messages: String) {
320-
/** All output files that were created by the compilation */
321-
val generatedFiles: Collection<File> = outputDirectory.listFilesRecursively()
322-
317+
class Result(
318+
val exitCode: ExitCode,
319+
/**
320+
* The class, resource and intermediate source files generated during the compilation.
321+
* Does not include stub files and kapt incremental data.
322+
*/
323+
val generatedFiles: Collection<File>,
324+
/** The directory where only the final output class and resources files will be */
325+
val outputDirectory: File,
326+
/** Messages that were printed by the compilation */
327+
val messages: String
328+
) {
323329
/** class loader to load the compile classes */
324330
val classLoader = URLClassLoader(arrayOf(outputDirectory.toURI().toURL()),
325331
this::class.java.classLoader)
@@ -724,7 +730,12 @@ class KotlinCompilation {
724730
if(exitCode != ExitCode.OK)
725731
searchSystemOutForKnownErrors(messages)
726732

727-
return Result(exitCode, classesDir, messages)
733+
val classAndResFiles = classesDir.listFilesRecursively()
734+
val kaptGeneratedKotlinFiles = kaptKotlinGeneratedDir.listFilesRecursively()
735+
val kaptGeneratedJavaFiles = kaptSourceDir.listFilesRecursively()
736+
val generatedFiles = classAndResFiles + kaptGeneratedJavaFiles + kaptGeneratedKotlinFiles
737+
738+
return Result(exitCode, generatedFiles, classesDir, messages)
728739
}
729740

730741
private fun commonClasspaths() = mutableListOf<File>().apply {

src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ class KotlinCompilationTests {
464464
}
465465
}
466466

467-
468467
@Test
469468
fun `Kotlin AP sees Java class`() {
470469
val jSource = SourceFile.java(
@@ -649,6 +648,101 @@ class KotlinCompilationTests {
649648
assertThat(result.messages).contains("non-existing-plugin.jar not found")
650649
}
651650

651+
@Test
652+
fun `Generated Java source is among generated files list`() {
653+
val kSource = SourceFile.kotlin(
654+
"KSource.kt", """
655+
package com.tschuchort.compiletesting
656+
657+
@ProcessElem
658+
class KSource {
659+
fun foo() {}
660+
}
661+
"""
662+
)
663+
664+
val result = defaultCompilerConfig().apply {
665+
sources = listOf(kSource)
666+
annotationProcessors = listOf(kotlinTestProc)
667+
inheritClassPath = true
668+
}.compile()
669+
670+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
671+
assertThat(result.generatedFiles.map { it.name }).contains(KotlinTestProcessor.GENERATED_JAVA_CLASS_NAME + ".java")
672+
}
673+
674+
@Test
675+
fun `Generated Kotlin source is among generated files list`() {
676+
val kSource = SourceFile.kotlin(
677+
"KSource.kt", """
678+
package com.tschuchort.compiletesting
679+
680+
@ProcessElem
681+
class KSource {
682+
fun foo() {}
683+
}
684+
"""
685+
)
686+
687+
val result = defaultCompilerConfig().apply {
688+
sources = listOf(kSource)
689+
annotationProcessors = listOf(kotlinTestProc)
690+
inheritClassPath = true
691+
}.compile()
692+
693+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
694+
assertThat(result.generatedFiles.map { it.name }).contains(KotlinTestProcessor.GENERATED_KOTLIN_CLASS_NAME + ".kt")
695+
}
696+
697+
@Test
698+
fun `Compiled Kotlin class file is among generated files list`() {
699+
val kSource = SourceFile.kotlin(
700+
"KSource.kt", """
701+
package com.tschuchort.compiletesting
702+
703+
@ProcessElem
704+
class KSource {
705+
fun foo() {}
706+
}
707+
"""
708+
)
709+
710+
val result = defaultCompilerConfig().apply {
711+
sources = listOf(kSource)
712+
annotationProcessors = listOf(kotlinTestProc)
713+
inheritClassPath = true
714+
}.compile()
715+
716+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
717+
assertThat(result.generatedFiles.map { it.name }).contains("KSource.class")
718+
}
719+
720+
@Test
721+
fun `Compiled Java class file is among generated files list`() {
722+
val jSource = SourceFile.java(
723+
"JSource.java", """
724+
package com.tschuchort.compiletesting;
725+
726+
@ProcessElem
727+
class JSource {
728+
void foo() {
729+
}
730+
}
731+
"""
732+
)
733+
734+
val result = defaultCompilerConfig().apply {
735+
sources = listOf(jSource)
736+
annotationProcessors = listOf(kotlinTestProc)
737+
inheritClassPath = true
738+
}.compile()
739+
740+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
741+
assertThat(result.generatedFiles.map { it.name }).contains("JSource.class")
742+
}
743+
744+
745+
652746
private fun defaultCompilerConfig(): KotlinCompilation {
653747
return KotlinCompilation().apply {
654748
workingDir = temporaryFolder.root

0 commit comments

Comments
 (0)