[BTA tests] Use explicit output assertions instead of implicit ones
^KT-61860 In Progress
This commit is contained in:
committed by
Space Team
parent
4facf5ffbc
commit
68cf1bdc57
+8
@@ -14,6 +14,14 @@ import kotlin.io.path.isRegularFile
|
|||||||
import kotlin.io.path.relativeTo
|
import kotlin.io.path.relativeTo
|
||||||
import kotlin.io.path.walk
|
import kotlin.io.path.walk
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Equivalent to [assertNoCompiledSources] with an empty array/set
|
||||||
|
*/
|
||||||
|
context(Module)
|
||||||
|
fun CompilationOutcome.assertNoCompiledSources() {
|
||||||
|
assertCompiledSources()
|
||||||
|
}
|
||||||
|
|
||||||
context(Module)
|
context(Module)
|
||||||
fun CompilationOutcome.assertCompiledSources(vararg expectedCompiledSources: String) {
|
fun CompilationOutcome.assertCompiledSources(vararg expectedCompiledSources: String) {
|
||||||
assertCompiledSources(expectedCompiledSources.toSet())
|
assertCompiledSources(expectedCompiledSources.toSet())
|
||||||
|
|||||||
+3
-5
@@ -15,23 +15,21 @@ interface ScenarioModule {
|
|||||||
*/
|
*/
|
||||||
fun changeFile(
|
fun changeFile(
|
||||||
fileName: String,
|
fileName: String,
|
||||||
addedOutputs: Set<String> = emptySet(),
|
|
||||||
removedOutputs: Set<String> = emptySet(),
|
|
||||||
transform: (String) -> String,
|
transform: (String) -> String,
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs registered file deletion.
|
* Performs registered file deletion.
|
||||||
*/
|
*/
|
||||||
fun deleteFile(fileName: String, removedOutputs: Set<String>)
|
fun deleteFile(fileName: String)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs registered new file creation.
|
* Performs registered new file creation.
|
||||||
*/
|
*/
|
||||||
fun createFile(fileName: String, addedOutputs: Set<String>, content: String)
|
fun createFile(fileName: String, content: String)
|
||||||
|
|
||||||
fun compile(
|
fun compile(
|
||||||
forceOutput: LogLevel? = null,
|
forceOutput: LogLevel? = null,
|
||||||
assertions: context(Module) CompilationOutcome.() -> Unit = {},
|
assertions: context(Module, ScenarioModule) CompilationOutcome.() -> Unit = {},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2024 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.assertions.assertOutputs
|
||||||
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.CompilationOutcome
|
||||||
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.Module
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This assertion has side effects modifying the expected total outputs list!
|
||||||
|
* If you decided to start using it, don't mix it with regular [assertOutputs]
|
||||||
|
*/
|
||||||
|
context(Module, ScenarioModule)
|
||||||
|
fun CompilationOutcome.assertAddedOutputs(vararg addedOutputs: String) {
|
||||||
|
assertAddedOutputs(addedOutputs.toSet())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This assertion has side effects modifying the expected total outputs list!
|
||||||
|
* If you decided to start using it, don't mix it with regular [assertOutputs]
|
||||||
|
*/
|
||||||
|
context(Module, ScenarioModule)
|
||||||
|
fun CompilationOutcome.assertAddedOutputs(addedOutputs: Set<String>) {
|
||||||
|
val outputs = requireScenarioModuleImpl().outputs
|
||||||
|
outputs.addAll(addedOutputs)
|
||||||
|
assertOutputs(outputs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This assertion has side effects modifying the expected total outputs list!
|
||||||
|
* If you decided to start using it, don't mix it with regular [assertOutputs]
|
||||||
|
*/
|
||||||
|
context(Module, ScenarioModule)
|
||||||
|
fun CompilationOutcome.assertRemovedOutputs(vararg removedOutputs: String) {
|
||||||
|
assertRemovedOutputs(removedOutputs.toSet())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This assertion has side effects modifying the expected total outputs list!
|
||||||
|
* If you decided to start using it, don't mix it with regular [assertOutputs]
|
||||||
|
*/
|
||||||
|
context(Module, ScenarioModule)
|
||||||
|
fun CompilationOutcome.assertRemovedOutputs(removedOutputs: Set<String>) {
|
||||||
|
val outputs = requireScenarioModuleImpl().outputs
|
||||||
|
val notPresentOutputs = removedOutputs - outputs
|
||||||
|
assert(notPresentOutputs.isEmpty()) {
|
||||||
|
"The following files were expected to be removed, however they weren't even produced: $notPresentOutputs"
|
||||||
|
}
|
||||||
|
outputs.removeAll(removedOutputs)
|
||||||
|
assertOutputs(outputs)
|
||||||
|
}
|
||||||
|
|
||||||
|
context(Module, ScenarioModule)
|
||||||
|
fun CompilationOutcome.assertNoOutputSetChanges() {
|
||||||
|
val outputs = requireScenarioModuleImpl().outputs
|
||||||
|
assertOutputs(outputs)
|
||||||
|
}
|
||||||
|
|
||||||
|
context(ScenarioModule)
|
||||||
|
private fun requireScenarioModuleImpl() =
|
||||||
|
(this@ScenarioModule as? ScenarioModuleImpl ?: error("Expected an instance of ScenarioModuleImpl"))
|
||||||
+5
-12
@@ -18,41 +18,35 @@ import kotlin.io.path.*
|
|||||||
|
|
||||||
internal class ScenarioModuleImpl(
|
internal class ScenarioModuleImpl(
|
||||||
internal val module: Module,
|
internal val module: Module,
|
||||||
private val outputs: MutableSet<String>,
|
internal val outputs: MutableSet<String>,
|
||||||
private val strategyConfig: CompilerExecutionStrategyConfiguration,
|
private val strategyConfig: CompilerExecutionStrategyConfiguration,
|
||||||
private val compilationOptionsModifier: ((JvmCompilationConfiguration) -> Unit)?,
|
private val compilationOptionsModifier: ((JvmCompilationConfiguration) -> Unit)?,
|
||||||
private val incrementalCompilationOptionsModifier: ((IncrementalJvmCompilationConfiguration<*>) -> Unit)?,
|
private val incrementalCompilationOptionsModifier: ((IncrementalJvmCompilationConfiguration<*>) -> Unit)?,
|
||||||
) : ScenarioModule {
|
) : ScenarioModule {
|
||||||
override fun changeFile(
|
override fun changeFile(
|
||||||
fileName: String,
|
fileName: String,
|
||||||
addedOutputs: Set<String>,
|
|
||||||
removedOutputs: Set<String>,
|
|
||||||
transform: (String) -> String,
|
transform: (String) -> String,
|
||||||
) {
|
) {
|
||||||
val file = module.sourcesDirectory.resolve(fileName)
|
val file = module.sourcesDirectory.resolve(fileName)
|
||||||
file.writeText(transform(file.readText()))
|
file.writeText(transform(file.readText()))
|
||||||
outputs.addAll(addedOutputs)
|
|
||||||
outputs.removeAll(removedOutputs)
|
|
||||||
sourcesChanges = SourcesChanges.Known(
|
sourcesChanges = SourcesChanges.Known(
|
||||||
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
||||||
removedFiles = sourcesChanges.removedFiles,
|
removedFiles = sourcesChanges.removedFiles,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deleteFile(fileName: String, removedOutputs: Set<String>) {
|
override fun deleteFile(fileName: String) {
|
||||||
val file = module.sourcesDirectory.resolve(fileName)
|
val file = module.sourcesDirectory.resolve(fileName)
|
||||||
file.deleteExisting()
|
file.deleteExisting()
|
||||||
outputs.removeAll(removedOutputs)
|
|
||||||
sourcesChanges = SourcesChanges.Known(
|
sourcesChanges = SourcesChanges.Known(
|
||||||
modifiedFiles = sourcesChanges.modifiedFiles,
|
modifiedFiles = sourcesChanges.modifiedFiles,
|
||||||
removedFiles = sourcesChanges.removedFiles + file.toFile(),
|
removedFiles = sourcesChanges.removedFiles + file.toFile(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createFile(fileName: String, addedOutputs: Set<String>, content: String) {
|
override fun createFile(fileName: String, content: String) {
|
||||||
val file = module.sourcesDirectory.resolve(fileName)
|
val file = module.sourcesDirectory.resolve(fileName)
|
||||||
file.writeText(content)
|
file.writeText(content)
|
||||||
outputs.addAll(addedOutputs)
|
|
||||||
sourcesChanges = SourcesChanges.Known(
|
sourcesChanges = SourcesChanges.Known(
|
||||||
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
||||||
removedFiles = sourcesChanges.removedFiles,
|
removedFiles = sourcesChanges.removedFiles,
|
||||||
@@ -63,7 +57,7 @@ internal class ScenarioModuleImpl(
|
|||||||
|
|
||||||
override fun compile(
|
override fun compile(
|
||||||
forceOutput: LogLevel?,
|
forceOutput: LogLevel?,
|
||||||
assertions: context(Module) CompilationOutcome.() -> Unit,
|
assertions: context(Module, ScenarioModule) CompilationOutcome.() -> Unit,
|
||||||
) {
|
) {
|
||||||
module.compileIncrementally(
|
module.compileIncrementally(
|
||||||
sourcesChanges,
|
sourcesChanges,
|
||||||
@@ -72,8 +66,7 @@ internal class ScenarioModuleImpl(
|
|||||||
compilationConfigAction = { compilationOptionsModifier?.invoke(it) },
|
compilationConfigAction = { compilationOptionsModifier?.invoke(it) },
|
||||||
incrementalCompilationConfigAction = { incrementalCompilationOptionsModifier?.invoke(it) },
|
incrementalCompilationConfigAction = { incrementalCompilationOptionsModifier?.invoke(it) },
|
||||||
assertions = {
|
assertions = {
|
||||||
assertions(module, this)
|
assertions(module, this@ScenarioModuleImpl, this)
|
||||||
assertOutputs(outputs)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-6
@@ -7,9 +7,13 @@ package org.jetbrains.kotlin.buildtools.api.tests.compilation
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.buildtools.api.CompilerExecutionStrategyConfiguration
|
import org.jetbrains.kotlin.buildtools.api.CompilerExecutionStrategyConfiguration
|
||||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.assertions.assertCompiledSources
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.assertions.assertCompiledSources
|
||||||
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.assertions.assertNoCompiledSources
|
||||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.scenario.scenario
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.scenario.scenario
|
||||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.BaseCompilationTest
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.BaseCompilationTest
|
||||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.DefaultStrategyAgnosticCompilationTest
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.DefaultStrategyAgnosticCompilationTest
|
||||||
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.scenario.assertAddedOutputs
|
||||||
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.scenario.assertNoOutputSetChanges
|
||||||
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.scenario.assertRemovedOutputs
|
||||||
import org.jetbrains.kotlin.test.TestMetadata
|
import org.jetbrains.kotlin.test.TestMetadata
|
||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
|
||||||
@@ -23,7 +27,6 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
|
|||||||
|
|
||||||
module1.createFile(
|
module1.createFile(
|
||||||
"foobar.kt",
|
"foobar.kt",
|
||||||
addedOutputs = setOf("FoobarKt.class"),
|
|
||||||
//language=kt
|
//language=kt
|
||||||
"""
|
"""
|
||||||
fun foobar() {}
|
fun foobar() {}
|
||||||
@@ -32,15 +35,16 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
|
|||||||
|
|
||||||
module1.compile {
|
module1.compile {
|
||||||
assertCompiledSources("foobar.kt")
|
assertCompiledSources("foobar.kt")
|
||||||
|
assertAddedOutputs("FoobarKt.class")
|
||||||
}
|
}
|
||||||
|
|
||||||
module1.deleteFile(
|
module1.deleteFile(
|
||||||
"foobar.kt",
|
"foobar.kt",
|
||||||
removedOutputs = setOf("FoobarKt.class"),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
module1.compile {
|
module1.compile {
|
||||||
assertCompiledSources()
|
assertNoCompiledSources()
|
||||||
|
assertRemovedOutputs("FoobarKt.class")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,7 +58,6 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
|
|||||||
|
|
||||||
module1.createFile(
|
module1.createFile(
|
||||||
"foobar.kt",
|
"foobar.kt",
|
||||||
addedOutputs = setOf("FoobarKt.class"),
|
|
||||||
//language=kt
|
//language=kt
|
||||||
"""
|
"""
|
||||||
fun foobar() {}
|
fun foobar() {}
|
||||||
@@ -63,15 +66,16 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
|
|||||||
|
|
||||||
module1.compile {
|
module1.compile {
|
||||||
assertCompiledSources("foobar.kt")
|
assertCompiledSources("foobar.kt")
|
||||||
|
assertAddedOutputs("FoobarKt.class")
|
||||||
}
|
}
|
||||||
|
|
||||||
module1.deleteFile(
|
module1.deleteFile(
|
||||||
"foobar.kt",
|
"foobar.kt",
|
||||||
removedOutputs = setOf("FoobarKt.class"),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
module1.compile {
|
module1.compile {
|
||||||
assertCompiledSources()
|
assertNoCompiledSources()
|
||||||
|
assertRemovedOutputs("FoobarKt.class")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,10 +98,12 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
|
|||||||
|
|
||||||
module1.compile {
|
module1.compile {
|
||||||
assertCompiledSources("bar.kt")
|
assertCompiledSources("bar.kt")
|
||||||
|
assertNoOutputSetChanges()
|
||||||
}
|
}
|
||||||
|
|
||||||
module2.compile {
|
module2.compile {
|
||||||
assertCompiledSources("b.kt")
|
assertCompiledSources("b.kt")
|
||||||
|
assertNoOutputSetChanges()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user