Skip to content

Commit f18a750

Browse files
qwwdfsadelizarov
authored andcommitted
Do not print GuideTest system out if test passes
1 parent 6839151 commit f18a750

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

core/kotlinx-coroutines-core/src/internal/SystemProps.kt

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ internal fun systemProp(
1616
null
1717
}
1818

19+
internal fun systemProp(
20+
propertyName: String,
21+
defaultValue: Boolean
22+
): Boolean =
23+
try {
24+
System.getProperty(propertyName)?.toBoolean() ?: defaultValue
25+
} catch (e: SecurityException) {
26+
defaultValue
27+
}
28+
1929
internal fun systemProp(
2030
propertyName: String,
2131
defaultValue: Int,

core/kotlinx-coroutines-core/test/guide/test/TestUtil.kt

+32-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package kotlinx.coroutines.experimental.guide.test
66

77
import kotlinx.coroutines.experimental.*
8+
import kotlinx.coroutines.experimental.internal.*
89
import org.junit.Assert.*
910
import java.io.*
1011
import java.util.concurrent.*
@@ -21,14 +22,17 @@ private inline fun <T> outputException(name: String, block: () -> T): T =
2122
}
2223

2324
private const val SHUTDOWN_TIMEOUT = 5000L // 5 sec at most to wait
25+
private val OUT_ENABLED = systemProp("guide.tests.sout", false)
2426

2527
fun test(name: String, block: () -> Unit): List<String> = outputException(name) {
26-
println("--- Running test$name")
27-
val oldOut = System.out
28+
val sout = System.out
29+
val oldOut = if (OUT_ENABLED) System.out else NullOut
2830
val oldErr = System.err
2931
val bytesOut = ByteArrayOutputStream()
3032
val tee = TeeOutput(bytesOut, oldOut)
3133
val ps = PrintStream(tee)
34+
35+
oldOut.println("--- Running test$name")
3236
System.setErr(ps)
3337
System.setOut(ps)
3438
CommonPool.usePrivatePool()
@@ -53,7 +57,7 @@ fun test(name: String, block: () -> Unit): List<String> = outputException(name)
5357
CommonPool.restore()
5458
if (tee.flushLine()) oldOut.println()
5559
oldOut.println("--- done")
56-
System.setOut(oldOut)
60+
System.setOut(sout)
5761
System.setErr(oldErr)
5862
checkTestThreads(threadsBefore)
5963
}
@@ -144,37 +148,56 @@ private fun List<String>.checkEqualNumberOfLines(expected: Array<out String>) {
144148
error("Expected ${expected.size} lines, but found $size")
145149
}
146150

147-
fun List<String>.verifyLines(vararg expected: String) {
151+
fun List<String>.verifyLines(vararg expected: String) = verify {
148152
verifyCommonLines(expected)
149153
checkEqualNumberOfLines(expected)
150154
}
151155

152-
fun List<String>.verifyLinesStartWith(vararg expected: String) {
156+
fun List<String>.verifyLinesStartWith(vararg expected: String) = verify {
153157
verifyCommonLines(expected)
154158
assertTrue("Number of lines", expected.size <= size)
155159
}
156160

157-
fun List<String>.verifyLinesArbitraryTime(vararg expected: String) {
161+
fun List<String>.verifyLinesArbitraryTime(vararg expected: String) = verify {
158162
verifyCommonLines(expected, SanitizeMode.ARBITRARY_TIME)
159163
checkEqualNumberOfLines(expected)
160164
}
161165

162-
fun List<String>.verifyLinesFlexibleThread(vararg expected: String) {
166+
fun List<String>.verifyLinesFlexibleThread(vararg expected: String) = verify {
163167
verifyCommonLines(expected, SanitizeMode.FLEXIBLE_THREAD)
164168
checkEqualNumberOfLines(expected)
165169
}
166170

167-
fun List<String>.verifyLinesStartUnordered(vararg expected: String) {
171+
fun List<String>.verifyLinesStartUnordered(vararg expected: String) = verify {
168172
val expectedSorted = expected.sorted().toTypedArray()
169173
sorted().verifyLinesStart(*expectedSorted)
170174
}
171175

172-
fun List<String>.verifyLinesStart(vararg expected: String) {
176+
fun List<String>.verifyLinesStart(vararg expected: String) = verify {
173177
val n = minOf(size, expected.size)
174178
for (i in 0 until n) {
175179
val exp = sanitize(expected[i], SanitizeMode.FLEXIBLE_THREAD)
176180
val act = sanitize(get(i), SanitizeMode.FLEXIBLE_THREAD)
177181
assertEquals("Line ${i + 1}", exp, act.substring(0, minOf(act.length, exp.length)))
178182
}
179183
checkEqualNumberOfLines(expected)
184+
}
185+
186+
private object NullOut : PrintStream(NullOutputStream())
187+
188+
private class NullOutputStream : OutputStream() {
189+
override fun write(b: Int) = Unit
190+
}
191+
192+
private inline fun List<String>.verify(verification: () -> Unit) {
193+
try {
194+
verification()
195+
} catch (t: Throwable) {
196+
if (!OUT_ENABLED) {
197+
println("Printing [delayed] test output")
198+
forEach { println(it) }
199+
}
200+
201+
throw t
202+
}
180203
}

0 commit comments

Comments
 (0)