Introduce tests runner for run configurations + highlighting + Gradle

This allows us to check two things:
- presence of line markers (haven't been checked by old
  GradleTestRunConfigurationTests, though could've been checked by
  LineMarkerTests)
- some properties of run configurations which will be created from those
  line markers

Old GradleTestRunConfigurationTest have been renamed to
GradleTestRunConfigurationCustomTest and slightly modified to be able
to re-use the same testdata if necessary.
This commit is contained in:
Dmitry Savvinov
2020-01-20 16:47:45 +03:00
parent 4f5c3d1387
commit b34f091fa4
5 changed files with 107 additions and 8 deletions
@@ -271,7 +271,7 @@ public abstract class ExternalSystemTestCase extends UsefulTestCase {
} }
} }
private static void setFileContent(final VirtualFile file, final String content, final boolean advanceStamps) throws IOException { protected static void setFileContent(final VirtualFile file, final String content, final boolean advanceStamps) throws IOException {
new WriteAction<VirtualFile>() { new WriteAction<VirtualFile>() {
@Override @Override
protected void run(@NotNull Result<VirtualFile> result) throws Throwable { protected void run(@NotNull Result<VirtualFile> result) throws Throwable {
@@ -0,0 +1,90 @@
/*
* Copyright 2010-2020 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.codeInsight.gradle
import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.execution.PsiLocation
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution.actions.ConfigurationFromContext
import org.jetbrains.kotlin.gradle.GradleDaemonAnalyzerTestCase
import org.jetbrains.kotlin.gradle.checkFiles
import org.jetbrains.kotlin.idea.run.KotlinGradleRunConfiguration
import org.jetbrains.kotlin.test.TagsTestDataUtil
import org.junit.Test
class GradleTestRunConfigurationAndHighlightingTest : GradleImportingTestCase() {
@Test
fun testExpectClassWithTests() {
doTest()
}
@Test
fun testKotlinJUnitSettings() {
doTest()
}
@Test
fun preferredConfigurations() {
doTest()
}
private fun doTest() {
val files = importProjectFromTestData()
val project = myTestFixture.project
checkFiles(
files.filter { it.extension == "kt" },
project,
object : GradleDaemonAnalyzerTestCase(testLineMarkers = true, checkWarnings = true, checkInfos = false) {
override fun renderAdditionalAttributeForTag(tag: TagsTestDataUtil.TagInfo<*>): String? {
val lineMarkerInfo = tag.data as? LineMarkerInfo<*> ?: return null
// Hacky way to check if it's test line-marker info. Can't rely on extractConfigurationsFromContext returning no
// suitable configurationsFromContext, because it basically works on offsets, so if for some range we have two
// line markers - one with tests, and one without, - then we'll get proper ConfigurationFromContext for both
if ("Run Test" !in lineMarkerInfo.lineMarkerTooltip.orEmpty()) return null
val kotlinConfigsFromContext = lineMarkerInfo.extractConfigurationsFromContext()
.filter { it.configuration is KotlinGradleRunConfiguration }
if (kotlinConfigsFromContext.isEmpty()) return "settings=\"Nothing here\""
val configFromContext = kotlinConfigsFromContext.single() // can we have more than one?
return "settings=\"${configFromContext.renderDescription().replace("\"", "\\\"")}\""
}
}
)
}
private fun ConfigurationFromContext.renderDescription(): String {
val configuration = configuration as KotlinGradleRunConfiguration
val location = PsiLocation(sourceElement)
val context = ConfigurationContext.createEmptyContextForLocation(location)
var result: String? = null
// We can not use settings straight away, because exact settings are determined only after 'onFirstRun'
// (see MultiplatformTestTasksChooser)
onFirstRun(context) {
result = configuration.settings.toString()
}
return result!!
}
private fun LineMarkerInfo<*>.extractConfigurationsFromContext(): List<ConfigurationFromContext> {
val location = PsiLocation(element)
// TODO(dsavvinov): consider getting proper context somehow
val context = ConfigurationContext.createEmptyContextForLocation(location)
return context.configurationsFromContext.orEmpty()
}
override fun testDataDirName(): String = "testRunConfigurations"
}
@@ -10,16 +10,19 @@ import com.intellij.execution.configurations.ConfigurationType
import com.intellij.execution.configurations.ConfigurationTypeUtil import com.intellij.execution.configurations.ConfigurationTypeUtil
import com.intellij.execution.junit.JUnitConfiguration import com.intellij.execution.junit.JUnitConfiguration
import com.intellij.execution.junit.JUnitConfigurationType import com.intellij.execution.junit.JUnitConfigurationType
import org.junit.Test import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.runInEdtAndWait import com.intellij.testFramework.runInEdtAndWait
import com.intellij.util.PlatformUtils import com.intellij.util.PlatformUtils
import org.jetbrains.kotlin.gradle.textWithoutTags
import org.jetbrains.kotlin.idea.run.* import org.jetbrains.kotlin.idea.run.*
import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer import org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer
import org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer import org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer
import org.jetbrains.plugins.gradle.tooling.annotation.TargetVersions import org.jetbrains.plugins.gradle.tooling.annotation.TargetVersions
import org.junit.Test
class GradleTestRunConfigurationTest : GradleImportingTestCase() { class GradleTestRunConfigurationCustomTest : GradleImportingTestCase() {
@Test @Test
@TargetVersions("4.7+") @TargetVersions("4.7+")
fun testPreferredConfigurations() { fun testPreferredConfigurations() {
@@ -87,4 +90,10 @@ class GradleTestRunConfigurationTest : GradleImportingTestCase() {
override fun testDataDirName(): String { override fun testDataDirName(): String {
return "testRunConfigurations" return "testRunConfigurations"
} }
override fun createProjectSubFile(relativePath: String, content: String): VirtualFile {
val file = createProjectSubFile(relativePath)
ExternalSystemTestCase.setFileContent(file, textWithoutTags(content), /* advanceStamps = */ false)
return file
}
} }
@@ -1,7 +1,7 @@
import org.junit.Test import org.junit.Test
class MyKotlinTest { class <lineMarker descr="Run Test" settings=":cleanTest :test --tests \"MyKotlinTest\"">MyKotlinTest</lineMarker> {
@Test @Test
fun testA() { fun <lineMarker descr="Run Test" settings=":cleanTest :test --tests \"MyKotlinTest.testA\"">testA</lineMarker>() {
} }
} }
@@ -1,11 +1,11 @@
import org.junit.Test import org.junit.Test
class MyKotlinTest { class <lineMarker descr="Run Test" settings=":cleanTest :test --tests \"MyKotlinTest\"">MyKotlinTest</lineMarker> {
@Test @Test
fun testA() { fun <lineMarker descr="Run Test" settings=":cleanTest :test --tests \"MyKotlinTest.testA\"">testA</lineMarker>() {
} }
@Test @Test
fun testB() { fun <lineMarker descr="Run Test" settings=":cleanTest :test --tests \"MyKotlinTest.testB\"">testB</lineMarker>() {
} }
} }