[FIR] add tests on external annotations

Tests are excluded in COMPILED_JAVA mode because in this
mode external annotations are not present at all

^KT-62310
This commit is contained in:
Dmitrii Gridin
2023-10-05 11:46:50 +02:00
committed by Space Team
parent e452113a7a
commit 2cac922cd0
45 changed files with 1158 additions and 17 deletions
@@ -23,7 +23,7 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign
public class Tests {
@Test
public void testAllFilesPresentInTests() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/foreignAnnotationsTests/tests"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/foreignAnnotationsTests/tests"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "externalAnnotations");
}
@Test
@@ -146,6 +146,64 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Warnings.kt");
}
@Nested
@TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations")
@TestDataPath("$PROJECT_ROOT")
public class ExternalAnnotations {
@Test
public void testAllFilesPresentInExternalAnnotations() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
}
@Test
@TestMetadata("constructorParameterNotNull.kt")
public void testConstructorParameterNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/constructorParameterNotNull.kt");
}
@Test
@TestMetadata("constructorParameterNotNull2.kt")
public void testConstructorParameterNotNull2() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/constructorParameterNotNull2.kt");
}
@Test
@TestMetadata("fieldNotNull.kt")
public void testFieldNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/fieldNotNull.kt");
}
@Test
@TestMetadata("fieldNotNullAnnotationWithPackage.kt")
public void testFieldNotNullAnnotationWithPackage() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/fieldNotNullAnnotationWithPackage.kt");
}
@Test
@TestMetadata("methodNotNull.kt")
public void testMethodNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodNotNull.kt");
}
@Test
@TestMetadata("methodNotNullFromAnotherModule.kt")
public void testMethodNotNullFromAnotherModule() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodNotNullFromAnotherModule.kt");
}
@Test
@TestMetadata("methodParameterNotNull.kt")
public void testMethodParameterNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodParameterNotNull.kt");
}
@Test
@TestMetadata("methodParameterNotNull2.kt")
public void testMethodParameterNotNull2() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodParameterNotNull2.kt");
}
}
@Nested
@TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305")
@TestDataPath("$PROJECT_ROOT")
@@ -146,6 +146,64 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/rxjava3Warnings.kt");
}
@Nested
@TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations")
@TestDataPath("$PROJECT_ROOT")
public class ExternalAnnotations {
@Test
public void testAllFilesPresentInExternalAnnotations() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
}
@Test
@TestMetadata("constructorParameterNotNull.kt")
public void testConstructorParameterNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/constructorParameterNotNull.kt");
}
@Test
@TestMetadata("constructorParameterNotNull2.kt")
public void testConstructorParameterNotNull2() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/constructorParameterNotNull2.kt");
}
@Test
@TestMetadata("fieldNotNull.kt")
public void testFieldNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/fieldNotNull.kt");
}
@Test
@TestMetadata("fieldNotNullAnnotationWithPackage.kt")
public void testFieldNotNullAnnotationWithPackage() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/fieldNotNullAnnotationWithPackage.kt");
}
@Test
@TestMetadata("methodNotNull.kt")
public void testMethodNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodNotNull.kt");
}
@Test
@TestMetadata("methodNotNullFromAnotherModule.kt")
public void testMethodNotNullFromAnotherModule() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodNotNullFromAnotherModule.kt");
}
@Test
@TestMetadata("methodParameterNotNull.kt")
public void testMethodParameterNotNull() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodParameterNotNull.kt");
}
@Test
@TestMetadata("methodParameterNotNull2.kt")
public void testMethodParameterNotNull2() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/externalAnnotations/methodParameterNotNull2.kt");
}
}
@Nested
@TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305")
@TestDataPath("$PROJECT_ROOT")
@@ -0,0 +1,26 @@
/*
* 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.
*/
package org.jetbrains.kotlin.test.preprocessors
import org.jetbrains.kotlin.test.model.TestFile
import org.jetbrains.kotlin.test.services.ReversibleSourceFilePreprocessor
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.isExternalAnnotation
/**
* This preprocessor is required to drop '//' comments from the file to avoid exceptions from XML parser
*/
class ExternalAnnotationsSourcePreprocessor(testServices: TestServices) : ReversibleSourceFilePreprocessor(testServices) {
override fun process(file: TestFile, content: String): String = if (file.isExternalAnnotation) {
content.trim().lineSequence().filterNot { it.startsWith('/') }.joinToString(separator = "\n")
} else {
content
}
override fun revert(file: TestFile, actualContent: String): String {
return if (file.isExternalAnnotation) file.originalContent.trim() + "\n" else actualContent
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/
@@ -24,7 +24,9 @@ import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirective
import org.jetbrains.kotlin.test.frontend.classic.handlers.*
import org.jetbrains.kotlin.test.model.DependencyKind
import org.jetbrains.kotlin.test.model.FrontendKinds
import org.jetbrains.kotlin.test.preprocessors.ExternalAnnotationsSourcePreprocessor
import org.jetbrains.kotlin.test.preprocessors.JspecifyMarksCleanupPreprocessor
import org.jetbrains.kotlin.test.services.SourceFilePreprocessor
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.configuration.*
import org.jetbrains.kotlin.test.services.jvm.ForeignAnnotationAgainstCompiledJavaTestSuppressor
@@ -69,9 +71,12 @@ abstract class AbstractForeignAnnotationsTestBase(private val kind: ForeignAnnot
useConfigurators(
::CommonEnvironmentConfigurator,
::JvmForeignAnnotationsConfigurator,
::JvmEnvironmentConfigurator
::JvmEnvironmentConfigurator,
::ExternalAnnotationsEnvironmentConfigurator,
)
useSourcePreprocessor(::ExternalAnnotationsSourcePreprocessor)
useAdditionalSourceProviders(
::AdditionalDiagnosticsSourceFilesProvider,
::CoroutineHelpersSourceFilesProvider,
@@ -0,0 +1,86 @@
/*
* 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.
*/
package org.jetbrains.kotlin.test.services.configuration
import com.intellij.codeInsight.BaseExternalAnnotationsManager
import com.intellij.codeInsight.ExternalAnnotationsManager
import com.intellij.mock.MockProject
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.StandardFileSystems
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiModifierListOwner
import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.EnvironmentConfigurator
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.sourceFileProvider
/**
* Adds the ability to declare 'annotations.xml' file with description of external annotations.
*
* Such synthetic Java annotations can be found by
* [JavaAnnotation.isIdeExternalAnnotation][org.jetbrains.kotlin.load.java.structure.JavaAnnotation.isIdeExternalAnnotation] flag.
*
* @see ExternalAnnotationsManager
* @see org.jetbrains.kotlin.test.preprocessors.ExternalAnnotationsSourcePreprocessor
*/
class ExternalAnnotationsEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfigurator(testServices) {
override fun legacyRegisterCompilerExtensions(project: Project, module: TestModule, configuration: CompilerConfiguration) {
var hasAnnotationFile = false
for (file in module.files) {
if (file.name != ExternalAnnotationsManager.ANNOTATIONS_XML) continue
// This call is required to copy files into the project to be able to read those xml files from the manager
testServices.sourceFileProvider.getRealFileForSourceFile(file)
hasAnnotationFile = true
}
if (!hasAnnotationFile) return
(project as MockProject).picoContainer.unregisterComponent(ExternalAnnotationsManager::class.java.name)
project.registerService(
ExternalAnnotationsManager::class.java,
ExternalAnnotationsManagerForTests(
// We use additionalFilesDirectory as a root of external annotations because this directory contains all
// declared 'annotations.xml' files
externalAnnotationsRootPath = testServices.sourceFileProvider.additionalFilesDirectory.path,
manager = PsiManager.getInstance(project),
),
)
}
}
/**
* Simple [ExternalAnnotationsManager] implementation
* which always suggests searching external annotations inside [externalAnnotationsRootPath].
*
* @param externalAnnotationsRootPath path to the directory with 'annotations.xml' files
*/
private class ExternalAnnotationsManagerForTests(
externalAnnotationsRootPath: String,
manager: PsiManager,
) : BaseExternalAnnotationsManager(manager) {
private val externalAnnotationsRoots: List<VirtualFile> by lazyPub {
VirtualFileManager.getInstance()
.getFileSystem(StandardFileSystems.FILE_PROTOCOL)
.findFileByPath(externalAnnotationsRootPath)
?.let(::listOf)
?: error("File with external annotations is not found")
}
/**
* We simply returns [externalAnnotationsRoots] because there is all our declared 'annotations.xml' files
*
* @param libraryFile is a file for which we want to find the corresponding external annotations file if it exists
*/
override fun getExternalAnnotationsRoots(libraryFile: VirtualFile): List<VirtualFile> = externalAnnotationsRoots
override fun hasAnyAnnotationsRoots(): Boolean = true
override fun hasConfiguredAnnotationRoot(owner: PsiModifierListOwner): Boolean = true
}
@@ -46,7 +46,7 @@ class ModuleStructureExtractorImpl(
private val environmentConfigurators: List<AbstractEnvironmentConfigurator>
) : ModuleStructureExtractor(testServices, additionalSourceProviders, moduleStructureTransformers) {
companion object {
private val allowedExtensionsForFiles = listOf(".kt", ".kts", ".java", ".js", ".mjs", ".config")
private val allowedExtensionsForFiles = listOf(".kt", ".kts", ".java", ".js", ".mjs", ".config", ".xml")
/*
* ([^()\n]+) module name