[BTA tests] Add scenario DSL for writing typical IC tests
^KT-61860 In Progress
This commit is contained in:
committed by
Space Team
parent
7141b4dcbb
commit
a234e4149b
+14
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.buildtools.api.tests.compilation.scenario
|
||||
|
||||
interface Scenario {
|
||||
fun module(
|
||||
moduleName: String,
|
||||
dependencies: List<ScenarioModule> = emptyList(),
|
||||
additionalCompilationArguments: List<String> = emptyList(),
|
||||
): ScenarioModule
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.buildtools.api.tests.compilation.scenario
|
||||
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.CompilationOutcome
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.LogLevel
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.Module
|
||||
|
||||
interface ScenarioModule {
|
||||
/**
|
||||
* Performs registered existing file modification.
|
||||
*/
|
||||
fun changeFile(
|
||||
fileName: String,
|
||||
addedOutputs: Set<String> = emptySet(),
|
||||
removedOutputs: Set<String> = emptySet(),
|
||||
transform: (String) -> String,
|
||||
)
|
||||
|
||||
/**
|
||||
* Performs registered file deletion.
|
||||
*/
|
||||
fun deleteFile(fileName: String, removedOutputs: Set<String>)
|
||||
|
||||
/**
|
||||
* Performs registered new file creation.
|
||||
*/
|
||||
fun createFile(fileName: String, addedOutputs: Set<String>, content: String)
|
||||
|
||||
fun compile(
|
||||
forceOutput: LogLevel? = null,
|
||||
assertions: context(Module) CompilationOutcome.() -> Unit = {},
|
||||
)
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.buildtools.api.tests.compilation.scenario
|
||||
|
||||
import org.jetbrains.kotlin.buildtools.api.CompilerExecutionStrategyConfiguration
|
||||
import org.jetbrains.kotlin.buildtools.api.SourcesChanges
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.assertions.assertOutputs
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.BaseCompilationTest
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.CompilationOutcome
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.LogLevel
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.Module
|
||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.Project
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.util.*
|
||||
import kotlin.io.path.*
|
||||
|
||||
private class ScenarioModuleImpl(
|
||||
val module: Module,
|
||||
val outputs: MutableSet<String>,
|
||||
private val strategyConfig: CompilerExecutionStrategyConfiguration,
|
||||
) : ScenarioModule {
|
||||
override fun changeFile(
|
||||
fileName: String,
|
||||
addedOutputs: Set<String>,
|
||||
removedOutputs: Set<String>,
|
||||
transform: (String) -> String,
|
||||
) {
|
||||
val file = module.sourcesDirectory.resolve(fileName)
|
||||
file.writeText(transform(file.readText()))
|
||||
outputs.addAll(addedOutputs)
|
||||
outputs.removeAll(removedOutputs)
|
||||
sourcesChanges = SourcesChanges.Known(
|
||||
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
||||
removedFiles = sourcesChanges.removedFiles,
|
||||
)
|
||||
}
|
||||
|
||||
override fun deleteFile(fileName: String, removedOutputs: Set<String>) {
|
||||
val file = module.sourcesDirectory.resolve(fileName)
|
||||
file.deleteExisting()
|
||||
outputs.removeAll(removedOutputs)
|
||||
sourcesChanges = SourcesChanges.Known(
|
||||
modifiedFiles = sourcesChanges.modifiedFiles,
|
||||
removedFiles = sourcesChanges.removedFiles + file.toFile(),
|
||||
)
|
||||
}
|
||||
|
||||
override fun createFile(fileName: String, addedOutputs: Set<String>, content: String) {
|
||||
val file = module.sourcesDirectory.resolve(fileName)
|
||||
file.writeText(content)
|
||||
outputs.addAll(addedOutputs)
|
||||
sourcesChanges = SourcesChanges.Known(
|
||||
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
||||
removedFiles = sourcesChanges.removedFiles,
|
||||
)
|
||||
}
|
||||
|
||||
var sourcesChanges = SourcesChanges.Known(emptyList(), emptyList())
|
||||
|
||||
override fun compile(
|
||||
forceOutput: LogLevel?,
|
||||
assertions: context(Module) CompilationOutcome.() -> Unit,
|
||||
) {
|
||||
module.compileIncrementally(strategyConfig, sourcesChanges, forceOutput, assertions = {
|
||||
assertions(module, this)
|
||||
assertOutputs(outputs)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class ScenarioDsl(
|
||||
private val project: Project,
|
||||
private val strategyConfig: CompilerExecutionStrategyConfiguration,
|
||||
) : Scenario {
|
||||
@Synchronized
|
||||
override fun module(
|
||||
moduleName: String,
|
||||
dependencies: List<ScenarioModule>,
|
||||
additionalCompilationArguments: List<String>,
|
||||
): ScenarioModule {
|
||||
val transformedDependencies = dependencies.map { (it as ScenarioModuleImpl).module }
|
||||
val module = project.module(moduleName, transformedDependencies, additionalCompilationArguments)
|
||||
module.compileIncrementally(strategyConfig, SourcesChanges.Unknown)
|
||||
val initialOutputs = mutableSetOf<String>()
|
||||
for (file in module.outputDirectory.walk()) {
|
||||
if (!file.isRegularFile()) continue
|
||||
initialOutputs.add(file.relativeTo(module.outputDirectory).toString())
|
||||
}
|
||||
return ScenarioModuleImpl(module, initialOutputs, strategyConfig)
|
||||
}
|
||||
}
|
||||
|
||||
fun BaseCompilationTest.scenario(strategyConfig: CompilerExecutionStrategyConfiguration, action: Scenario.() -> Unit) {
|
||||
action(ScenarioDsl(Project(workingDirectory), strategyConfig))
|
||||
}
|
||||
Reference in New Issue
Block a user