[BTA tests] Add createFile overload for versioned source creation

^KT-61860 In Progress
This commit is contained in:
Alexander.Likhachev
2024-03-07 12:41:41 +01:00
committed by Space Team
parent d3c3e0faad
commit 95b832fa8b
4 changed files with 67 additions and 9 deletions
@@ -21,7 +21,8 @@ interface ScenarioModule {
)
/**
* Performs registered existing file modification.
* Performs registered existing file modification by copying a revision from
* the file located by path "[fileName].[version]" in the module sources directory.
*
* Check the example `ExampleIncrementalScenarioTest.testScenario4` out
*/
@@ -37,9 +38,17 @@ interface ScenarioModule {
/**
* Performs registered new file creation.
*
* Prefer the overload with versioned file modification if it's possible
*/
fun createFile(fileName: String, content: String)
/**
* Performs registered new file creation by copying a revision from
* the file located by path "[fileName].[version]" in the module sources directory.
*/
fun createFile(fileName: String, version: UInt)
fun compile(
forceOutput: LogLevel? = null,
assertions: context(Module, ScenarioModule) CompilationOutcome.() -> Unit = {},
@@ -13,6 +13,8 @@ import org.jetbrains.kotlin.buildtools.api.tests.compilation.assertions.assertOu
import org.jetbrains.kotlin.buildtools.api.tests.compilation.BaseCompilationTest
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.CompilationOutcome
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.*
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.*
internal class ScenarioModuleImpl(
@@ -31,32 +33,50 @@ internal class ScenarioModuleImpl(
}
override fun changeFile(fileName: String, version: UInt) {
val file = module.sourcesDirectory.resolve("$fileName.$version")
writeFile(fileName, file.readText())
val file = module.sourcesDirectory.resolve(fileName)
val chosenRevision = module.sourcesDirectory.resolve("$fileName.$version")
Files.delete(file)
Files.copy(chosenRevision, file)
addToModifiedFiles(file)
}
override fun deleteFile(fileName: String) {
val file = module.sourcesDirectory.resolve(fileName)
file.deleteExisting()
sourcesChanges = SourcesChanges.Known(
modifiedFiles = sourcesChanges.modifiedFiles,
removedFiles = sourcesChanges.removedFiles + file.toFile(),
)
addToRemovedFiles(file)
}
override fun createFile(fileName: String, content: String) {
writeFile(fileName, content)
}
override fun createFile(fileName: String, version: UInt) {
val file = module.sourcesDirectory.resolve(fileName)
val chosenRevision = module.sourcesDirectory.resolve("$fileName.$version")
Files.copy(chosenRevision, file)
addToModifiedFiles(file)
}
private fun writeFile(fileName: String, newContent: String) {
val file = module.sourcesDirectory.resolve(fileName)
file.writeText(newContent)
addToModifiedFiles(file)
}
private fun addToModifiedFiles(file: Path) {
sourcesChanges = SourcesChanges.Known(
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
removedFiles = sourcesChanges.removedFiles,
)
}
private fun addToRemovedFiles(file: Path) {
sourcesChanges = SourcesChanges.Known(
modifiedFiles = sourcesChanges.modifiedFiles,
removedFiles = sourcesChanges.removedFiles + file.toFile(),
)
}
private var sourcesChanges = SourcesChanges.Known(emptyList(), emptyList())
override fun compile(
@@ -0,0 +1,3 @@
fun heyheyhey() {
println("I won't give you any secrets :P")
}
@@ -15,6 +15,8 @@ import org.jetbrains.kotlin.buildtools.api.tests.compilation.scenario.assertNoOu
import org.jetbrains.kotlin.buildtools.api.tests.compilation.scenario.assertRemovedOutputs
import org.jetbrains.kotlin.test.TestMetadata
import org.junit.jupiter.api.DisplayName
import java.util.UUID
import kotlin.random.Random
/**
* The preferred way to write incremental compilation tests.
@@ -28,11 +30,15 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
val module1 = module("jvm-module-1")
// at this moment, the module is already initially built and ready for further incremental compilations
val randomString = UUID.randomUUID().toString()
// Use this overload to create file with some dynamic content
module1.createFile(
"foobar.kt",
//language=kt
"""
fun foobar() {}
fun foobar() {
println("$randomString")
}
""".trimIndent()
)
@@ -92,11 +98,13 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
val module1 = module("jvm-module-1")
val module2 = module("jvm-module-2", listOf(module1))
val randomInt = Random.nextInt()
// Use this overload to modify file dynamically
module1.changeFile(
"bar.kt",
transform = {
//language=kt
it.replace("fun bar()", "fun bar(someNumber: Int = 50)")
it.replace("fun bar()", "fun bar(someNumber: Int = $randomInt)")
}
)
@@ -120,6 +128,7 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
scenario(strategyConfig) {
val module1 = module("jvm-module-1")
// replaces bar.kt with bar.kt.1
module1.changeFile("bar.kt", 1U)
module1.compile {
@@ -128,4 +137,21 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
}
}
}
@DefaultStrategyAgnosticCompilationTest
@DisplayName("Sample scenario DSL IC test with versioned source file creation")
@TestMetadata("jvm-module-1")
fun testScenario5(strategyConfig: CompilerExecutionStrategyConfiguration) {
scenario(strategyConfig) {
val module1 = module("jvm-module-1")
// creates secret.kt from secret.kt.1
module1.createFile("secret.kt", 1U)
module1.compile {
assertCompiledSources("secret.kt")
assertAddedOutputs("SecretKt.class")
}
}
}
}