Skip to content

Commit 6ca0769

Browse files
committed
Check correct file extension in SourceFile factory functions
1 parent c5fc2cf commit 6ca0769

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ class KotlinCompilation {
437437
)
438438
)
439439

440-
val kotlinSources = sourceFiles.filter(File::isKotlinFile)
441-
val javaSources = sourceFiles.filter(File::isJavaFile)
440+
val kotlinSources = sourceFiles.filter(File::hasKotlinFileExtension)
441+
val javaSources = sourceFiles.filter(File::hasJavaFileExtension)
442442

443443
val sourcePaths = mutableListOf<File>().apply {
444444
addAll(javaSources)
@@ -513,7 +513,7 @@ class KotlinCompilation {
513513
kaptSourceDir.listFilesRecursively()
514514

515515
// if no Kotlin sources are available, skip the compileKotlin step
516-
if(sources.filter<File>(File::isKotlinFile).isEmpty())
516+
if(sources.filter<File>(File::hasKotlinFileExtension).isEmpty())
517517
return ExitCode.OK
518518

519519
// in this step also include source files generated by kapt in the previous step
@@ -563,7 +563,7 @@ class KotlinCompilation {
563563
/** Performs the 4th compilation step to compile Java source files */
564564
private fun compileJava(sourceFiles: List<File>): ExitCode {
565565
val javaSources = (sourceFiles + kaptSourceDir.listFilesRecursively())
566-
.filterNot<File>(File::isKotlinFile)
566+
.filterNot<File>(File::hasKotlinFileExtension)
567567

568568
if(javaSources.isEmpty())
569569
return ExitCode.OK

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ abstract class SourceFile {
1616
* Create a new Java source file for the compilation when the compilation is run
1717
*/
1818
fun java(name: String, @Language("java") contents: String, trimIndent: Boolean = true): SourceFile {
19+
require(File(name).hasJavaFileExtension())
1920
val finalContents = if (trimIndent) contents.trimIndent() else contents
2021
return new(name, finalContents)
2122
}
@@ -24,6 +25,7 @@ abstract class SourceFile {
2425
* Create a new Kotlin source file for the compilation when the compilation is run
2526
*/
2627
fun kotlin(name: String, @Language("kotlin") contents: String, trimIndent: Boolean = true): SourceFile {
28+
require(File(name).hasKotlinFileExtension())
2729
val finalContents = if (trimIndent) contents.trimIndent() else contents
2830
return new(name, finalContents)
2931
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ internal fun Path.listFilesRecursively(): List<Path> {
5151
return files
5252
}
5353

54-
internal fun File.isKotlinFile() = hasFileExtension(listOf("kt", "kts"))
54+
internal fun File.hasKotlinFileExtension() = hasFileExtension(listOf("kt", "kts"))
5555

56-
internal fun File.isJavaFile() = hasFileExtension(listOf("java"))
56+
internal fun File.hasJavaFileExtension() = hasFileExtension(listOf("java"))
5757

5858
internal fun File.hasFileExtension(extensions: List<String>)
5959
= extensions.any{ it.equals(extension, ignoreCase = true) }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class KotlinCompilationTests {
466466
@Test
467467
fun `Kotlin AP sees Java class`() {
468468
val jSource = SourceFile.java(
469-
"JSource.kt", """
469+
"JSource.java", """
470470
package com.tschuchort.compiletesting;
471471
472472
@ProcessElem

0 commit comments

Comments
 (0)