From 3c04e6af031e5af5180c6323e2713c6e9b2a1624 Mon Sep 17 00:00:00 2001 From: Artem Kobzar Date: Thu, 28 Dec 2023 10:01:17 +0000 Subject: [PATCH] [K/Wasm] Remove tmp files for the binaryen runner --- .../kotlin/wasm/test/tools/BinaryenRunner.kt | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/tools/BinaryenRunner.kt b/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/tools/BinaryenRunner.kt index 2910417fa6d..60d49d05266 100644 --- a/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/tools/BinaryenRunner.kt +++ b/wasm/wasm.tests/test/org/jetbrains/kotlin/wasm/test/tools/BinaryenRunner.kt @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.wasm.test.tools import org.jetbrains.kotlin.platform.wasm.BinaryenConfig import org.jetbrains.kotlin.utils.addToStdlib.runIf -import java.io.ByteArrayOutputStream import java.io.File import kotlin.test.fail @@ -28,24 +27,31 @@ sealed interface WasmOptimizer { } private fun exec(command: Array, input: ByteArray): ByteArray { - val timestamp = System.currentTimeMillis() - val savedInput = File - .createTempFile("binaryen_input_$timestamp", ".wasm") - .apply { writeBytes(input) } - val savedOutput = File - .createTempFile("binaryen_output_$timestamp", ".wasm") + var savedInput: File? = null + var savedOutput: File? = null + try { + val timestamp = System.currentTimeMillis() - val processBuilder = ProcessBuilder(*(command + listOf("-o", savedOutput.absolutePath, savedInput.absolutePath))) - .redirectErrorStream(true) + savedInput = File + .createTempFile("binaryen_input_$timestamp", ".wasm") + .apply { writeBytes(input) } - val exitValue = processBuilder.start().waitFor() + savedOutput = File.createTempFile("binaryen_output_$timestamp", ".wasm") - if (exitValue != 0) { - val commandString = command.joinToString(" ") { escapeShellArgument(it) } - fail("Command \"$commandString\" terminated with exit code $exitValue") + val processBuilder = ProcessBuilder(*(command + listOf("-o", savedOutput.absolutePath, savedInput.absolutePath))) + .redirectErrorStream(true) + val exitValue = processBuilder.start().waitFor() + + if (exitValue != 0) { + val commandString = command.joinToString(" ") { escapeShellArgument(it) } + fail("Command \"$commandString\" terminated with exit code $exitValue") + } + + return savedOutput.readBytes() + } finally { + savedInput?.delete() + savedOutput?.delete() } - - return savedOutput.readBytes() } } } \ No newline at end of file