[Native][tests] Split TestExecutable into TestExecutable and TestRun

The latter keeps the list of run parameters now. It's assumed that the same TestExecutable instance can be run with different TestRuns.
This commit is contained in:
Dmitriy Dolovov
2021-11-11 22:46:33 +03:00
parent 6cef8c1f91
commit 4bc48ea466
5 changed files with 26 additions and 20 deletions
@@ -11,9 +11,9 @@ import org.junit.jupiter.api.extension.ExtendWith
@ExtendWith(NativeBlackBoxTestSupport::class)
abstract class AbstractNativeBlackBoxTest {
internal lateinit var blackBoxTestProvider: TestProvider
internal lateinit var testRunProvider: TestRunProvider
fun runTest(@TestDataFile testDataFilePath: String) {
blackBoxTestProvider.getTestByTestDataFile(getAbsoluteFile(testDataFilePath)).runAndVerify()
testRunProvider.getSingleTestRunForTestDataFile(getAbsoluteFile(testDataFilePath)).runAndVerify()
}
}
@@ -28,14 +28,14 @@ class NativeBlackBoxTestSupport : BeforeEachCallback {
* not allow accessing its parent test instance in case there are inner test classes in the generated test suite.
*/
override fun beforeEach(extensionContext: ExtensionContext) = with(extensionContext) {
enclosingTestInstance.blackBoxTestProvider = getOrCreateBlackBoxTestProvider()
enclosingTestInstance.testRunProvider = getOrCreateTestRunProvider()
}
companion object {
private val NAMESPACE = ExtensionContext.Namespace.create(NativeBlackBoxTestSupport::class.java.simpleName)
/** Creates a single instance of [TestProvider] per test class. */
private fun ExtensionContext.getOrCreateBlackBoxTestProvider(): TestProvider =
/** Creates a single instance of [TestRunProvider] per test class. */
private fun ExtensionContext.getOrCreateTestRunProvider(): TestRunProvider =
root.getStore(NAMESPACE).getOrComputeIfAbsent(enclosingTestClass.sanitizedName) { sanitizedName ->
val globalEnvironment = getOrCreateGlobalEnvironment()
@@ -71,7 +71,7 @@ class NativeBlackBoxTestSupport : BeforeEachCallback {
val testCaseGroupProvider = requiredTestClass.createTestCaseGroupProvider(environment)
TestProvider(environment, testCaseGroupProvider)
TestRunProvider(environment, testCaseGroupProvider)
}.cast()
private fun ExtensionContext.getOrCreateGlobalEnvironment(): GlobalTestEnvironment =
@@ -8,6 +8,17 @@ package org.jetbrains.kotlin.konan.blackboxtest
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.io.File
internal class TestExecutable(
val executableFile: File,
val origin: TestOrigin.SingleTestDataFile,
val loggedCompilerCall: LoggedData.CompilerCall
)
internal class TestRun(
val executable: TestExecutable,
val runParameters: List<TestRunParameter>
)
internal sealed interface TestRunParameter {
fun applyTo(programArgs: MutableList<String>)
@@ -32,13 +43,6 @@ internal sealed interface TestRunParameter {
}
}
internal class TestExecutable(
val executableFile: File,
val runParameters: List<TestRunParameter>,
val origin: TestOrigin.SingleTestDataFile,
val loggedCompilerCall: LoggedData.CompilerCall
)
internal inline fun <reified T : TestRunParameter> List<TestRunParameter>.has(): Boolean =
firstIsInstanceOrNull<T>() != null
@@ -15,14 +15,14 @@ import org.junit.jupiter.api.extension.ExtensionContext
import java.io.File
import java.util.concurrent.ConcurrentHashMap
internal class TestProvider(
internal class TestRunProvider(
private val environment: TestEnvironment,
private val testCaseGroupProvider: TestCaseGroupProvider
) : ExtensionContext.Store.CloseableResource {
private val compilationFactory = TestCompilationFactory(environment)
private val cachedCompilations: MutableMap<TestCompilationCacheKey, TestCompilation> = ConcurrentHashMap()
fun getTestByTestDataFile(testDataFile: File): TestExecutable {
fun getSingleTestRunForTestDataFile(testDataFile: File): TestRun {
environment.assertNotDisposed()
val testDataDir = testDataFile.parentFile
@@ -49,7 +49,9 @@ internal class TestProvider(
}
}
}
val (executableFile, loggedCompilerCall) = testCompilation.result.assertSuccess() // <-- Compilation happens here.
val executable = TestExecutable(executableFile, testCase.origin, loggedCompilerCall)
val runParameters = when (testCase.kind) {
TestKind.STANDALONE_NO_TR -> listOfNotNull(
@@ -67,7 +69,7 @@ internal class TestProvider(
)
}
return TestExecutable(executableFile, runParameters, testCase.origin, loggedCompilerCall)
return TestRun(executable, runParameters)
}
override fun close() {
@@ -8,14 +8,14 @@ package org.jetbrains.kotlin.konan.blackboxtest
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals
import kotlin.properties.Delegates
internal fun TestExecutable.runAndVerify() {
val programArgs = mutableListOf<String>(executableFile.path)
internal fun TestRun.runAndVerify() {
val programArgs = mutableListOf<String>(executable.executableFile.path)
runParameters.forEach { it.applyTo(programArgs) }
val loggedParameters = LoggedData.TestRunParameters(loggedCompilerCall, origin, programArgs, runParameters)
val loggedParameters = LoggedData.TestRunParameters(executable.loggedCompilerCall, executable.origin, programArgs, runParameters)
val startTimeMillis = System.currentTimeMillis()
val process = ProcessBuilder(programArgs).directory(executableFile.parentFile).start()
val process = ProcessBuilder(programArgs).directory(executable.executableFile.parentFile).start()
runParameters.get<TestRunParameter.WithInputData> {
process.outputStream.write(inputDataFile.readBytes())
process.outputStream.flush()