Add test for inspections suppression
This commit is contained in:
committed by
Nikolay Krasko
parent
aff83087a3
commit
8196621be5
@@ -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<String> filePaths = collectPathsMetadata(testCaseClass);
|
||||
Set<String> exclude = SetsKt.setOf(excludeDirs);
|
||||
|
||||
+8
-1
@@ -158,7 +158,14 @@ public class KotlinCacheService(val project: Project) {
|
||||
private val kotlinSuppressCache: CachedValue<KotlinSuppressCache> = CachedValuesManager.getManager(project).createCachedValue({
|
||||
CachedValueProvider.Result<KotlinSuppressCache>(object : KotlinSuppressCache() {
|
||||
override fun getSuppressionAnnotations(annotated: KtAnnotated): List<AnnotationDescriptor> {
|
||||
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) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Suppress 'IfNullToElvis' for fun foo" "true"
|
||||
|
||||
fun foo(p: List<String?>, b: Boolean) {
|
||||
var v = p[0]
|
||||
<caret>if (v == null) return
|
||||
if (b) v = null
|
||||
}
|
||||
|
||||
// TOOL: org.jetbrains.kotlin.idea.intentions.IfNullToElvisInspection
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Suppress 'IfNullToElvis' for fun foo" "true"
|
||||
|
||||
@Suppress("IfNullToElvis")
|
||||
fun foo(p: List<String?>, b: Boolean) {
|
||||
var v = p[0]
|
||||
<caret>if (v == null) return
|
||||
if (b) v = null
|
||||
}
|
||||
|
||||
// TOOL: org.jetbrains.kotlin.idea.intentions.IfNullToElvisInspection
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Suppress 'KotlinUnusedImport' for file ${file}" "true"
|
||||
|
||||
import<caret> java.io.*
|
||||
|
||||
// TOOL: org.jetbrains.kotlin.idea.inspections.KotlinUnusedImportInspection
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Suppress 'KotlinUnusedImport' for file ${file}" "true"
|
||||
|
||||
@file:Suppress("KotlinUnusedImport")
|
||||
|
||||
import java.io.*
|
||||
|
||||
// TOOL: org.jetbrains.kotlin.idea.inspections.KotlinUnusedImportInspection
|
||||
@@ -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())
|
||||
|
||||
|
||||
@@ -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<String> toolsStrings = InTextDirectivesUtils.findListWithPrefixes(configFileText, "TOOL:");
|
||||
|
||||
if (toolsStrings.isEmpty()) return super.configureLocalInspectionTools();
|
||||
|
||||
return ArrayUtil.toObjectArray(CollectionsKt.map(toolsStrings, new Function1<String, LocalInspectionTool>() {
|
||||
@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;
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user