[K/N][tests] Extracted common utility functions

This commit is contained in:
Igor Chevdar
2022-12-20 22:26:38 +02:00
committed by Space Team
parent 758e9ea9ae
commit ae56f2686a
3 changed files with 143 additions and 163 deletions
@@ -7,27 +7,15 @@ package org.jetbrains.kotlin.konan.blackboxtest
import com.intellij.testFramework.TestDataPath
import org.jetbrains.kotlin.konan.blackboxtest.CachesAutoBuildTest.Companion.TEST_SUITE_PATH
import org.jetbrains.kotlin.konan.blackboxtest.support.*
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCase.WithTestRunnerExtras
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExecutableCompilation
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExistingDependency
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.LibraryCompilation
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact.Executable
import org.jetbrains.kotlin.konan.blackboxtest.support.EnforcedHostTarget
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCompilerArgs
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact.KLIB
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependency
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependencyType.Library
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationResult.Companion.assertSuccess
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationResult.Success
import org.jetbrains.kotlin.konan.blackboxtest.support.runner.TestRunChecks
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.*
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.CacheMode
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.KotlinNativeTargets
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.SimpleTestDirectories
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.Timeouts
import org.jetbrains.kotlin.konan.blackboxtest.support.util.LAUNCHER_MODULE_NAME
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.OptimizationMode
import org.jetbrains.kotlin.test.TestMetadata
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertFalse
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import java.io.File
@@ -61,78 +49,18 @@ class CachesAutoBuildTest : AbstractNativeSimpleTest() {
assertFalse(autoCacheDir.resolve(cacheFlavor).resolve("userLib").exists())
}
private fun compileToLibrary(sourcesDir: File, outputDir: File, vararg dependencies: KLIB): KLIB {
val testCase: TestCase = generateTestCaseWithSingleModule(sourcesDir)
val compilationResult: Success<out KLIB> = testCase.compileToLibrary(outputDir, dependencies.map { it.asLibraryDependency() })
return compilationResult.resultingArtifact
}
private fun compileToExecutable(sourcesDir: File, autoCacheFrom: File, vararg dependencies: KLIB): Executable {
val testCase: TestCase = generateTestCaseWithSingleModule(
private fun compileToExecutable(sourcesDir: File, autoCacheFrom: File, vararg dependencies: KLIB) =
compileToExecutable(
sourcesDir,
freeCompilerArgs = TestCompilerArgs(
listOf(
"-Xauto-cache-from=${autoCacheFrom.absolutePath}",
"-Xauto-cache-dir=${autoCacheDir.absolutePath}",
)
)
),
*dependencies
)
val compilationResult = testCase.compileToExecutable(dependencies.map { it.asLibraryDependency() })
return compilationResult.resultingArtifact
}
private fun generateTestCaseWithSingleModule(moduleDir: File?, freeCompilerArgs: TestCompilerArgs = TestCompilerArgs.EMPTY): TestCase {
val moduleName: String = moduleDir?.name ?: LAUNCHER_MODULE_NAME
val module = TestModule.Exclusive(moduleName, emptySet(), emptySet())
moduleDir?.walkTopDown()
?.filter { it.isFile && it.extension == "kt" }
?.forEach { file -> module.files += TestFile.createCommitted(file, module) }
return TestCase(
id = TestCaseId.Named(moduleName),
kind = TestKind.STANDALONE,
modules = setOf(module),
freeCompilerArgs = freeCompilerArgs,
nominalPackageName = PackageName.EMPTY,
checks = TestRunChecks.Default(testRunSettings.get<Timeouts>().executionTimeout),
extras = DEFAULT_EXTRAS
).apply {
initialize(null, null)
}
}
private fun TestCase.compileToLibrary(dir: File, vararg dependencies: TestCompilationDependency<*>) =
compileToLibrary(dir, dependencies.asList())
private fun TestCase.compileToLibrary(dir: File, dependencies: List<TestCompilationDependency<*>>): Success<out KLIB> {
val compilation = LibraryCompilation(
settings = testRunSettings,
freeCompilerArgs = freeCompilerArgs,
sourceModules = modules,
dependencies = dependencies,
expectedArtifact = toLibraryArtifact(dir)
)
return compilation.result.assertSuccess()
}
private fun TestCase.compileToExecutable(vararg dependencies: TestCompilationDependency<*>) =
compileToExecutable(dependencies.asList())
private fun TestCase.compileToExecutable(dependencies: List<TestCompilationDependency<*>>): Success<out Executable> {
val compilation = ExecutableCompilation(
settings = testRunSettings,
freeCompilerArgs = freeCompilerArgs,
sourceModules = modules,
extras = DEFAULT_EXTRAS,
dependencies = dependencies,
expectedArtifact = toExecutableArtifact()
)
return compilation.result.assertSuccess()
}
private val buildDir: File get() = testRunSettings.get<SimpleTestDirectories>().testBuildDir
private val autoCacheDir: File get() = buildDir.resolve("__auto_cache__")
private val cacheFlavor: String
get() = CacheMode.computeCacheDirName(
@@ -141,15 +69,7 @@ class CachesAutoBuildTest : AbstractNativeSimpleTest() {
testRunSettings.get<OptimizationMode>() == OptimizationMode.DEBUG
)
private fun TestCase.toLibraryArtifact(dir: File) = KLIB(dir.resolve(modules.first().name + ".klib"))
private fun toExecutableArtifact() =
Executable(buildDir.resolve("app." + testRunSettings.get<KotlinNativeTargets>().testTarget.family.exeSuffix))
private fun KLIB.asLibraryDependency() = ExistingDependency(this, Library)
companion object {
const val TEST_SUITE_PATH = "native/native.tests/testData/caches/testAutoBuild"
private val DEFAULT_EXTRAS = WithTestRunnerExtras(TestRunnerType.DEFAULT)
}
}
@@ -9,26 +9,14 @@ import com.intellij.openapi.util.text.StringUtil.convertLineSeparators
import com.intellij.testFramework.TestDataFile
import com.intellij.testFramework.TestDataPath
import org.jetbrains.kotlin.konan.blackboxtest.InfrastructureDumpedTestListingTest.Companion.TEST_SUITE_PATH
import org.jetbrains.kotlin.konan.blackboxtest.support.*
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCase.WithTestRunnerExtras
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExecutableCompilation
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExistingDependency
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.LibraryCompilation
import org.jetbrains.kotlin.konan.blackboxtest.support.EnforcedHostTarget
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCase
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact.Executable
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact.KLIB
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependency
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependencyType.IncludedLibrary
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependencyType.Library
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationResult.Companion.assertSuccess
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationResult.Success
import org.jetbrains.kotlin.konan.blackboxtest.support.runner.TestExecutable
import org.jetbrains.kotlin.konan.blackboxtest.support.runner.TestRunChecks
import org.jetbrains.kotlin.konan.blackboxtest.support.runner.TestRunners
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.KotlinNativeTargets
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.SimpleTestDirectories
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.Timeouts
import org.jetbrains.kotlin.konan.blackboxtest.support.util.DumpedTestListing
import org.jetbrains.kotlin.konan.blackboxtest.support.util.LAUNCHER_MODULE_NAME
import org.jetbrains.kotlin.test.TestMetadata
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertTrue
@@ -57,9 +45,7 @@ class InfrastructureDumpedTestListingTest : AbstractNativeSimpleTest() {
private fun doTest(@TestDataFile testDataPath: String, fromSources: Boolean) {
val rootDir = File(testDataPath)
val fooTestCase: TestCase = generateTestCaseWithSingleModule(rootDir.resolve("foo"))
val fooCompilationResult: Success<out KLIB> = fooTestCase.compileToLibrary()
val fooLibrary: KLIB = fooCompilationResult.resultingArtifact
val fooLibrary = compileToLibrary(rootDir.resolve("foo"))
val barTestCase: TestCase = generateTestCaseWithSingleModule(rootDir.resolve("bar"))
@@ -68,13 +54,14 @@ class InfrastructureDumpedTestListingTest : AbstractNativeSimpleTest() {
if (fromSources) {
executableTestCase = barTestCase
executableCompilationResult = barTestCase.compileToExecutable(fooLibrary.asLibraryDependency())
executableCompilationResult = compileToExecutable(barTestCase, fooLibrary.asLibraryDependency())
} else {
val barCompilationResult: Success<out KLIB> = barTestCase.compileToLibrary(fooLibrary.asLibraryDependency())
val barCompilationResult: Success<out KLIB> = compileToLibrary(barTestCase, fooLibrary.asLibraryDependency())
val barLibrary: KLIB = barCompilationResult.resultingArtifact
executableTestCase = generateTestCaseWithSingleModule(moduleDir = null) // No sources.
executableCompilationResult = executableTestCase.compileToExecutable(
executableCompilationResult = compileToExecutable(
executableTestCase,
fooLibrary.asLibraryDependency(),
barLibrary.asIncludedLibraryDependency()
)
@@ -99,59 +86,6 @@ class InfrastructureDumpedTestListingTest : AbstractNativeSimpleTest() {
runExecutableAndVerify(executableTestCase, testExecutable) // <-- run executable and verify
}
private fun generateTestCaseWithSingleModule(moduleDir: File?): TestCase {
val moduleName: String = moduleDir?.name ?: LAUNCHER_MODULE_NAME
val module = TestModule.Exclusive(moduleName, emptySet(), emptySet())
moduleDir?.walkTopDown()
?.filter { it.isFile && it.extension == "kt" }
?.forEach { file -> module.files += TestFile.createCommitted(file, module) }
return TestCase(
id = TestCaseId.Named(moduleName),
kind = TestKind.STANDALONE,
modules = setOf(module),
freeCompilerArgs = TestCompilerArgs.EMPTY,
nominalPackageName = PackageName.EMPTY,
checks = TestRunChecks.Default(testRunSettings.get<Timeouts>().executionTimeout),
extras = DEFAULT_EXTRAS
).apply {
initialize(null, null)
}
}
private fun TestCase.compileToLibrary(vararg dependencies: TestCompilationDependency<*>): Success<out KLIB> {
val compilation = LibraryCompilation(
settings = testRunSettings,
freeCompilerArgs = freeCompilerArgs,
sourceModules = modules,
dependencies = dependencies.toList(),
expectedArtifact = toLibraryArtifact()
)
return compilation.result.assertSuccess()
}
private fun TestCase.compileToExecutable(vararg dependencies: TestCompilationDependency<*>): Success<out Executable> {
val compilation = ExecutableCompilation(
settings = testRunSettings,
freeCompilerArgs = freeCompilerArgs,
sourceModules = modules,
extras = DEFAULT_EXTRAS,
dependencies = dependencies.toList(),
expectedArtifact = toExecutableArtifact()
)
return compilation.result.assertSuccess()
}
private val buildDir: File get() = testRunSettings.get<SimpleTestDirectories>().testBuildDir
private fun TestCase.toLibraryArtifact() = KLIB(buildDir.resolve(modules.first().name + ".klib"))
private fun toExecutableArtifact() =
Executable(buildDir.resolve("app." + testRunSettings.get<KotlinNativeTargets>().testTarget.family.exeSuffix))
private fun KLIB.asLibraryDependency() = ExistingDependency(this, Library)
private fun KLIB.asIncludedLibraryDependency() = ExistingDependency(this, IncludedLibrary)
private fun assertDumpFilesEqual(expected: File, actual: File) {
val expectedDumpFileContents = convertLineSeparators(expected.readText().trimEnd())
val actualDumpFileContents = convertLineSeparators(actual.readText().trimEnd())
@@ -169,7 +103,5 @@ class InfrastructureDumpedTestListingTest : AbstractNativeSimpleTest() {
const val TEST_SUITE_PATH = "native/native.tests/testData/infrastructure"
const val TEST_CASE_NAME = "testListing"
const val TEST_CASE_PATH = "$TEST_SUITE_PATH/$TEST_CASE_NAME"
private val DEFAULT_EXTRAS = WithTestRunnerExtras(TestRunnerType.DEFAULT)
}
}
@@ -0,0 +1,128 @@
/*
* Copyright 2010-2022 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.konan.blackboxtest
import org.jetbrains.kotlin.konan.blackboxtest.support.PackageName
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCase
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCaseId
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCompilerArgs
import org.jetbrains.kotlin.konan.blackboxtest.support.TestFile
import org.jetbrains.kotlin.konan.blackboxtest.support.TestKind
import org.jetbrains.kotlin.konan.blackboxtest.support.TestModule
import org.jetbrains.kotlin.konan.blackboxtest.support.TestRunnerType
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExecutableCompilation
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.ExistingDependency
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.LibraryCompilation
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependency
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationDependencyType
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationResult
import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationResult.Companion.assertSuccess
import org.jetbrains.kotlin.konan.blackboxtest.support.runner.TestRunChecks
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.KotlinNativeTargets
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.SimpleTestDirectories
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.Timeouts
import org.jetbrains.kotlin.konan.blackboxtest.support.util.LAUNCHER_MODULE_NAME
import java.io.File
private val DEFAULT_EXTRAS = TestCase.WithTestRunnerExtras(TestRunnerType.DEFAULT)
internal val AbstractNativeSimpleTest.buildDir: File get() = testRunSettings.get<SimpleTestDirectories>().testBuildDir
internal fun TestCompilationArtifact.KLIB.asLibraryDependency() =
ExistingDependency(this, TestCompilationDependencyType.Library)
internal fun TestCompilationArtifact.KLIB.asIncludedLibraryDependency() =
ExistingDependency(this, TestCompilationDependencyType.IncludedLibrary)
internal fun AbstractNativeSimpleTest.compileToLibrary(sourcesDir: File, vararg dependencies: TestCompilationArtifact.KLIB) =
compileToLibrary(sourcesDir, buildDir, *dependencies)
internal fun AbstractNativeSimpleTest.compileToLibrary(testCase: TestCase, vararg dependencies: TestCompilationDependency<*>) =
compileToLibrary(testCase, buildDir, dependencies.asList())
internal fun AbstractNativeSimpleTest.compileToLibrary(
sourcesDir: File,
outputDir: File,
vararg dependencies: TestCompilationArtifact.KLIB
): TestCompilationArtifact.KLIB {
val testCase: TestCase = generateTestCaseWithSingleModule(sourcesDir)
val compilationResult = compileToLibrary(testCase, outputDir, dependencies.map { it.asLibraryDependency() })
return compilationResult.resultingArtifact
}
internal fun AbstractNativeSimpleTest.compileToExecutable(
sourcesDir: File,
freeCompilerArgs: TestCompilerArgs,
vararg dependencies: TestCompilationArtifact.KLIB
): TestCompilationArtifact.Executable {
val testCase: TestCase = generateTestCaseWithSingleModule(sourcesDir, freeCompilerArgs)
val compilationResult = compileToExecutable(testCase, dependencies.map { it.asLibraryDependency() })
return compilationResult.resultingArtifact
}
internal fun AbstractNativeSimpleTest.compileToExecutable(testCase: TestCase, vararg dependencies: TestCompilationDependency<*>) =
compileToExecutable(testCase, dependencies.asList())
internal fun AbstractNativeSimpleTest.generateTestCaseWithSingleModule(
moduleDir: File?,
freeCompilerArgs: TestCompilerArgs = TestCompilerArgs.EMPTY
): TestCase {
val moduleName: String = moduleDir?.name ?: LAUNCHER_MODULE_NAME
val module = TestModule.Exclusive(moduleName, emptySet(), emptySet())
moduleDir?.walkTopDown()
?.filter { it.isFile && it.extension == "kt" }
?.forEach { file -> module.files += TestFile.createCommitted(file, module) }
return TestCase(
id = TestCaseId.Named(moduleName),
kind = TestKind.STANDALONE,
modules = setOf(module),
freeCompilerArgs = freeCompilerArgs,
nominalPackageName = PackageName.EMPTY,
checks = TestRunChecks.Default(testRunSettings.get<Timeouts>().executionTimeout),
extras = DEFAULT_EXTRAS
).apply {
initialize(null, null)
}
}
private fun AbstractNativeSimpleTest.compileToLibrary(
testCase: TestCase,
dir: File,
dependencies: List<TestCompilationDependency<*>>
): TestCompilationResult.Success<out TestCompilationArtifact.KLIB> {
val compilation = LibraryCompilation(
settings = testRunSettings,
freeCompilerArgs = testCase.freeCompilerArgs,
sourceModules = testCase.modules,
dependencies = dependencies,
expectedArtifact = getLibraryArtifact(testCase, dir)
)
return compilation.result.assertSuccess()
}
private fun AbstractNativeSimpleTest.compileToExecutable(
testCase: TestCase,
dependencies: List<TestCompilationDependency<*>>
): TestCompilationResult.Success<out TestCompilationArtifact.Executable> {
val compilation = ExecutableCompilation(
settings = testRunSettings,
freeCompilerArgs = testCase.freeCompilerArgs,
sourceModules = testCase.modules,
extras = DEFAULT_EXTRAS,
dependencies = dependencies,
expectedArtifact = getExecutableArtifact()
)
return compilation.result.assertSuccess()
}
private fun getLibraryArtifact(testCase: TestCase, dir: File) =
TestCompilationArtifact.KLIB(dir.resolve(testCase.modules.first().name + ".klib"))
private fun AbstractNativeSimpleTest.getExecutableArtifact() =
TestCompilationArtifact.Executable(buildDir.resolve("app." + testRunSettings.get<KotlinNativeTargets>().testTarget.family.exeSuffix))