diff --git a/build.gradle.kts b/build.gradle.kts index 2b1256a..7b27441 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,12 +12,13 @@ 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") + testImplementation("io.mockk:mockk:1.10.0") } pluginBundle { 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