Refactor Infrastructure of IC tests

1) Move root util functions to separate CompileRunnerUtils file from IncrementalJvmCompileRunner to leave the only class instance there and to separate util functions
2) Move TestLookupTracker from the abstract parent class location to a separate file as an implementation class
3) Small change naming of building functions just to clarify what they do
4) Some small code-style refactorings
This commit is contained in:
Aleksei.Cherepanov
2023-03-23 12:33:21 +01:00
committed by Space Team
parent 02abafd98d
commit ae8428e8d0
10 changed files with 147 additions and 118 deletions
@@ -319,7 +319,7 @@ abstract class CompileServiceImplBase(
CompilerMode.JPS_COMPILER -> {
@Suppress("UNCHECKED_CAST")
servicesFacade as JpsServicesFacadeT
withIC(k2PlatformArgs, enabled = servicesFacade.hasIncrementalCaches()) {
withIncrementalCompilation(k2PlatformArgs, enabled = servicesFacade.hasIncrementalCaches()) {
doCompile(sessionId, daemonReporter, tracer = null) { eventManger, profiler ->
val services = createServices(servicesFacade, eventManger, profiler)
compiler.exec(messageCollector, services, k2PlatformArgs)
@@ -346,7 +346,7 @@ abstract class CompileServiceImplBase(
val gradleIncrementalServicesFacade = servicesFacade
when (targetPlatform) {
CompileService.TargetPlatform.JVM -> withIC(k2PlatformArgs) {
CompileService.TargetPlatform.JVM -> withIncrementalCompilation(k2PlatformArgs) {
doCompile(sessionId, daemonReporter, tracer = null) { _, _ ->
execIncrementalCompiler(
k2PlatformArgs as K2JVMCompilerArguments,
@@ -616,7 +616,7 @@ abstract class CompileServiceImplBase(
val projectRoot = incrementalCompilationOptions.modulesInfo.projectRoot
val useK2 = k2jvmArgs.useK2 || LanguageVersion.fromVersionString(k2jvmArgs.languageVersion)?.usesK2 == true
// TODO: This should be reverted after implementing of fir-based java tracker (KT-57147).
// See org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunnerKt.makeIncrementally
// See org.jetbrains.kotlin.incremental.CompilerRunnerUtilsKt.makeJvmIncrementally
val usePreciseJavaTracking = if (useK2) false else incrementalCompilationOptions.usePreciseJavaTracking
val compiler = IncrementalJvmCompilerRunner(
@@ -0,0 +1,95 @@
/*
* 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.
*/
@file:JvmName("CompilerRunnerUtils")
package org.jetbrains.kotlin.incremental
import org.jetbrains.kotlin.build.DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS
import org.jetbrains.kotlin.build.report.BuildReporter
import org.jetbrains.kotlin.build.report.DoNothingICReporter
import org.jetbrains.kotlin.build.report.ICReporter
import org.jetbrains.kotlin.build.report.metrics.DoNothingBuildMetricsReporter
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.IncrementalCompilation
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.incremental.multiproject.EmptyModulesApiHistory
import java.io.File
var K2JVMCompilerArguments.destinationAsFile: File
get() = File(destination)
set(value) {
destination = value.absolutePath
}
var K2JVMCompilerArguments.classpathAsList: List<File>
get() = classpath.orEmpty().split(File.pathSeparator).map(::File)
set(value) {
classpath = value.joinToString(separator = File.pathSeparator, transform = { it.absolutePath })
}
@Suppress("unused") // used in Maven compile runner
fun makeJvmIncrementally(
cachesDir: File,
sourceRoots: Iterable<File>,
args: K2JVMCompilerArguments,
messageCollector: MessageCollector = MessageCollector.NONE,
reporter: ICReporter = DoNothingICReporter
) {
val kotlinExtensions = DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS
val allExtensions = kotlinExtensions + "java"
val rootsWalk = sourceRoots.asSequence().flatMap { it.walk() }
val files = rootsWalk.filter(File::isFile)
val sourceFiles = files.filter { it.extension.lowercase() in allExtensions }.toList()
val buildHistoryFile = File(cachesDir, "build-history.bin")
args.javaSourceRoots = sourceRoots.map { it.absolutePath }.toTypedArray()
val buildReporter = BuildReporter(icReporter = reporter, buildMetricsReporter = DoNothingBuildMetricsReporter)
withIncrementalCompilation(args) {
val useK2 = args.useK2 || LanguageVersion.fromVersionString(args.languageVersion)?.usesK2 == true
val compiler =
if (useK2 && args.useFirIC && args.useFirLT /* TODO by @Ilya.Chernikov: move LT check into runner */)
IncrementalFirJvmCompilerRunner(
cachesDir,
buildReporter,
buildHistoryFile,
outputDirs = null,
EmptyModulesApiHistory,
kotlinExtensions,
ClasspathChanges.ClasspathSnapshotDisabled
)
else
IncrementalJvmCompilerRunner(
cachesDir,
buildReporter,
// Use precise setting in case of non-Gradle build
usePreciseJavaTracking = !useK2, // TODO by @Ilya.Chernikov: add fir-based java classes tracker when available and set this to true
buildHistoryFile = buildHistoryFile,
outputDirs = null,
modulesApiHistory = EmptyModulesApiHistory,
kotlinSourceFilesExtensions = kotlinExtensions,
classpathChanges = ClasspathChanges.ClasspathSnapshotDisabled
)
//TODO by @Ilya.Chernikov set properly
compiler.compile(sourceFiles, args, messageCollector, changedFiles = null)
}
}
@Suppress("DEPRECATION")
inline fun <R> withIncrementalCompilation(args: CommonCompilerArguments, enabled: Boolean = true, fn: () -> R): R {
val isEnabledBackup = IncrementalCompilation.isEnabledForJvm()
IncrementalCompilation.setIsEnabledForJvm(enabled)
try {
if (args.incrementalCompilation == null) {
args.incrementalCompilation = enabled
}
return fn()
} finally {
IncrementalCompilation.setIsEnabledForJvm(isEnabledBackup)
}
}
@@ -22,7 +22,6 @@ import com.intellij.psi.PsiClass
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiJavaFile
import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.build.DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS
import org.jetbrains.kotlin.build.GeneratedFile
import org.jetbrains.kotlin.build.GeneratedJvmClass
@@ -30,7 +29,6 @@ import org.jetbrains.kotlin.build.report.*
import org.jetbrains.kotlin.build.report.metrics.*
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.FilteringMessageCollector
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -49,7 +47,6 @@ import org.jetbrains.kotlin.incremental.classpathDiff.*
import org.jetbrains.kotlin.incremental.classpathDiff.ClasspathChangesComputer.computeClasspathChanges
import org.jetbrains.kotlin.incremental.components.ExpectActualTracker
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.incremental.multiproject.EmptyModulesApiHistory
import org.jetbrains.kotlin.incremental.multiproject.ModulesApiHistory
import org.jetbrains.kotlin.incremental.util.BufferingMessageCollector
import org.jetbrains.kotlin.incremental.util.Either
@@ -62,68 +59,6 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import java.io.File
@TestOnly
fun makeIncrementally(
cachesDir: File,
sourceRoots: Iterable<File>,
args: K2JVMCompilerArguments,
messageCollector: MessageCollector = MessageCollector.NONE,
reporter: ICReporter = DoNothingICReporter
) {
val kotlinExtensions = DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS
val allExtensions = kotlinExtensions + "java"
val rootsWalk = sourceRoots.asSequence().flatMap { it.walk() }
val files = rootsWalk.filter(File::isFile)
val sourceFiles = files.filter { it.extension.lowercase() in allExtensions }.toList()
val buildHistoryFile = File(cachesDir, "build-history.bin")
args.javaSourceRoots = sourceRoots.map { it.absolutePath }.toTypedArray()
val buildReporter = BuildReporter(icReporter = reporter, buildMetricsReporter = DoNothingBuildMetricsReporter)
withIC(args) {
val useK2 = args.useK2 || LanguageVersion.fromVersionString(args.languageVersion)?.usesK2 == true
val compiler =
if (useK2 && args.useFirIC && args.useFirLT /* TODO: move LT check into runner */)
IncrementalFirJvmCompilerRunner(
cachesDir,
buildReporter,
buildHistoryFile,
outputDirs = null,
EmptyModulesApiHistory,
kotlinExtensions,
ClasspathSnapshotDisabled
)
else
IncrementalJvmCompilerRunner(
cachesDir,
buildReporter,
// Use precise setting in case of non-Gradle build
usePreciseJavaTracking = !useK2, // TODO: add fir-based java classes tracker when available and set this to true (KT-57147)
buildHistoryFile = buildHistoryFile,
outputDirs = null,
modulesApiHistory = EmptyModulesApiHistory,
kotlinSourceFilesExtensions = kotlinExtensions,
classpathChanges = ClasspathSnapshotDisabled
)
//TODO set properly
compiler.compile(sourceFiles, args, messageCollector, changedFiles = null)
}
}
@Suppress("DEPRECATION")
inline fun <R> withIC(args: CommonCompilerArguments, enabled: Boolean = true, fn: () -> R): R {
val isEnabledBackup = IncrementalCompilation.isEnabledForJvm()
IncrementalCompilation.setIsEnabledForJvm(enabled)
try {
if (args.incrementalCompilation == null) {
args.incrementalCompilation = enabled
}
return fn()
} finally {
IncrementalCompilation.setIsEnabledForJvm(isEnabledBackup)
}
}
open class IncrementalJvmCompilerRunner(
workingDir: File,
reporter: BuildReporter,
@@ -542,15 +477,3 @@ open class IncrementalJvmCompilerRunner(
}
}
}
var K2JVMCompilerArguments.destinationAsFile: File
get() = File(destination)
set(value) {
destination = value.path
}
var K2JVMCompilerArguments.classpathAsList: List<File>
get() = classpath.orEmpty().split(File.pathSeparator).map(::File)
set(value) {
classpath = value.joinToString(separator = File.pathSeparator, transform = { it.path })
}
@@ -36,9 +36,10 @@ abstract class AbstractIncrementalCompilerRunnerTestBase<Args : CommonCompilerAr
protected open fun resetTest(testDir: File, newOutDir: File, newCacheDir: File) {}
private fun createCompilerArgumentsImpl(destinationDir: File, testDir: File): Args = createCompilerArguments(destinationDir, testDir).apply {
parseCommandLineArguments(parseAdditionalArgs(testDir), this)
}
private fun createCompilerArgumentsImpl(destinationDir: File, testDir: File): Args =
createCompilerArguments(destinationDir, testDir).apply {
parseCommandLineArguments(parseAdditionalArgs(testDir), this)
}
fun doTest(path: String) {
val testDir = File(path)
@@ -127,7 +128,8 @@ abstract class AbstractIncrementalCompilerRunnerTestBase<Args : CommonCompilerAr
// these functions are needed only to simplify debugging of IC tests
private fun initialMake(cacheDir: File, outDir: File, sourceRoots: List<File>, args: Args) = make(cacheDir, outDir, sourceRoots, args)
private fun incrementalMake(cacheDir: File, outDir: File, sourceRoots: List<File>, args: Args) = make(cacheDir, outDir, sourceRoots, args)
private fun incrementalMake(cacheDir: File, outDir: File, sourceRoots: List<File>, args: Args) =
make(cacheDir, outDir, sourceRoots, args)
protected open fun rebuildAndCompareOutput(
sourceRoots: List<File>,
@@ -30,7 +30,7 @@ abstract class AbstractIncrementalJvmCompilerRunnerTest : AbstractIncrementalCom
override fun make(cacheDir: File, outDir: File, sourceRoots: Iterable<File>, args: K2JVMCompilerArguments): TestCompilationResult {
val reporter = TestICReporter()
val messageCollector = TestMessageCollector()
makeIncrementally(cacheDir, sourceRoots, args, reporter = reporter, messageCollector = messageCollector)
makeJvmIncrementally(cacheDir, sourceRoots, args, reporter = reporter, messageCollector = messageCollector)
val kotlinCompileResult = TestCompilationResult(reporter, messageCollector)
if (kotlinCompileResult.exitCode != ExitCode.OK) return kotlinCompileResult
@@ -55,9 +55,10 @@ abstract class AbstractIncrementalJvmCompilerRunnerTest : AbstractIncrementalCom
}
mkdirs()
}
val args = arrayOf("-cp", javaClasspath,
"-d", javaDestinationDir.canonicalPath,
*javaSources.map { it.canonicalPath }.toTypedArray()
val args = arrayOf(
"-cp", javaClasspath,
"-d", javaDestinationDir.canonicalPath,
*javaSources.map { it.canonicalPath }.toTypedArray()
)
val err = ByteArrayOutputStream()
@@ -30,8 +30,7 @@ class TestICReporter : ICReporterBase() {
var exitCode: ExitCode = ExitCode.OK
private set
override fun report(message: () -> String, severity: ReportSeverity) {
}
override fun report(message: () -> String, severity: ReportSeverity) {}
override fun reportCompileIteration(incremental: Boolean, sourceFiles: Collection<File>, exitCode: ExitCode) {
compiledSourcesMutable.addAll(sourceFiles)
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2022 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.incremental.utils
import com.intellij.util.containers.Interner
import org.jetbrains.kotlin.incremental.LookupSymbol
import org.jetbrains.kotlin.incremental.components.LookupInfo
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.incremental.components.Position
import org.jetbrains.kotlin.incremental.components.ScopeKind
class TestLookupTracker(val savedLookups: MutableSet<LookupSymbol> = mutableSetOf()) : LookupTracker {
val lookups = arrayListOf<LookupInfo>()
private val interner = Interner.createStringInterner()
override val requiresPosition: Boolean
get() = true
override fun record(filePath: String, position: Position, scopeFqName: String, scopeKind: ScopeKind, name: String) {
val internedFilePath = interner.intern(filePath)
val internedScopeFqName = interner.intern(scopeFqName)
val internedName = interner.intern(name)
lookups.add(LookupInfo(internedFilePath, position, internedScopeFqName, scopeKind, internedName))
}
override fun clear() {
lookups.clear()
}
}
@@ -38,6 +38,7 @@ import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
import org.jetbrains.kotlin.incremental.LookupSymbol
import org.jetbrains.kotlin.incremental.testingUtils.*
import org.jetbrains.kotlin.incremental.utils.TestLookupTracker
import org.jetbrains.kotlin.jps.build.dependeciestxt.ModulesTxt
import org.jetbrains.kotlin.jps.build.dependeciestxt.ModulesTxtBuilder
import org.jetbrains.kotlin.jps.build.fixtures.EnableICFixture
@@ -8,10 +8,8 @@ package org.jetbrains.kotlin.jps.build
import com.intellij.testFramework.RunAll
import com.intellij.testFramework.UsefulTestCase
import com.intellij.util.ThrowableRunnable
import com.intellij.util.containers.Interner
import org.jetbrains.kotlin.TestWithWorkingDir
import org.jetbrains.kotlin.build.JvmSourceRoot
import org.jetbrains.kotlin.cli.common.CompilerSystemProperties
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
@@ -20,14 +18,13 @@ import org.jetbrains.kotlin.compilerRunner.*
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.incremental.components.LookupInfo
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.incremental.components.Position
import org.jetbrains.kotlin.incremental.components.ScopeKind
import org.jetbrains.kotlin.incremental.isKotlinFile
import org.jetbrains.kotlin.incremental.js.*
import org.jetbrains.kotlin.incremental.makeModuleFile
import org.jetbrains.kotlin.incremental.testingUtils.TouchPolicy
import org.jetbrains.kotlin.incremental.testingUtils.copyTestSources
import org.jetbrains.kotlin.incremental.testingUtils.getModificationsToPerform
import org.jetbrains.kotlin.incremental.utils.TestLookupTracker
import org.jetbrains.kotlin.incremental.utils.TestMessageCollector
import org.jetbrains.kotlin.jps.build.fixtures.EnableICFixture
import org.jetbrains.kotlin.jps.incremental.createTestingCompilerEnvironment
@@ -349,25 +346,3 @@ abstract class AbstractLookupTrackerTest : TestWithWorkingDir() {
KotlinTestUtils.assertEqualsToFile("Lookups do not match after $step", expectedFile, actual)
}
}
class TestLookupTracker : LookupTracker {
val lookups = arrayListOf<LookupInfo>()
private val interner = Interner.createStringInterner()
override val requiresPosition: Boolean
get() = true
override fun record(filePath: String, position: Position, scopeFqName: String, scopeKind: ScopeKind, name: String) {
val internedFilePath = interner.intern(filePath)
val internedScopeFqName = interner.intern(scopeFqName)
val internedName = interner.intern(name)
lookups.add(LookupInfo(internedFilePath, position, internedScopeFqName, scopeKind, internedName))
}
override fun clear() {
lookups.clear()
}
}
@@ -34,7 +34,7 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
import org.jetbrains.kotlin.config.JvmTarget;
import org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunnerKt;
import org.jetbrains.kotlin.incremental.CompilerRunnerUtils;
import org.jetbrains.kotlin.maven.incremental.FileCopier;
import org.jetbrains.kotlin.maven.incremental.MavenICReporter;
import org.jetbrains.kotlin.maven.kapt.AnnotationProcessingManager;
@@ -270,7 +270,7 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
arguments.setClasspath(StringUtil.join(filteredClasspath, File.pathSeparator));
}
IncrementalJvmCompilerRunnerKt.makeIncrementally(cachesDir, sourceRoots, arguments, messageCollector, icReporter);
CompilerRunnerUtils.makeJvmIncrementally(cachesDir, sourceRoots, arguments, messageCollector, icReporter);
int compiledKtFilesCount = icReporter.getCompiledKotlinFiles().size();
getLog().info("Compiled " + icReporter.getCompiledKotlinFiles().size() + " Kotlin files using incremental compiler");