Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to instead remove any ending \r character here? And maybe only do that if the System.lineSeparator() is \r\n.

wasCR = false
sb = StringBuilder()
} else if (character == '\r') {
wasCR = true
} else
sb.append(character)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) }
}
}