diff --git a/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/ScenarioModule.kt b/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/ScenarioModule.kt index 10bc28d1581..01b4ca85f43 100644 --- a/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/ScenarioModule.kt +++ b/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/ScenarioModule.kt @@ -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 = {}, diff --git a/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/scenarioDsl.kt b/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/scenarioDsl.kt index 52408a2f491..9504765e67b 100644 --- a/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/scenarioDsl.kt +++ b/compiler/build-tools/kotlin-build-tools-api-tests/src/main/kotlin/compilation/scenario/scenarioDsl.kt @@ -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( diff --git a/compiler/build-tools/kotlin-build-tools-api-tests/src/main/resources/modules/jvm-module-1/secret.kt.1 b/compiler/build-tools/kotlin-build-tools-api-tests/src/main/resources/modules/jvm-module-1/secret.kt.1 new file mode 100644 index 00000000000..a43c142da7d --- /dev/null +++ b/compiler/build-tools/kotlin-build-tools-api-tests/src/main/resources/modules/jvm-module-1/secret.kt.1 @@ -0,0 +1,3 @@ +fun heyheyhey() { + println("I won't give you any secrets :P") +} \ No newline at end of file diff --git a/compiler/build-tools/kotlin-build-tools-api-tests/src/testExample/kotlin/ExampleIncrementalScenarioTest.kt b/compiler/build-tools/kotlin-build-tools-api-tests/src/testExample/kotlin/ExampleIncrementalScenarioTest.kt index f032ef6f1b4..aa4ddbb3080 100644 --- a/compiler/build-tools/kotlin-build-tools-api-tests/src/testExample/kotlin/ExampleIncrementalScenarioTest.kt +++ b/compiler/build-tools/kotlin-build-tools-api-tests/src/testExample/kotlin/ExampleIncrementalScenarioTest.kt @@ -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") + } + } + } } \ No newline at end of file