From 6268082a064fa9b01cee301931ca926fb2585b06 Mon Sep 17 00:00:00 2001 From: hesch Date: Fri, 5 Jun 2020 14:53:37 +0200 Subject: [PATCH 1/3] update dependency resolution --- build.gradle.kts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2b1256a..8899800 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,12 +12,12 @@ version = File("VERSION").readText().trim() buildDir = File("build/gradle") dependencies { - compile(gradleApi()) - compile("org.jetbrains.kotlin:kotlin-stdlib:1.3.41") - compile("org.jetbrains.kotlin:kotlin-reflect:1.3.41") + implementation(gradleApi()) + implementation("org.jetbrains.kotlin:kotlin-stdlib:1.3.41") + implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.41") - testCompile("junit:junit:4.12") - testCompile("org.hamcrest:hamcrest-all:1.3") + testImplementation("junit:junit:4.12") + testImplementation("org.hamcrest:hamcrest-all:1.3") } pluginBundle { From f92a6ec20f11b5c85bbb5104296405b088fcd1ed Mon Sep 17 00:00:00 2001 From: hesch Date: Fri, 5 Jun 2020 14:53:52 +0200 Subject: [PATCH 2/3] add mockk dependency --- build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle.kts b/build.gradle.kts index 8899800..7b27441 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,6 +18,7 @@ dependencies { testImplementation("junit:junit:4.12") testImplementation("org.hamcrest:hamcrest-all:1.3") + testImplementation("io.mockk:mockk:1.10.0") } pluginBundle { From a095881e040f5a04bce43be4fcef0e235e7b8d42 Mon Sep 17 00:00:00 2001 From: hesch Date: Fri, 5 Jun 2020 14:54:52 +0200 Subject: [PATCH 3/3] implement better line-ending handling in OutputStreamLogger --- .../psxpaul/stream/OutputStreamLogger.kt | 6 ++- .../psxpaul/stream/OutputStreamLoggerTest.kt | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/com/github/psxpaul/stream/OutputStreamLoggerTest.kt diff --git a/src/main/kotlin/com/github/psxpaul/stream/OutputStreamLogger.kt b/src/main/kotlin/com/github/psxpaul/stream/OutputStreamLogger.kt index c54e287..0b91beb 100644 --- a/src/main/kotlin/com/github/psxpaul/stream/OutputStreamLogger.kt +++ b/src/main/kotlin/com/github/psxpaul/stream/OutputStreamLogger.kt @@ -9,12 +9,16 @@ import java.io.OutputStream class OutputStreamLogger(private val logger: Logger) : OutputStream() { var sb = StringBuilder() + private var wasCR = false override fun write(b: Int) { val character = b.toChar() - if (character == '\n') { + if (wasCR || character == '\n') { logger.lifecycle(sb.toString()) + wasCR = false sb = StringBuilder() + } else if (character == '\r') { + wasCR = true } else sb.append(character) } diff --git a/src/test/kotlin/com/github/psxpaul/stream/OutputStreamLoggerTest.kt b/src/test/kotlin/com/github/psxpaul/stream/OutputStreamLoggerTest.kt new file mode 100644 index 0000000..7355bcc --- /dev/null +++ b/src/test/kotlin/com/github/psxpaul/stream/OutputStreamLoggerTest.kt @@ -0,0 +1,52 @@ +package com.github.psxpaul.stream + +import io.mockk.MockKAnnotations +import org.junit.Test +import org.gradle.api.logging.Logger +import io.mockk.impl.annotations.MockK +import io.mockk.verify +import org.junit.Before + +internal class OutputStreamLoggerTest { + @MockK + lateinit var logger: Logger + + @Before + fun setUp() = MockKAnnotations.init(this, relaxUnitFun = true) + + @Test + fun writeWritesLinesWithoutLF() { + val sut = OutputStreamLogger(logger) + val testString = "test" + testString.forEach { + sut.write(it.toInt()) + } + sut.write('\n'.toInt()) + verify { logger.lifecycle(testString) } + } + + @Test + fun writeWritesLinesWithoutCR() { + val sut = OutputStreamLogger(logger) + val testString = "test" + testString.forEach { + sut.write(it.toInt()) + } + sut.write('\r'.toInt()) + // We need to supply another character to trigger the logging + sut.write(' '.toInt()) + verify { logger.lifecycle(testString) } + } + + @Test + fun writeWritesLinesWithoutCRLF() { + val sut = OutputStreamLogger(logger) + val testString = "test" + testString.forEach { + sut.write(it.toInt()) + } + sut.write('\r'.toInt()) + sut.write('\n'.toInt()) + verify { logger.lifecycle(testString) } + } +} \ No newline at end of file