From a47ee44e652e3feb1f82c1327c0805b98964aaee Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 9 Nov 2023 12:25:36 +0100 Subject: [PATCH] Scripting: refactor test infrastructure - add missing stderr handling --- .../kotlin/mainKts/test/mainKtsIT.kt | 6 ++-- .../kotlin/mainKts/test/mainKtsTest.kt | 11 +++++-- .../scripting/compiler/plugin/testUtil.kt | 33 +++++++++++++------ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt b/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt index 5d9c44e1183..c7fb7f94d09 100644 --- a/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt +++ b/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt @@ -163,12 +163,13 @@ class MainKtsIT { fun runWithKotlincAndMainKts( scriptPath: String, expectedOutPatterns: List = emptyList(), + expectedErrPatterns: List = emptyList(), expectedExitCode: Int = 0, cacheDir: Path? = null ) { val paths = PathUtil.kotlinPathsForDistDirectory runWithKotlinc( - scriptPath, expectedOutPatterns, expectedExitCode, + scriptPath, expectedOutPatterns, expectedErrPatterns, expectedExitCode, classpath = listOf( paths.jar(KotlinPaths.Jar.MainKts).also { Assert.assertTrue("kotlin-main-kts.jar not found, run dist task: ${it.absolutePath}", it.exists()) @@ -181,12 +182,13 @@ fun runWithKotlincAndMainKts( fun runWithKotlinRunner( scriptPath: String, expectedOutPatterns: List = emptyList(), + expectedErrPatterns: List = emptyList(), expectedExitCode: Int = 0, cacheDir: Path? = null, expectErrorOnK2: Boolean = false ) { runWithKotlinLauncherScript( - "kotlin", listOf(scriptPath), expectedOutPatterns, expectedExitCode, + "kotlin", listOf(scriptPath), expectedOutPatterns, expectedErrPatterns, expectedExitCode, additionalEnvVars = listOf(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR to (cacheDir?.toAbsolutePath()?.toString() ?: "")), expectErrorOnK2 = expectErrorOnK2 ) diff --git a/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsTest.kt b/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsTest.kt index 943fe927229..29581979c15 100644 --- a/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsTest.kt +++ b/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsTest.kt @@ -403,17 +403,24 @@ class CacheDirectoryDetectorTest { private val directories = Directories(systemProperties, environment) } -internal fun captureOut(body: () -> Unit): String { +internal fun captureOut(body: () -> Unit): String = captureOutAndErr(body).first + +internal fun captureOutAndErr(body: () -> Unit): Pair { val outStream = ByteArrayOutputStream() + val errStream = ByteArrayOutputStream() val prevOut = System.out + val prevErr = System.err System.setOut(PrintStream(outStream)) + System.setErr(PrintStream(errStream)) try { body() } finally { System.out.flush() + System.err.flush() System.setOut(prevOut) + System.setErr(prevErr) } - return outStream.toString().trim() + return outStream.toString().trim() to errStream.toString().trim() } internal fun withProperty(name: String, value: String?, body: () -> T): T { diff --git a/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt b/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt index 5938649900e..6cc70e7367c 100644 --- a/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt +++ b/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt @@ -30,6 +30,7 @@ internal fun getBaseCompilerArgumentsFromProperty(): List? = fun runWithKotlinc( scriptPath: String, expectedOutPatterns: List = emptyList(), + expectedErrPatterns: List = emptyList(), expectedExitCode: Int = 0, workDirectory: File? = null, classpath: List = emptyList(), @@ -38,7 +39,7 @@ fun runWithKotlinc( ) { runWithKotlinc( arrayOf("-script", scriptPath), - expectedOutPatterns, expectedExitCode, workDirectory, classpath, additionalEnvVars, expectErrorOnK2 + expectedOutPatterns, expectedErrPatterns, expectedExitCode, workDirectory, classpath, additionalEnvVars, expectErrorOnK2 ) } @@ -46,6 +47,7 @@ fun runWithKotlinLauncherScript( launcherScriptName: String, compilerArgs: Iterable, expectedOutPatterns: List = emptyList(), + expectedErrPatterns: List = emptyList(), expectedExitCode: Int = 0, workDirectory: File? = null, classpath: List = emptyList(), @@ -66,12 +68,16 @@ fun runWithKotlinLauncherScript( addAll(compilerArgs) } - runAndCheckResults(args, expectedOutPatterns, expectedExitCode, workDirectory, additionalEnvVars, expectErrorOnK2 = expectErrorOnK2) + runAndCheckResults( + args, expectedOutPatterns, expectedErrPatterns, expectedExitCode, workDirectory, additionalEnvVars, + expectErrorOnK2 = expectErrorOnK2 + ) } fun runWithKotlinc( compilerArgs: Array, expectedOutPatterns: List = emptyList(), + expectedErrPatterns: List = emptyList(), expectedExitCode: Int = 0, workDirectory: File? = null, classpath: List = emptyList(), @@ -79,14 +85,15 @@ fun runWithKotlinc( expectErrorOnK2: Boolean = false ) { runWithKotlinLauncherScript( - "kotlinc", compilerArgs.asIterable(), expectedOutPatterns, expectedExitCode, workDirectory, classpath, - additionalEnvVars, expectErrorOnK2 + "kotlinc", compilerArgs.asIterable(), expectedOutPatterns, expectedErrPatterns, + expectedExitCode, workDirectory, classpath, additionalEnvVars, expectErrorOnK2 ) } fun runAndCheckResults( args: List, expectedOutPatterns: List = emptyList(), + expectedErrPatterns: List = emptyList(), expectedExitCode: Int = 0, workDirectory: File? = null, additionalEnvVars: Iterable>? = null, @@ -143,12 +150,18 @@ fun runAndCheckResults( process.exitValue() != 0 || processOut.contains("error: ") ) } else { - Assert.assertEquals(expectedOutPatterns.size, processOut.size) - for ((expectedPattern, actualLine) in expectedOutPatterns.zip(processOut)) { - Assert.assertTrue( - "line \"$actualLine\" do not match with expected pattern \"$expectedPattern\"", - Regex(expectedPattern).matches(actualLine) - ) + fun checkExpectedOutputPatterns(expectedPatterns: List, actualOut: List) { + Assert.assertEquals(expectedPatterns.size, actualOut.size) + for ((expectedPattern, actualLine) in expectedPatterns.zip(actualOut)) { + Assert.assertTrue( + "line \"$actualLine\" do not match with expected pattern \"$expectedPattern\"", + Regex(expectedPattern).matches(actualLine) + ) + } + } + checkExpectedOutputPatterns(expectedOutPatterns, processOut) + if (expectedErrPatterns.isNotEmpty()) { + checkExpectedOutputPatterns(expectedErrPatterns, processErr) } Assert.assertEquals(expectedExitCode, process.exitValue()) }