Skip to content

Commit

Permalink
feat(devins-run): add default langauge runner support for configurati…
Browse files Browse the repository at this point in the history
…ons and test discovery #100

This commit introduces support for C++ test configurations and test discovery within the DevIns IDE. It includes modifications to the `TestCodeGenTask` to handle C++ test files and the `CppAutoTestService` to provide the necessary functionality for running C++ tests. The `psiFileClass` method is overridden to specify the `OCFile` class for C++ test files.

The `PythonAutoTestService`, `JavaAutoTestService`, `KotlinAutoTestService`, `RustTestService`, `RunService`, `ScalaTestService`, `GoAutoTestService`, and `RunInsCommand` classes have also been modified to support the new C++ test functionality. These changes include overriding the `psiFileClass` method to specify the correct PsiElement class for each language and updating the `runFile` method to accept a `testElement` parameter for C++ tests.

The `JSAutoTestService` has been updated to include the necessary functionality for JavaScript test discovery and execution.
  • Loading branch information
phodal committed Mar 25, 2024
1 parent fb588e3 commit 49e2ae6
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 13 deletions.
Expand Up @@ -14,11 +14,14 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.jetbrains.cidr.cpp.execution.testing.tcatch.CMakeCatchTestRunConfigurationType
import com.jetbrains.cidr.lang.OCLanguage
import com.jetbrains.cidr.lang.psi.OCFile
import com.jetbrains.cidr.lang.psi.OCFunctionDeclaration
import java.io.File

class CppAutoTestService : AutoTestService() {
override fun runConfigurationClass(project: Project): Class<out RunProfile>? = null
override fun psiFileClass(project: Project): Class<out PsiElement> = OCFile::class.java

override fun isApplicable(element: PsiElement): Boolean = element.language is OCLanguage

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
Expand Down
Expand Up @@ -21,7 +21,7 @@ class RunInsCommand(val myProject: Project, private val argument: String) : InsC
PsiManager.getInstance(myProject).findFile(virtualFile) ?: return "<DevInsError>: File not found: $argument"
val testService =
AutoTestService.context(psiFile) ?: return "<DevInsError>: No test service found for file: $argument"
testService.runFile(myProject, virtualFile)
testService.runFile(myProject, virtualFile, null)

return "Running tests for file: $argument"
} catch (e: Exception) {
Expand Down
Expand Up @@ -27,6 +27,8 @@ import com.intellij.testIntegration.TestFinderHelper
class GoAutoTestService : AutoTestService() {
override fun isApplicable(element: PsiElement): Boolean = element.containingFile?.language == GoLanguage.INSTANCE
override fun runConfigurationClass(project: Project): Class<out RunProfile> = GoTestRunConfiguration::class.java
override fun psiFileClass(project: Project): Class<out PsiElement> = GoFile::class.java

override fun lookupRelevantClass(project: Project, element: PsiElement): List<ClassContext> = listOf()

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
Expand Down
Expand Up @@ -28,6 +28,7 @@ import java.io.File
class JavaAutoTestService : AutoTestService() {
override fun runConfigurationClass(project: Project): Class<out RunProfile> = GradleRunConfiguration::class.java
override fun isApplicable(element: PsiElement): Boolean = element.language is JavaLanguage
override fun psiFileClass(project: Project): Class<out PsiElement> = PsiJavaFile::class.java

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
return createConfigForGradle(virtualFile, project)
Expand Down
Expand Up @@ -36,6 +36,7 @@ import kotlin.io.path.Path
class JSAutoTestService : AutoTestService() {
val log = logger<JSAutoTestService>()
override fun runConfigurationClass(project: Project): Class<out RunProfile> = NpmRunConfiguration::class.java
override fun psiFileClass(project: Project): Class<out PsiElement> = JSFile::class.java

override fun isApplicable(element: PsiElement): Boolean {
val sourceFile: PsiFile = element.containingFile ?: return false
Expand Down
Expand Up @@ -30,12 +30,11 @@ import org.jetbrains.plugins.gradle.service.execution.GradleRunConfiguration
import java.io.File

class KotlinAutoTestService : AutoTestService() {
companion object {
val log = logger<KotlinAutoTestService>()
}
private val log = logger<KotlinAutoTestService>()

override fun runConfigurationClass(project: Project): Class<out RunProfile> = GradleRunConfiguration::class.java
override fun isApplicable(element: PsiElement): Boolean = element.language is KotlinLanguage
override fun psiFileClass(project: Project): Class<out PsiElement> = KtFile::class.java

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
return createConfigForGradle(virtualFile, project)
Expand Down
Expand Up @@ -26,8 +26,8 @@ import com.jetbrains.python.run.PythonRunConfigurationProducer

class PythonAutoTestService : AutoTestService() {
override fun isApplicable(element: PsiElement): Boolean = element.language is PythonLanguage

override fun runConfigurationClass(project: Project): Class<out RunProfile> = PythonRunConfiguration::class.java
override fun psiFileClass(project: Project): Class<out PsiElement> = PyFile::class.java

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
val psiFile: PyFile = PsiManager.getInstance(project).findFile(virtualFile) as? PyFile ?: return null
Expand Down
Expand Up @@ -11,7 +11,6 @@ import com.intellij.execution.configurations.RunProfile
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
Expand All @@ -27,10 +26,8 @@ import org.rust.lang.core.psi.*

class RustTestService : AutoTestService() {
override fun runConfigurationClass(project: Project): Class<out RunProfile> = CargoCommandConfiguration::class.java

override fun isApplicable(element: PsiElement): Boolean {
return element.language is RsLanguage
}
override fun isApplicable(element: PsiElement): Boolean = element.language is RsLanguage
override fun psiFileClass(project: Project): Class<out PsiElement> = RsFile::class.java

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
val pkg = findCargoPackage(project, virtualFile) ?: return null
Expand Down
Expand Up @@ -21,6 +21,7 @@ import org.jetbrains.plugins.scala.testingSupport.test.testdata.AllInPackageTest
class ScalaTestService : AutoTestService() {
override fun isApplicable(element: PsiElement): Boolean = element is ScalaPsiElement
override fun runConfigurationClass(project: Project): Class<out RunProfile>? = ScalaTestRunConfiguration::class.java
override fun psiFileClass(project: Project): Class<out PsiElement> = ScalaFile::class.java

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
val scalaFile = PsiManager.getInstance(project).findFile(virtualFile) as? ScalaFile ?: return null
Expand Down
Expand Up @@ -131,7 +131,8 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
runBlocking {
writeTestToFile(request.project, flow, testContext)
navigateTestFile(testContext.outputFile, request.project)
autoTestService?.runFile(request.project, testContext.outputFile)

autoTestService?.runFile(request.project, testContext.outputFile, testContext.testElement)

AutoDevStatusService.notifyApplication(AutoDevStatus.Ready)
indicator.fraction = 1.0
Expand Down
16 changes: 14 additions & 2 deletions src/main/kotlin/cc/unitmesh/devti/provider/RunService.kt
Expand Up @@ -4,6 +4,7 @@ import com.intellij.execution.ExecutionManager
import com.intellij.execution.Executor
import com.intellij.execution.RunManager
import com.intellij.execution.RunnerAndConfigurationSettings
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.execution.configurations.RunProfile
import com.intellij.execution.executors.DefaultRunExecutor
Expand All @@ -13,6 +14,7 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement

interface RunService {
private val logger: Logger get() = logger<RunService>()
Expand All @@ -25,6 +27,8 @@ interface RunService {
*/
fun runConfigurationClass(project: Project): Class<out RunProfile>?

fun psiFileClass(project: Project): Class<out PsiElement>

fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? = null

/**
Expand Down Expand Up @@ -80,8 +84,12 @@ interface RunService {
* @param virtualFile The virtual file that represents the file to be run.
* @return The result of the run operation, or `null` if an error occurred.
*/
fun runFile(project: Project, virtualFile: VirtualFile): String? {
val settings = createRunSettings(project, virtualFile) ?: return null
fun runFile(project: Project, virtualFile: VirtualFile, testElement: PsiElement?): String? {
var settings: RunnerAndConfigurationSettings? = createRunSettings(project, virtualFile)
if (settings == null) {
settings = createDefaultTestConfigurations(project, testElement ?: return null) ?: return null
}

val executor: Executor = DefaultRunExecutor.getRunExecutorInstance()
val builder = ExecutionEnvironmentBuilder.createOrNull(executor, settings) ?: return null

Expand All @@ -90,4 +98,8 @@ interface RunService {

return null
}

fun createDefaultTestConfigurations(project: Project, element: PsiElement): RunnerAndConfigurationSettings? {
return ConfigurationContext(element).configurationsFromContext?.firstOrNull()?.configurationSettings
}
}

0 comments on commit 49e2ae6

Please sign in to comment.