Minor, extract ProgramWithDependencyOnCompiler for tests
Also use ProcessBuilder instead of GeneralCommandLine for simplicity.
This commit is contained in:
committed by
teamcityserver
parent
d397efb2bd
commit
f63dac26e6
@@ -5,12 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.integration
|
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.test.util.KtTestUtil
|
||||||
import org.jetbrains.kotlin.utils.PathUtil.kotlinPathsForDistDirectory
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class ParallelBuildTest : KotlinIntegrationTestBase() {
|
class ParallelBuildTest : KotlinIntegrationTestBase() {
|
||||||
@@ -18,7 +13,8 @@ class ParallelBuildTest : KotlinIntegrationTestBase() {
|
|||||||
fun rawString(text: String): String = "\"\"\"$text\"\"\""
|
fun rawString(text: String): String = "\"\"\"$text\"\"\""
|
||||||
|
|
||||||
val testDataDir = KtTestUtil.getTestDataPathBase() + "/integration/smoke/helloApp"
|
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.messages.MessageRenderer
|
||||||
import org.jetbrains.kotlin.cli.common.ExitCode
|
import org.jetbrains.kotlin.cli.common.ExitCode
|
||||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
|
||||||
@@ -56,44 +52,12 @@ class ParallelBuildTest : KotlinIntegrationTestBase() {
|
|||||||
System.exit(1)
|
System.exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
""".trimIndent()
|
""".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,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
assertEquals("Compilation failed:\n$output", ExitCode.OK, exitCode)
|
|
||||||
|
|
||||||
val result = runJava(
|
program.compile()
|
||||||
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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun runJava(testDataDir: String, vararg arguments: String): String {
|
val result = program.run(File(testDataDir))
|
||||||
val commandLine = GeneralCommandLine().withWorkDirectory(testDataDir)
|
assertEquals("0 errors", result)
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user