From f31dca32a444b5162014b6c1c41d0553f0867d66 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 29 Mar 2016 13:43:16 +0300 Subject: [PATCH] Flush stdout and stderr before shutdown in scripts Although a test is present, it doesn't check the behavior because it seems that in the test environment both streams are necessarily flushed #KT-9546 Fixed --- .../cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt | 9 ++++++++- .../smoke/scriptFlushBeforeShutdown/script.expected | 4 ++++ .../smoke/scriptFlushBeforeShutdown/script.kts | 3 +++ .../jetbrains/kotlin/integration/CompilerSmokeTest.java | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.expected create mode 100644 compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.kts diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt index 83b69406654..1d7995965cb 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt @@ -238,7 +238,14 @@ object KotlinToJVMBytecodeCompiler { val scriptConstructor = getScriptConstructor(scriptClass) try { - scriptConstructor.newInstance(*arrayOf(scriptArgs.toTypedArray())) + try { + scriptConstructor.newInstance(*arrayOf(scriptArgs.toTypedArray())) + } + finally { + // NB: these lines are required (see KT-9546) but aren't covered by tests + System.out.flush() + System.err.flush() + } } catch (e: Throwable) { reportExceptionFromScript(e) diff --git a/compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.expected b/compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.expected new file mode 100644 index 00000000000..f8d1988005e --- /dev/null +++ b/compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.expected @@ -0,0 +1,4 @@ +OUT: +AB +C +Return code: 0 diff --git a/compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.kts b/compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.kts new file mode 100644 index 00000000000..93c9f68c118 --- /dev/null +++ b/compiler/testData/integration/smoke/scriptFlushBeforeShutdown/script.kts @@ -0,0 +1,3 @@ +print("A") +println("B") +print("C") diff --git a/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java b/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java index bfe1533e4ab..c59a2dbf926 100644 --- a/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java +++ b/compiler/tests/org/jetbrains/kotlin/integration/CompilerSmokeTest.java @@ -65,6 +65,10 @@ public class CompilerSmokeTest extends CompilerSmokeTestBase { runCompiler("script", "-script", "script.kts"); } + public void testScriptFlushBeforeShutdown() throws Exception { + runCompiler("script", "-script", "script.kts"); + } + public void testCompileScript() throws Exception { String jar = tmpdir.getAbsolutePath() + File.separator + "script.jar";