[BTA tests] Add createFile overload for versioned source creation
^KT-61860 In Progress
This commit is contained in:
committed by
Space Team
parent
d3c3e0faad
commit
95b832fa8b
+10
-1
@@ -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
|
* Check the example `ExampleIncrementalScenarioTest.testScenario4` out
|
||||||
*/
|
*/
|
||||||
@@ -37,9 +38,17 @@ interface ScenarioModule {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs registered new file creation.
|
* Performs registered new file creation.
|
||||||
|
*
|
||||||
|
* Prefer the overload with versioned file modification if it's possible
|
||||||
*/
|
*/
|
||||||
fun createFile(fileName: String, content: String)
|
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(
|
fun compile(
|
||||||
forceOutput: LogLevel? = null,
|
forceOutput: LogLevel? = null,
|
||||||
assertions: context(Module, ScenarioModule) CompilationOutcome.() -> Unit = {},
|
assertions: context(Module, ScenarioModule) CompilationOutcome.() -> Unit = {},
|
||||||
|
|||||||
+26
-6
@@ -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.BaseCompilationTest
|
||||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.CompilationOutcome
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.CompilationOutcome
|
||||||
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.*
|
import org.jetbrains.kotlin.buildtools.api.tests.compilation.model.*
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
import kotlin.io.path.*
|
import kotlin.io.path.*
|
||||||
|
|
||||||
internal class ScenarioModuleImpl(
|
internal class ScenarioModuleImpl(
|
||||||
@@ -31,32 +33,50 @@ internal class ScenarioModuleImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun changeFile(fileName: String, version: UInt) {
|
override fun changeFile(fileName: String, version: UInt) {
|
||||||
val file = module.sourcesDirectory.resolve("$fileName.$version")
|
val file = module.sourcesDirectory.resolve(fileName)
|
||||||
writeFile(fileName, file.readText())
|
val chosenRevision = module.sourcesDirectory.resolve("$fileName.$version")
|
||||||
|
Files.delete(file)
|
||||||
|
Files.copy(chosenRevision, file)
|
||||||
|
addToModifiedFiles(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deleteFile(fileName: String) {
|
override fun deleteFile(fileName: String) {
|
||||||
val file = module.sourcesDirectory.resolve(fileName)
|
val file = module.sourcesDirectory.resolve(fileName)
|
||||||
file.deleteExisting()
|
file.deleteExisting()
|
||||||
sourcesChanges = SourcesChanges.Known(
|
addToRemovedFiles(file)
|
||||||
modifiedFiles = sourcesChanges.modifiedFiles,
|
|
||||||
removedFiles = sourcesChanges.removedFiles + file.toFile(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createFile(fileName: String, content: String) {
|
override fun createFile(fileName: String, content: String) {
|
||||||
writeFile(fileName, content)
|
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) {
|
private fun writeFile(fileName: String, newContent: String) {
|
||||||
val file = module.sourcesDirectory.resolve(fileName)
|
val file = module.sourcesDirectory.resolve(fileName)
|
||||||
file.writeText(newContent)
|
file.writeText(newContent)
|
||||||
|
addToModifiedFiles(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addToModifiedFiles(file: Path) {
|
||||||
sourcesChanges = SourcesChanges.Known(
|
sourcesChanges = SourcesChanges.Known(
|
||||||
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
modifiedFiles = sourcesChanges.modifiedFiles + file.toFile(),
|
||||||
removedFiles = sourcesChanges.removedFiles,
|
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())
|
private var sourcesChanges = SourcesChanges.Known(emptyList(), emptyList())
|
||||||
|
|
||||||
override fun compile(
|
override fun compile(
|
||||||
|
|||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
fun heyheyhey() {
|
||||||
|
println("I won't give you any secrets :P")
|
||||||
|
}
|
||||||
+28
-2
@@ -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.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
|
||||||
|
import java.util.UUID
|
||||||
|
import kotlin.random.Random
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The preferred way to write incremental compilation tests.
|
* The preferred way to write incremental compilation tests.
|
||||||
@@ -28,11 +30,15 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
|
|||||||
val module1 = module("jvm-module-1")
|
val module1 = module("jvm-module-1")
|
||||||
// at this moment, the module is already initially built and ready for further incremental compilations
|
// 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(
|
module1.createFile(
|
||||||
"foobar.kt",
|
"foobar.kt",
|
||||||
//language=kt
|
//language=kt
|
||||||
"""
|
"""
|
||||||
fun foobar() {}
|
fun foobar() {
|
||||||
|
println("$randomString")
|
||||||
|
}
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -92,11 +98,13 @@ class ExampleIncrementalScenarioTest : BaseCompilationTest() {
|
|||||||
val module1 = module("jvm-module-1")
|
val module1 = module("jvm-module-1")
|
||||||
val module2 = module("jvm-module-2", listOf(module1))
|
val module2 = module("jvm-module-2", listOf(module1))
|
||||||
|
|
||||||
|
val randomInt = Random.nextInt()
|
||||||
|
// Use this overload to modify file dynamically
|
||||||
module1.changeFile(
|
module1.changeFile(
|
||||||
"bar.kt",
|
"bar.kt",
|
||||||
transform = {
|
transform = {
|
||||||
//language=kt
|
//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) {
|
scenario(strategyConfig) {
|
||||||
val module1 = module("jvm-module-1")
|
val module1 = module("jvm-module-1")
|
||||||
|
|
||||||
|
// replaces bar.kt with bar.kt.1
|
||||||
module1.changeFile("bar.kt", 1U)
|
module1.changeFile("bar.kt", 1U)
|
||||||
|
|
||||||
module1.compile {
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user