From 27ac8066f6fb868cd74afaa275600ccb5e8df5a7 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Fri, 7 Aug 2015 15:01:30 +0300 Subject: [PATCH] Add test infrastructure to check the tracking lookups Added simple test to verify infrastructure. Original commit: 7e250ab2ca051b45036e74072b2f4d28ca881cd0 --- .../jps/build/AbstractIncrementalJpsTest.kt | 71 +++++++++++++------ .../jps/build/AbstractLookupTrackerTest.kt | 23 ++++++ .../jps/build/LookupTrackerTestGenerated.java | 44 ++++++++++++ .../lookupTracker/simple/lookups.txt | 5 ++ .../incremental/lookupTracker/simple/main.kt | 6 ++ 5 files changed, 128 insertions(+), 21 deletions(-) create mode 100644 jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractLookupTrackerTest.kt create mode 100644 jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/LookupTrackerTestGenerated.java create mode 100644 jps/jps-plugin/testData/incremental/lookupTracker/simple/lookups.txt create mode 100644 jps/jps-plugin/testData/incremental/lookupTracker/simple/main.kt diff --git a/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt b/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt index 0e35256e173..54d0d999e27 100644 --- a/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt +++ b/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt @@ -38,9 +38,12 @@ import org.jetbrains.jps.cmdline.ProjectDescriptor import org.jetbrains.jps.incremental.BuilderRegistry import org.jetbrains.jps.incremental.IncProjectBuilder import org.jetbrains.jps.incremental.messages.BuildMessage +import org.jetbrains.jps.model.JpsElementFactory import org.jetbrains.jps.model.JpsModuleRootModificationUtil import org.jetbrains.jps.model.java.JpsJavaExtensionService import org.jetbrains.jps.util.JpsPathUtil +import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.incremental.components.ScopeKind import org.jetbrains.kotlin.jps.build.classFilesComparison.assertEqualDirectories import org.jetbrains.kotlin.jps.incremental.getKotlinCache import org.jetbrains.kotlin.test.JetTestUtils @@ -58,7 +61,9 @@ import kotlin.test.fail public abstract class AbstractIncrementalJpsTest( private val allowNoFilesWithSuffixInTestData: Boolean = false, - private val checkDumpsCaseInsensitively: Boolean = false + private val checkDumpsCaseInsensitively: Boolean = false, + private val allowNoBuildLogFileInTestData: Boolean = false, + private val allowNoLookupsFileInTestData: Boolean = true ) : JpsBuildTestCase() { companion object { val COMPILATION_FAILED = "COMPILATION FAILED" @@ -106,6 +111,11 @@ public abstract class AbstractIncrementalJpsTest( val workDirPath = FileUtil.toSystemIndependentName(workDir.getAbsolutePath()) val logger = MyLogger(workDirPath) val descriptor = createProjectDescriptor(BuildLoggingManager(logger)) + + val lookupTracker = TestLookupTracker(workDirPath) + val dataContainer = JpsElementFactory.getInstance().createSimpleElement(lookupTracker) + descriptor.project.container.setChild(KotlinBuilder.LOOKUP_TRACKER, dataContainer) + try { val builder = IncProjectBuilder(descriptor, BuilderRegistry.getInstance(), myBuildParams, CanceledStatus.NULL, mockConstantSearch, true) val buildResult = BuildResult() @@ -119,19 +129,20 @@ public abstract class AbstractIncrementalJpsTest( .map { it.getMessageText() } .map { it.replace("^.+:\\d+:\\s+".toRegex(), "").trim() } .joinToString("\n") - return MakeResult(logger.log + "$COMPILATION_FAILED\n" + errorMessages + "\n", true, null) + return MakeResult(logger.log + "$COMPILATION_FAILED\n" + errorMessages + "\n", true, null, lookupTracker.lookups) } else { - return MakeResult(logger.log, false, createMappingsDump(descriptor)) + return MakeResult(logger.log, false, createMappingsDump(descriptor), lookupTracker.lookups) } } finally { descriptor.release() } } - private fun initialMake() { + private fun initialMake(): MakeResult { val makeResult = build() assertFalse(makeResult.makeFailed, "Initial make failed:\n$makeResult") + return makeResult } private fun make(): MakeResult { @@ -252,13 +263,26 @@ public abstract class AbstractIncrementalJpsTest( workDir = FileUtilRt.createTempDirectory(TEMP_DIRECTORY_TO_USE, "jps-build", null) val moduleNames = configureModules() - initialMake() + val initialMakeResult = initialMake() - val makeOverallResult = performModificationsAndMake(moduleNames) - UsefulTestCase.assertSameLinesWithFile(File(testDataDir, "build.log").getAbsolutePath(), makeOverallResult.log) + val otherMakeResults = performModificationsAndMake(moduleNames) - rebuildAndCheckOutput(makeOverallResult) - clearCachesRebuildAndCheckOutput(makeOverallResult) + val buildLogFile = File(testDataDir, "build.log") + if (buildLogFile.exists() || !allowNoBuildLogFileInTestData) { + val logs = otherMakeResults.joinToString("\n\n") { it.log } + UsefulTestCase.assertSameLinesWithFile(buildLogFile.absolutePath, logs) + } + + val lookupsFile = File(testDataDir, "lookups.txt") + if (lookupsFile.exists() || !allowNoLookupsFileInTestData) { + val allActualLookups = otherMakeResults.mapTo(arrayListOf(initialMakeResult.lookups), MakeResult::lookups.getter) + val actualLookups = allActualLookups.joinToString("\n\n") { it.toSortedList().join("\n") } + "\n" + UsefulTestCase.assertSameLinesWithFile(lookupsFile.absolutePath, actualLookups) + } + + val lastMakeResult = otherMakeResults.last() + rebuildAndCheckOutput(lastMakeResult) + clearCachesRebuildAndCheckOutput(lastMakeResult) } private fun createMappingsDump(project: ProjectDescriptor) = @@ -312,25 +336,18 @@ public abstract class AbstractIncrementalJpsTest( return byteArrayOutputStream.toString() } - private data class MakeResult(val log: String, val makeFailed: Boolean, val mappingsDump: String?) + private data class MakeResult(val log: String, val makeFailed: Boolean, val mappingsDump: String?, val lookups: List) - private fun performModificationsAndMake(moduleNames: Set?): MakeResult { - val logs = ArrayList() + private fun performModificationsAndMake(moduleNames: Set?): List { + val results = arrayListOf() val modifications = getModificationsToPerform(moduleNames) - var lastCompilationFailed = false - var lastMappingsDump: String? = null for (step in modifications) { step.forEach { it.perform(workDir) } performAdditionalModifications() - - val makeResult = make() - logs.add(makeResult.log) - lastCompilationFailed = makeResult.makeFailed - lastMappingsDump = makeResult.mappingsDump + results.add(make()) } - - return MakeResult(logs.join("\n\n"), lastCompilationFailed, lastMappingsDump) + return results } protected open fun performAdditionalModifications() { @@ -377,6 +394,18 @@ public abstract class AbstractIncrementalJpsTest( override fun doGetProjectDir(): File? = workDir + private class TestLookupTracker(val rootPath: String) : LookupTracker { + val lookups = arrayListOf() + + override val verbose: Boolean + get() = true + + override fun record(lookupLocation: String, scopeFqName: String, scopeContainingFile: String?, scopeKind: ScopeKind, name: String) { + val s = """from "$lookupLocation" by "$name" in $scopeKind scope "$scopeFqName" in file ${scopeContainingFile?.let { "$it" } ?: null}""" + lookups.add(s.replace(rootPath, "\$TEST_DIR$")) + } + } + private class MyLogger(val rootPath: String) : ProjectBuilderLoggerBase() { private val logBuf = StringBuilder() public val log: String diff --git a/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractLookupTrackerTest.kt b/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractLookupTrackerTest.kt new file mode 100644 index 00000000000..623a800dede --- /dev/null +++ b/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/AbstractLookupTrackerTest.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.jps.build + +abstract class AbstractLookupTrackerTest : AbstractIncrementalJpsTest( + allowNoFilesWithSuffixInTestData = true, + allowNoBuildLogFileInTestData = true, + allowNoLookupsFileInTestData = false +) diff --git a/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/LookupTrackerTestGenerated.java b/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/LookupTrackerTestGenerated.java new file mode 100644 index 00000000000..72dc1d1a9c1 --- /dev/null +++ b/jps/jps-plugin/test/org/jetbrains/kotlin/jps/build/LookupTrackerTestGenerated.java @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.jps.build; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.JetTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("jps-plugin/testData/incremental/lookupTracker") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class LookupTrackerTestGenerated extends AbstractLookupTrackerTest { + public void testAllFilesPresentInLookupTracker() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("jps-plugin/testData/incremental/lookupTracker"), Pattern.compile("^([^\\.]+)$"), true); + } + + @TestMetadata("simple") + public void testSimple() throws Exception { + String fileName = JetTestUtils.navigationMetadata("jps-plugin/testData/incremental/lookupTracker/simple/"); + doTest(fileName); + } + +} diff --git a/jps/jps-plugin/testData/incremental/lookupTracker/simple/lookups.txt b/jps/jps-plugin/testData/incremental/lookupTracker/simple/lookups.txt new file mode 100644 index 00000000000..5502708b8ab --- /dev/null +++ b/jps/jps-plugin/testData/incremental/lookupTracker/simple/lookups.txt @@ -0,0 +1,5 @@ +from "$TEST_DIR$/src/main.kt:3:16" by "Array" in PACKAGE scope "foo.bar" in file null +from "$TEST_DIR$/src/main.kt:3:22" by "String" in PACKAGE scope "foo.bar" in file null +from "$TEST_DIR$/src/main.kt:4:12" by "Array" in PACKAGE scope "foo.bar" in file null +from "$TEST_DIR$/src/main.kt:4:18" by "Int" in PACKAGE scope "foo.bar" in file null +from "$TEST_DIR$/src/main.kt:5:12" by "String" in PACKAGE scope "foo.bar" in file null diff --git a/jps/jps-plugin/testData/incremental/lookupTracker/simple/main.kt b/jps/jps-plugin/testData/incremental/lookupTracker/simple/main.kt new file mode 100644 index 00000000000..380d4f3998f --- /dev/null +++ b/jps/jps-plugin/testData/incremental/lookupTracker/simple/main.kt @@ -0,0 +1,6 @@ +package foo.bar + +fun main(args: Array) { + val f: Array + val s: String +}