FIR IDE: introduce HLLocalInspectionTest
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.idea.inspections
|
||||
|
||||
import org.jetbrains.kotlin.test.uitls.IgnoreTests
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractHLLocalInspectionTest : AbstractLocalInspectionTest() {
|
||||
override fun isFirPlugin() = true
|
||||
|
||||
override val inspectionFileName: String = ".firInspection"
|
||||
|
||||
override fun checkForUnexpectedErrors(fileText: String) {}
|
||||
|
||||
override fun doTestFor(mainFile: File, inspection: AbstractKotlinInspection, fileText: String) {
|
||||
IgnoreTests.runTestIfNotDisabledByFileDirective(mainFile.toPath(), IgnoreTests.DIRECTIVES.IGNORE_FIR, "after") {
|
||||
super.doTestFor(mainFile, inspection, fileText)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,12 @@ import com.intellij.openapi.util.SystemInfo
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.profile.codeInspection.ProjectInspectionProfileManager
|
||||
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl
|
||||
import com.intellij.util.io.outputStream
|
||||
import com.intellij.util.io.write
|
||||
import junit.framework.ComparisonFailure
|
||||
import junit.framework.TestCase
|
||||
import org.jdom.Element
|
||||
import org.jetbrains.annotations.NotNull
|
||||
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
|
||||
import org.jetbrains.kotlin.idea.highlighter.AbstractHighlightingPassBase
|
||||
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils
|
||||
@@ -31,10 +34,14 @@ import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.junit.Assert
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import kotlin.io.path.*
|
||||
|
||||
|
||||
abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
private val inspectionFileName: String
|
||||
protected open val inspectionFileName: String
|
||||
get() = ".inspection"
|
||||
|
||||
private val afterFileNameSuffix: String
|
||||
@@ -121,14 +128,18 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
|
||||
ScriptConfigurationManager.updateScriptDependenciesSynchronously(myFixture.file)
|
||||
}
|
||||
|
||||
doTestFor(mainFile.name, inspection, fileText)
|
||||
doTestFor(mainFile, inspection, fileText)
|
||||
|
||||
if (file is KtFile && !InTextDirectivesUtils.isDirectiveDefined(fileText, "// SKIP_ERRORS_AFTER")) {
|
||||
DirectiveBasedActionUtils.checkForUnexpectedErrors(file as KtFile)
|
||||
checkForUnexpectedErrors(fileText)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun checkForUnexpectedErrors(fileText: String) {
|
||||
DirectiveBasedActionUtils.checkForUnexpectedErrors(file as KtFile)
|
||||
}
|
||||
|
||||
protected fun runInspectionWithFixesAndCheck(
|
||||
inspection: AbstractKotlinInspection,
|
||||
expectedProblemString: String?,
|
||||
@@ -241,7 +252,8 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
|
||||
return true
|
||||
}
|
||||
|
||||
private fun doTestFor(mainFilePath: String, inspection: AbstractKotlinInspection, fileText: String) {
|
||||
protected open fun doTestFor(mainFile: File, inspection: AbstractKotlinInspection, fileText: String) {
|
||||
val mainFilePath = mainFile.name
|
||||
val expectedProblemString = InTextDirectivesUtils.findStringWithPrefixes(
|
||||
fileText, "// $expectedProblemDirectiveName: "
|
||||
)
|
||||
@@ -257,6 +269,7 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
|
||||
}
|
||||
|
||||
val canonicalPathToExpectedFile = mainFilePath + afterFileNameSuffix
|
||||
createAfterFileIfItDoesNotExist(canonicalPathToExpectedFile)
|
||||
try {
|
||||
myFixture.checkResultByFile(canonicalPathToExpectedFile)
|
||||
} catch (e: ComparisonFailure) {
|
||||
@@ -267,6 +280,16 @@ abstract class AbstractLocalInspectionTest : KotlinLightCodeInsightFixtureTestCa
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalPathApi::class)
|
||||
private fun createAfterFileIfItDoesNotExist(canonicalPathToExpectedFile: String) {
|
||||
val path = Path(testDataPath) / canonicalPathToExpectedFile
|
||||
|
||||
if (!Files.exists(path)) {
|
||||
path.createFile().write(editor.document.text)
|
||||
error("File $canonicalPathToExpectedFile was not found and thus was generated")
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val EXTENSIONS = arrayOf(".kt", ".kts", ".java", ".groovy")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user