From 8196621be5b002f5d2ca88afa1758e968fdf3f3c Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Mon, 14 Dec 2015 17:58:20 +0300 Subject: [PATCH] Add test for inspections suppression --- .../kotlin/test/KotlinTestUtils.java | 16 +++------- .../idea/caches/resolve/KotlinCacheService.kt | 9 +++++- .../suppress/inspections/ifNullToElvis.kt | 9 ++++++ .../inspections/ifNullToElvis.kt.after | 10 ++++++ .../suppress/inspections/unusedImports.kt | 5 +++ .../inspections/unusedImports.kt.after | 7 ++++ .../resolve/AbstractIdeLightClassTest.kt | 2 +- .../idea/quickfix/AbstractQuickFixTest.java | 32 +++++++++++++++++++ .../idea/quickfix/QuickFixTestGenerated.java | 21 ++++++++++++ 9 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt create mode 100644 idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt.after create mode 100644 idea/testData/quickfix/suppress/inspections/unusedImports.kt create mode 100644 idea/testData/quickfix/suppress/inspections/unusedImports.kt.after diff --git a/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java index d684b348ca3..ee7203864d9 100644 --- a/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -795,13 +795,10 @@ public class KotlinTestUtils { return testFile; } - public static String getTestsRoot(@NotNull TestCase testCase) { - try { - return (String) testCase.getClass().getMethod("getTestsRoot").invoke(testCase); - } - catch (Exception e) { - throw new RuntimeException("Can't call getTestsRoot() on test class", e); - } + public static String getTestsRoot(@NotNull Class testCaseClass) { + TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class); + Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata); + return testClassMetadata.value(); } public static void assertAllTestsPresentByMetadata( @@ -811,10 +808,7 @@ public class KotlinTestUtils { boolean recursive, @NotNull String... excludeDirs ) { - TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class); - Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata); - String rootPath = testClassMetadata.value(); - File rootFile = new File(rootPath); + File rootFile = new File(getTestsRoot(testCaseClass)); Set filePaths = collectPathsMetadata(testCaseClass); Set exclude = SetsKt.setOf(excludeDirs); diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/KotlinCacheService.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/KotlinCacheService.kt index 83e62a7d73d..b95221e1aee 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/KotlinCacheService.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/KotlinCacheService.kt @@ -158,7 +158,14 @@ public class KotlinCacheService(val project: Project) { private val kotlinSuppressCache: CachedValue = CachedValuesManager.getManager(project).createCachedValue({ CachedValueProvider.Result(object : KotlinSuppressCache() { override fun getSuppressionAnnotations(annotated: KtAnnotated): List { - val context = annotated.analyze(BodyResolveMode.PARTIAL) + val context = + if (annotated is KtFile) { + annotated.fileAnnotationList?.analyze(BodyResolveMode.PARTIAL) ?: return emptyList() + } + else { + annotated.analyze(BodyResolveMode.PARTIAL) + } + val annotatedDescriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, annotated) if (annotatedDescriptor != null) { diff --git a/idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt b/idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt new file mode 100644 index 00000000000..695bf9e0800 --- /dev/null +++ b/idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt @@ -0,0 +1,9 @@ +// "Suppress 'IfNullToElvis' for fun foo" "true" + +fun foo(p: List, b: Boolean) { + var v = p[0] + if (v == null) return + if (b) v = null +} + +// TOOL: org.jetbrains.kotlin.idea.intentions.IfNullToElvisInspection \ No newline at end of file diff --git a/idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt.after b/idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt.after new file mode 100644 index 00000000000..c3589d6d13c --- /dev/null +++ b/idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt.after @@ -0,0 +1,10 @@ +// "Suppress 'IfNullToElvis' for fun foo" "true" + +@Suppress("IfNullToElvis") +fun foo(p: List, b: Boolean) { + var v = p[0] + if (v == null) return + if (b) v = null +} + +// TOOL: org.jetbrains.kotlin.idea.intentions.IfNullToElvisInspection \ No newline at end of file diff --git a/idea/testData/quickfix/suppress/inspections/unusedImports.kt b/idea/testData/quickfix/suppress/inspections/unusedImports.kt new file mode 100644 index 00000000000..da63f8bc0af --- /dev/null +++ b/idea/testData/quickfix/suppress/inspections/unusedImports.kt @@ -0,0 +1,5 @@ +// "Suppress 'KotlinUnusedImport' for file ${file}" "true" + +import java.io.* + +// TOOL: org.jetbrains.kotlin.idea.inspections.KotlinUnusedImportInspection \ No newline at end of file diff --git a/idea/testData/quickfix/suppress/inspections/unusedImports.kt.after b/idea/testData/quickfix/suppress/inspections/unusedImports.kt.after new file mode 100644 index 00000000000..4186178450d --- /dev/null +++ b/idea/testData/quickfix/suppress/inspections/unusedImports.kt.after @@ -0,0 +1,7 @@ +// "Suppress 'KotlinUnusedImport' for file ${file}" "true" + +@file:Suppress("KotlinUnusedImport") + +import java.io.* + +// TOOL: org.jetbrains.kotlin.idea.inspections.KotlinUnusedImportInspection \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/caches/resolve/AbstractIdeLightClassTest.kt b/idea/tests/org/jetbrains/kotlin/idea/caches/resolve/AbstractIdeLightClassTest.kt index 6529a4ac337..db32e3ef85b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/caches/resolve/AbstractIdeLightClassTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/caches/resolve/AbstractIdeLightClassTest.kt @@ -47,7 +47,7 @@ public abstract class AbstractIdeCompiledLightClassTest : KotlinDaemonAnalyzerTe val testName = getTestName(false) if (KotlinTestUtils.isAllFilesPresentTest(testName)) return - val filePath = "${KotlinTestUtils.getTestsRoot(this)}/${getTestName(false)}.kt" + val filePath = "${KotlinTestUtils.getTestsRoot(this.javaClass)}/${getTestName(false)}.kt" Assert.assertTrue("File doesn't exist $filePath", File(filePath).exists()) diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java index cbb53e5aad5..b7493a92055 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.quickfix; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInsight.daemon.quickFix.QuickFixTestCase; import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.SuppressIntentionAction; import com.intellij.codeInspection.SuppressableProblemGroup; import com.intellij.ide.startup.impl.StartupManagerImpl; @@ -34,25 +35,31 @@ import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.psi.PsiElement; import com.intellij.psi.stubs.StubUpdatingIndex; import com.intellij.rt.execution.junit.FileComparisonFailure; +import com.intellij.util.ArrayUtil; import com.intellij.util.indexing.FileBasedIndex; import kotlin.CollectionsKt; import kotlin.StringsKt; +import kotlin.io.FilesKt; import kotlin.jvm.functions.Function1; import org.apache.commons.lang.SystemUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.idea.KotlinLightQuickFixTestCase; +import org.jetbrains.kotlin.idea.inspections.KotlinUnusedImportInspection; +import org.jetbrains.kotlin.idea.intentions.IfNullToElvisInspection; import org.jetbrains.kotlin.idea.quickfix.utils.QuickfixTestUtilsKt; import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil; import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils; import org.jetbrains.kotlin.idea.test.PluginTestCaseBase; import org.jetbrains.kotlin.psi.KtFile; import org.jetbrains.kotlin.test.InTextDirectivesUtils; +import org.jetbrains.kotlin.test.KotlinTestUtils; import org.jetbrains.kotlin.test.TestMetadata; import org.junit.Assert; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -88,6 +95,31 @@ public abstract class AbstractQuickFixTest extends KotlinLightQuickFixTestCase { private static QuickFixTestCase myWrapper; + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + if (KotlinTestUtils.isAllFilesPresentTest(getTestName(false))) return super.configureLocalInspectionTools(); + + String testRoot = KotlinTestUtils.getTestsRoot(this.getClass()); + String configFileText = FilesKt.readText(new File(testRoot, getTestName(true) + ".kt"), Charset.defaultCharset()); + List toolsStrings = InTextDirectivesUtils.findListWithPrefixes(configFileText, "TOOL:"); + + if (toolsStrings.isEmpty()) return super.configureLocalInspectionTools(); + + return ArrayUtil.toObjectArray(CollectionsKt.map(toolsStrings, new Function1() { + @Override + public LocalInspectionTool invoke(String toolFqName) { + try { + Class aClass = Class.forName(toolFqName); + return (LocalInspectionTool) aClass.newInstance(); + } + catch (Exception e) { + throw new IllegalArgumentException("Failed to create inspection for key '" + toolFqName + "'", e); + } + } + }), LocalInspectionTool.class); + } + private void doKotlinQuickFixTest(final String testName, final QuickFixTestCase quickFixTestCase) { String relativePath = notNull(quickFixTestCase.getBasePath(), "") + "/" + StringsKt.decapitalize(testName); final String testFullPath = quickFixTestCase.getTestDataPath().replace(File.separatorChar, '/') + relativePath; diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index a4ca883dcf0..54f5d18d1f8 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -6614,6 +6614,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } } + + @TestMetadata("idea/testData/quickfix/suppress/inspections") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Inspections extends AbstractQuickFixTest { + public void testAllFilesPresentInInspections() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/suppress/inspections"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("ifNullToElvis.kt") + public void testIfNullToElvis() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/suppress/inspections/ifNullToElvis.kt"); + doTest(fileName); + } + + @TestMetadata("unusedImports.kt") + public void testUnusedImports() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/suppress/inspections/unusedImports.kt"); + doTest(fileName); + } + } } @TestMetadata("idea/testData/quickfix/typeAddition")