Skip to content

Commit

Permalink
Using NoSystemOutListener prevents single tests from being executed (#…
Browse files Browse the repository at this point in the history
…3923)

Fixes #3303
  • Loading branch information
sksamuel committed Mar 10, 2024
1 parent e0d3d7a commit 68a6a28
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 61 deletions.
@@ -1,7 +1,8 @@
package io.kotest.extensions.system

import io.kotest.core.listeners.TestListener
import io.kotest.core.spec.Spec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import java.io.ByteArrayOutputStream
import java.io.PrintStream

Expand All @@ -12,44 +13,56 @@ class SystemErrWriteException(val str: String?) : RuntimeException()
* A [TestListener] that throws an error if anything is written to standard out.
*/
object NoSystemOutListener : TestListener {
private fun setup() {
val out = ByteArrayOutputStream()
System.setOut(object : PrintStream(out) {
private fun error(a: Any?): Nothing = throw SystemOutWriteException(a?.toString())
override fun print(b: Boolean) = error(b)
override fun print(c: Char) = error(c)
override fun print(i: Int) = error(i)
override fun print(l: Long) = error(l)
override fun print(s: String) = error(s)
override fun print(o: Any) = error(o)
override fun print(c: CharArray) = error(c)
override fun print(d: Double) = error(d)
override fun print(f: Float) = error(f)
})

private val stream = object : PrintStream(ByteArrayOutputStream()) {
private fun error(a: Any?): Nothing = throw SystemOutWriteException(a?.toString())
override fun print(b: Boolean) = error(b)
override fun print(c: Char) = error(c)
override fun print(i: Int) = error(i)
override fun print(l: Long) = error(l)
override fun print(s: String) = error(s)
override fun print(o: Any) = error(o)
override fun print(c: CharArray) = error(c)
override fun print(d: Double) = error(d)
override fun print(f: Float) = error(f)
}

override suspend fun beforeSpec(spec: Spec) = setup()
private val stdout = System.out

override suspend fun beforeTest(testCase: TestCase) {
System.setOut(stream)
}

override suspend fun afterTest(testCase: TestCase, result: TestResult) {
System.setOut(stdout)
}
}

/**
* A [TestListener] that throws an error if anything is written to standard err.
*/
object NoSystemErrListener : TestListener {
private fun setup() {
val out = ByteArrayOutputStream()
System.setErr(object : PrintStream(out) {
private fun error(a: Any?): Nothing = throw SystemErrWriteException(a?.toString())
override fun print(b: Boolean) = error(b)
override fun print(c: Char) = error(c)
override fun print(i: Int) = error(i)
override fun print(l: Long) = error(l)
override fun print(s: String) = error(s)
override fun print(o: Any) = error(o)
override fun print(c: CharArray) = error(c)
override fun print(d: Double) = error(d)
override fun print(f: Float) = error(f)
})

private val stream = object : PrintStream(ByteArrayOutputStream()) {
private fun error(a: Any?): Nothing = throw SystemErrWriteException(a?.toString())
override fun print(b: Boolean) = error(b)
override fun print(c: Char) = error(c)
override fun print(i: Int) = error(i)
override fun print(l: Long) = error(l)
override fun print(s: String) = error(s)
override fun print(o: Any) = error(o)
override fun print(c: CharArray) = error(c)
override fun print(d: Double) = error(d)
override fun print(f: Float) = error(f)
}

override suspend fun beforeSpec(spec: Spec) = setup()
private val stderr = System.err

override suspend fun beforeTest(testCase: TestCase) {
System.setErr(stream)
}

override suspend fun afterTest(testCase: TestCase, result: TestResult) {
System.setErr(stderr)
}
}
@@ -1,30 +1,30 @@
//package com.sksamuel.kt.extensions.system
//
//import io.kotest.assertions.throwables.shouldThrow
//import io.kotest.core.annotation.Isolate
//import io.kotest.extensions.system.NoSystemErrListener
//import io.kotest.extensions.system.NoSystemOutListener
//import io.kotest.extensions.system.SystemErrWriteException
//import io.kotest.extensions.system.SystemOutWriteException
//import io.kotest.core.spec.style.StringSpec
//
//@Isolate
//class NoSystemOutOrErrTest : StringSpec() {
//
// override fun listeners() = listOf(NoSystemOutListener, NoSystemErrListener)
//
// init {
//
// "System.out should throw an exception when the listener is added" {
// shouldThrow<SystemOutWriteException> {
// println("boom")
// }
// }
//
// "System.err should throw an exception when the listener is added" {
// shouldThrow<SystemErrWriteException> {
// System.err.println("boom")
// }
// }
// }
//}
package com.sksamuel.kt.extensions.system

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.annotation.Isolate
import io.kotest.extensions.system.NoSystemErrListener
import io.kotest.extensions.system.NoSystemOutListener
import io.kotest.extensions.system.SystemErrWriteException
import io.kotest.extensions.system.SystemOutWriteException
import io.kotest.core.spec.style.StringSpec

@Isolate
class NoSystemOutOrErrTest : StringSpec() {

override fun listeners() = listOf(NoSystemOutListener, NoSystemErrListener)

init {

"System.out should throw an exception when the listener is added" {
shouldThrow<SystemOutWriteException> {
println("boom")
}
}

"System.err should throw an exception when the listener is added" {
shouldThrow<SystemErrWriteException> {
System.err.println("boom")
}
}
}
}

0 comments on commit 68a6a28

Please sign in to comment.