Skip to content

Commit 6e2a666

Browse files
Leland Takaminetschuchortdev
authored andcommitted
Fix SourceFile.fromPath compilation
1 parent 922eaff commit 6e2a666

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

gradlew

100644100755
File mode changed.

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class KotlinCompilation {
401401
}
402402

403403
/** Performs the 1st and 2nd compilation step to generate stubs and run annotation processors */
404-
private fun stubsAndApt(): ExitCode {
404+
private fun stubsAndApt(sourceFiles: List<File>): ExitCode {
405405
if(annotationProcessors.isEmpty()) {
406406
log("No services were given. Not running kapt steps.")
407407
return ExitCode.OK
@@ -434,8 +434,8 @@ class KotlinCompilation {
434434
)
435435
)
436436

437-
val kotlinSources = sourcesDir.listFilesRecursively().filter<File>(File::isKotlinFile)
438-
val javaSources = sourcesDir.listFilesRecursively().filter(File::isJavaFile)
437+
val kotlinSources = sourceFiles.filter(File::isKotlinFile)
438+
val javaSources = sourceFiles.filter(File::isJavaFile)
439439

440440
val sourcePaths = mutableListOf<File>().apply {
441441
addAll(javaSources)
@@ -503,8 +503,8 @@ class KotlinCompilation {
503503
}
504504

505505
/** Performs the 3rd compilation step to compile Kotlin source files */
506-
private fun compileKotlin(): ExitCode {
507-
val sources = sourcesDir.listFilesRecursively() +
506+
private fun compileKotlin(sourceFiles: List<File>): ExitCode {
507+
val sources = sourceFiles +
508508
kaptKotlinGeneratedDir.listFilesRecursively() +
509509
kaptSourceDir.listFilesRecursively()
510510

@@ -557,8 +557,8 @@ class KotlinCompilation {
557557
}
558558

559559
/** Performs the 4th compilation step to compile Java source files */
560-
private fun compileJava(): ExitCode {
561-
val javaSources = (sourcesDir.listFilesRecursively() + kaptSourceDir.listFilesRecursively())
560+
private fun compileJava(sourceFiles: List<File>): ExitCode {
561+
val javaSources = (sourceFiles + kaptSourceDir.listFilesRecursively())
562562
.filterNot<File>(File::isKotlinFile)
563563

564564
if(javaSources.isEmpty())
@@ -660,7 +660,7 @@ class KotlinCompilation {
660660
kaptKotlinGeneratedDir.mkdirs()
661661

662662
// write given sources to working directory
663-
sources.forEach { it.writeIfNeeded(sourcesDir) }
663+
val sourceFiles = sources.map { it.writeIfNeeded(sourcesDir) }
664664

665665
/*
666666
There are 4 steps to the compilation process:
@@ -679,7 +679,7 @@ class KotlinCompilation {
679679
withSystemProperty("idea.use.native.fs.for.win", "false") {
680680
// step 1 and 2: generate stubs and run annotation processors
681681
try {
682-
val exitCode = stubsAndApt()
682+
val exitCode = stubsAndApt(sourceFiles)
683683
if (exitCode != ExitCode.OK) {
684684
val messages = internalMessageBuffer.readUtf8()
685685
searchSystemOutForKnownErrors(messages)
@@ -690,7 +690,7 @@ class KotlinCompilation {
690690
}
691691

692692
// step 3: compile Kotlin files
693-
compileKotlin().let { exitCode ->
693+
compileKotlin(sourceFiles).let { exitCode ->
694694
if(exitCode != ExitCode.OK) {
695695
val messages = internalMessageBuffer.readUtf8()
696696
searchSystemOutForKnownErrors(messages)
@@ -700,7 +700,7 @@ class KotlinCompilation {
700700
}
701701

702702
// step 4: compile Java files
703-
compileJava().let { exitCode ->
703+
compileJava(sourceFiles).let { exitCode ->
704704
val messages = internalMessageBuffer.readUtf8()
705705

706706
if(exitCode != ExitCode.OK)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ class KotlinCompilationTests {
4646
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
4747
}
4848

49+
@Test
50+
fun `runs with SourceFile from path`() {
51+
val sourceFile = temporaryFolder.newFile("KSource.kt").apply {
52+
writeText("class KSource")
53+
}
54+
55+
val result = defaultCompilerConfig().apply {
56+
sources = listOf(SourceFile.fromPath(sourceFile))
57+
}.compile()
58+
59+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
60+
assertClassLoadable(result, "KSource")
61+
}
62+
63+
@Test
64+
fun `runs with SourceFile from paths with filename conflicts`() {
65+
temporaryFolder.newFolder("a")
66+
val sourceFileA = temporaryFolder.newFile("a/KSource.kt").apply {
67+
writeText("package a\n\nclass KSource")
68+
}
69+
70+
temporaryFolder.newFolder("b")
71+
val sourceFileB = temporaryFolder.newFile("b/KSource.kt").apply {
72+
writeText("package b\n\nclass KSource")
73+
}
74+
75+
val result = defaultCompilerConfig().apply {
76+
sources = listOf(
77+
SourceFile.fromPath(sourceFileA),
78+
SourceFile.fromPath(sourceFileB))
79+
}.compile()
80+
81+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
82+
assertClassLoadable(result, "a.KSource")
83+
assertClassLoadable(result, "b.KSource")
84+
}
4985

5086
@Test
5187
fun `Kotlin can access JDK`() {

0 commit comments

Comments
 (0)