From f63dac26e61e0916fdbcc5cdc854116aa2b13783 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 29 Apr 2021 19:35:52 +0200 Subject: [PATCH] Minor, extract ProgramWithDependencyOnCompiler for tests Also use ProcessBuilder instead of GeneralCommandLine for simplicity. --- .../kotlin/integration/ParallelBuildTest.kt | 48 ++------------ .../ProgramWithDependencyOnCompiler.kt | 62 +++++++++++++++++++ 2 files changed, 68 insertions(+), 42 deletions(-) create mode 100644 compiler/tests/org/jetbrains/kotlin/integration/ProgramWithDependencyOnCompiler.kt diff --git a/compiler/tests/org/jetbrains/kotlin/integration/ParallelBuildTest.kt b/compiler/tests/org/jetbrains/kotlin/integration/ParallelBuildTest.kt index 41ab9e522e2..0fa8e126610 100644 --- a/compiler/tests/org/jetbrains/kotlin/integration/ParallelBuildTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/integration/ParallelBuildTest.kt @@ -5,12 +5,7 @@ package org.jetbrains.kotlin.integration -import com.intellij.execution.configurations.GeneralCommandLine -import org.jetbrains.kotlin.cli.AbstractCliTest -import org.jetbrains.kotlin.cli.common.ExitCode -import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler import org.jetbrains.kotlin.test.util.KtTestUtil -import org.jetbrains.kotlin.utils.PathUtil.kotlinPathsForDistDirectory import java.io.File class ParallelBuildTest : KotlinIntegrationTestBase() { @@ -18,7 +13,8 @@ class ParallelBuildTest : KotlinIntegrationTestBase() { fun rawString(text: String): String = "\"\"\"$text\"\"\"" val testDataDir = KtTestUtil.getTestDataPathBase() + "/integration/smoke/helloApp" - val programText = """ + val program = ProgramWithDependencyOnCompiler( + tmpdir, """ import org.jetbrains.kotlin.cli.common.messages.MessageRenderer import org.jetbrains.kotlin.cli.common.ExitCode import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler @@ -56,44 +52,12 @@ class ParallelBuildTest : KotlinIntegrationTestBase() { System.exit(1) } } - """.trimIndent() - - val programSource = File(tmpdir, "program.kt") - programSource.writeText(programText) - - val program = File(tmpdir, "program").absolutePath - val (output, exitCode) = AbstractCliTest.executeCompilerGrabOutput( - K2JVMCompiler(), - listOf( - programSource.path, - "-d", program, - "-cp", kotlinPathsForDistDirectory.compilerPath.absolutePath, - ), + """.trimIndent() ) - assertEquals("Compilation failed:\n$output", ExitCode.OK, exitCode) - val result = runJava( - testDataDir, - "-cp", - listOf( - program, - kotlinPathsForDistDirectory.compilerPath.absolutePath, - ).joinToString(File.pathSeparator), - "ProgramKt", - ) - assertEquals( - "Run failed:\n$result", - "OUT:\n0 errors\n\nReturn code: 0\n", result - ) - } + program.compile() - private fun runJava(testDataDir: String, vararg arguments: String): String { - val commandLine = GeneralCommandLine().withWorkDirectory(testDataDir) - commandLine.exePath = getJavaRuntime().absolutePath - commandLine.addParameters(*arguments) - val executionLog = StringBuilder() - val exitCode = runProcess(commandLine, executionLog) - assertEquals("Non-zero exit code ($exitCode):\n$executionLog", 0, exitCode) - return executionLog.toString() + val result = program.run(File(testDataDir)) + assertEquals("0 errors", result) } } diff --git a/compiler/tests/org/jetbrains/kotlin/integration/ProgramWithDependencyOnCompiler.kt b/compiler/tests/org/jetbrains/kotlin/integration/ProgramWithDependencyOnCompiler.kt new file mode 100644 index 00000000000..4e9cdcc7d99 --- /dev/null +++ b/compiler/tests/org/jetbrains/kotlin/integration/ProgramWithDependencyOnCompiler.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.integration + +import junit.framework.TestCase +import org.jetbrains.kotlin.cli.AbstractCliTest +import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler +import org.jetbrains.kotlin.utils.PathUtil +import java.io.File + +class ProgramWithDependencyOnCompiler( + private val tmpdir: File, + private val programText: String, +) { + private lateinit var program: File + + fun compile() { + val programSource = File(tmpdir, "program.kt") + programSource.writeText(programText) + + program = File(tmpdir, "program") + val (output, exitCode) = AbstractCliTest.executeCompilerGrabOutput( + K2JVMCompiler(), + listOf( + programSource.path, + "-d", program.absolutePath, + "-cp", PathUtil.kotlinPathsForDistDirectory.compilerPath.absolutePath, + ), + ) + TestCase.assertEquals("Compilation failed:\n$output", ExitCode.OK, exitCode) + } + + fun run(workingDirectory: File, vararg arguments: String): String = runJava( + workingDirectory, + "-cp", + listOf( + program.absolutePath, + PathUtil.kotlinPathsForDistDirectory.compilerPath.absolutePath, + ).joinToString(File.pathSeparator), + "ProgramKt", + *arguments, + ) + + private fun runJava(workingDirectory: File, vararg arguments: String): String { + val pb = ProcessBuilder() + .directory(workingDirectory) + .command(KotlinIntegrationTestBase.getJavaRuntime().absolutePath, *arguments) + val process = pb.start() + val stdout = String(process.inputStream.readBytes()) + val stderr = String(process.errorStream.readBytes()) + process.waitFor() + + TestCase.assertEquals("Exit code should be 0.", 0, process.exitValue()) + TestCase.assertEquals("Stderr should be empty.", "", stderr) + + return stdout.trimEnd() + } +}