Skip to content

Commit

Permalink
Fix missing UTF-8 encoding of logs resulting in unknown chars (#162)
Browse files Browse the repository at this point in the history
(cherry picked from commit 63a1691)
  • Loading branch information
SimonMarquis authored and tschuchortdev committed May 26, 2024
1 parent 9a543c2 commit 29a1e48
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
override fun close() = messageOutputStream.close()
},
internalMessageBuffer.outputStream()
)
),
/* autoFlush = */ false,
/* encoding = */ "UTF-8",
)

protected fun log(s: String) {
Expand Down
35 changes: 35 additions & 0 deletions ksp/src/test/kotlin/com/tschuchort/compiletesting/KspTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger
import org.mockito.kotlin.any
import org.mockito.kotlin.inOrder
import org.mockito.kotlin.mock
import kotlin.text.Typography.ellipsis

@RunWith(JUnit4::class)
@ExperimentalCompilerApi
Expand Down Expand Up @@ -323,6 +324,40 @@ class KspTest {
assertThat(result.messages).contains("This is a failure")
}

@Test
fun messagesAreEncodedAndDecodedWithUtf8() {
val annotation = SourceFile.kotlin(
"TestAnnotation.kt", """
package foo.bar
annotation class TestAnnotation
""".trimIndent()
)
val targetClass = SourceFile.kotlin(
"AppCode.kt", """
package foo.bar
@TestAnnotation
class AppCode
""".trimIndent()
)
val result = KotlinCompilation().apply {
sources = listOf(annotation, targetClass)
symbolProcessorProviders = listOf(processorProviderOf { env ->
object : AbstractTestSymbolProcessor(env.codeGenerator) {
override fun process(resolver: Resolver): List<KSAnnotated> {
env.logger.logging("This is a log message with ellipsis $ellipsis")
env.logger.info("This is an info message with unicode \uD83D\uDCAB")
env.logger.warn("This is an warn message with emoji 🔥")
return emptyList()
}
}
})
}.compile()
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
assertThat(result.messages).contains("This is a log message with ellipsis $ellipsis")
assertThat(result.messages).contains("This is an info message with unicode \uD83D\uDCAB")
assertThat(result.messages).contains("This is an warn message with emoji 🔥")
}

companion object {
private val DUMMY_KOTLIN_SRC = SourceFile.kotlin(
"foo.bar.Dummy.kt", """
Expand Down

0 comments on commit 29a1e48

Please sign in to comment.