Skip to content

Commit 29a1e48

Browse files
SimonMarquistschuchortdev
authored andcommitted
Fix missing UTF-8 encoding of logs resulting in unknown chars (#162)
(cherry picked from commit 63a1691)
1 parent 9a543c2 commit 29a1e48

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
326326
override fun close() = messageOutputStream.close()
327327
},
328328
internalMessageBuffer.outputStream()
329-
)
329+
),
330+
/* autoFlush = */ false,
331+
/* encoding = */ "UTF-8",
330332
)
331333

332334
protected fun log(s: String) {

ksp/src/test/kotlin/com/tschuchort/compiletesting/KspTest.kt

+35
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger
1414
import org.mockito.kotlin.any
1515
import org.mockito.kotlin.inOrder
1616
import org.mockito.kotlin.mock
17+
import kotlin.text.Typography.ellipsis
1718

1819
@RunWith(JUnit4::class)
1920
@ExperimentalCompilerApi
@@ -323,6 +324,40 @@ class KspTest {
323324
assertThat(result.messages).contains("This is a failure")
324325
}
325326

327+
@Test
328+
fun messagesAreEncodedAndDecodedWithUtf8() {
329+
val annotation = SourceFile.kotlin(
330+
"TestAnnotation.kt", """
331+
package foo.bar
332+
annotation class TestAnnotation
333+
""".trimIndent()
334+
)
335+
val targetClass = SourceFile.kotlin(
336+
"AppCode.kt", """
337+
package foo.bar
338+
@TestAnnotation
339+
class AppCode
340+
""".trimIndent()
341+
)
342+
val result = KotlinCompilation().apply {
343+
sources = listOf(annotation, targetClass)
344+
symbolProcessorProviders = listOf(processorProviderOf { env ->
345+
object : AbstractTestSymbolProcessor(env.codeGenerator) {
346+
override fun process(resolver: Resolver): List<KSAnnotated> {
347+
env.logger.logging("This is a log message with ellipsis $ellipsis")
348+
env.logger.info("This is an info message with unicode \uD83D\uDCAB")
349+
env.logger.warn("This is an warn message with emoji 🔥")
350+
return emptyList()
351+
}
352+
}
353+
})
354+
}.compile()
355+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
356+
assertThat(result.messages).contains("This is a log message with ellipsis $ellipsis")
357+
assertThat(result.messages).contains("This is an info message with unicode \uD83D\uDCAB")
358+
assertThat(result.messages).contains("This is an warn message with emoji 🔥")
359+
}
360+
326361
companion object {
327362
private val DUMMY_KOTLIN_SRC = SourceFile.kotlin(
328363
"foo.bar.Dummy.kt", """

0 commit comments

Comments
 (0)