From 6a627b8a7b16ea6e75f9072955e9dc82407e28c0 Mon Sep 17 00:00:00 2001 From: Daniel Brauner <44034965+LeFrosch@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:54:07 +0100 Subject: [PATCH] Add catch2 test support in CLion (#7141) Based on the changes from #7062 this adds support for catch2 run configurations. --- .../radler/RadCatchTestContextProvider.kt | 28 ++++++ .../radler/RadGoogleTestContextProvider.kt | 72 ++------------- .../clwb/radler/RadTestContextProvider.kt | 88 +++++++++++++++++++ .../blaze/clwb/radler/optional-plugin.xml | 1 + 4 files changed, 123 insertions(+), 66 deletions(-) create mode 100644 clwb/src/com/google/idea/blaze/clwb/radler/RadCatchTestContextProvider.kt create mode 100644 clwb/src/com/google/idea/blaze/clwb/radler/RadTestContextProvider.kt diff --git a/clwb/src/com/google/idea/blaze/clwb/radler/RadCatchTestContextProvider.kt b/clwb/src/com/google/idea/blaze/clwb/radler/RadCatchTestContextProvider.kt new file mode 100644 index 00000000000..d17f87aec4e --- /dev/null +++ b/clwb/src/com/google/idea/blaze/clwb/radler/RadCatchTestContextProvider.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2024 The Bazel Authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.idea.blaze.clwb.radler + +import com.jetbrains.rider.model.RadTestElementModel +import com.jetbrains.rider.model.RadTestFramework + +class RadCatchTestContextProvider : RadTestContextProvider() { + + override val testFramework: RadTestFramework = RadTestFramework.Catch + + override fun createTestFilter(test: RadTestElementModel): String? { + return test.test + } +} diff --git a/clwb/src/com/google/idea/blaze/clwb/radler/RadGoogleTestContextProvider.kt b/clwb/src/com/google/idea/blaze/clwb/radler/RadGoogleTestContextProvider.kt index 2d710db86f5..719c8ca4a7c 100644 --- a/clwb/src/com/google/idea/blaze/clwb/radler/RadGoogleTestContextProvider.kt +++ b/clwb/src/com/google/idea/blaze/clwb/radler/RadGoogleTestContextProvider.kt @@ -15,77 +15,17 @@ */ package com.google.idea.blaze.clwb.radler -import com.google.common.util.concurrent.Futures -import com.google.common.util.concurrent.ListenableFuture -import com.google.idea.blaze.base.dependencies.TargetInfo -import com.google.idea.blaze.base.model.primitives.RuleType -import com.google.idea.blaze.base.run.ExecutorType -import com.google.idea.blaze.base.run.SourceToTargetFinder -import com.google.idea.blaze.base.run.TestTargetHeuristic -import com.google.idea.blaze.base.run.producers.RunConfigurationContext -import com.google.idea.blaze.base.run.producers.TestContext -import com.google.idea.blaze.base.run.producers.TestContextProvider -import com.google.idea.blaze.base.util.pluginProjectScope -import com.google.idea.blaze.cpp.CppBlazeRules.RuleTypes -import com.intellij.execution.actions.ConfigurationContext -import com.intellij.util.asSafely -import com.jetbrains.cidr.radler.testing.RadTestPsiElement import com.jetbrains.rider.model.RadTestElementModel import com.jetbrains.rider.model.RadTestFramework -import kotlinx.coroutines.async -import kotlinx.coroutines.guava.await -import kotlinx.coroutines.guava.asListenableFuture -import java.io.File -import java.util.* -class RadGoogleTestContextProvider : TestContextProvider { +class RadGoogleTestContextProvider : RadTestContextProvider() { - override fun getTestContext(context: ConfigurationContext): RunConfigurationContext? { - val psiElement = context.psiLocation.asSafely() ?: return null + override val testFramework: RadTestFramework = RadTestFramework.GTest - if (psiElement.test.framework != RadTestFramework.GTest) { - return null - } + override fun createTestFilter(test: RadTestElementModel): String { + val suite = test.suites?.firstOrNull() ?: "*" + val name = test.test ?: "*" - val target = pluginProjectScope(context.project).async { - chooseTargetForFile(context, findTargets(context).await()) - }.asListenableFuture() - - return TestContext.builder(psiElement, ExecutorType.DEBUG_SUPPORTED_TYPES) - .setTarget(target) - .setTestFilter(createTestFilter(psiElement.test)) - .build() + return "$suite.$name" } } - -private fun findTargets(context: ConfigurationContext): ListenableFuture> { - val virtualFile = context.location?.virtualFile ?: return Futures.immediateFuture(emptyList()) - - return SourceToTargetFinder.findTargetInfoFuture( - context.project, - File(virtualFile.path), - Optional.of(RuleType.TEST), - ) ?: Futures.immediateFuture(emptyList()) -} - -private fun chooseTargetForFile(context: ConfigurationContext, targets: Collection): TargetInfo? { - val psiFile = context.psiLocation?.containingFile ?: return null - val virtualFile = psiFile.virtualFile ?: return null - - val ccTargets = targets.filter { it -> it.kind == RuleTypes.CC_TEST.kind } - - return TestTargetHeuristic.chooseTestTargetForSourceFile( - context.project, - psiFile, - File(virtualFile.path), - ccTargets, - null, - ) -} - -private fun createTestFilter(test: RadTestElementModel): String? { - val suite = test.suites?.firstOrNull() ?: "*" - val name = test.test ?: "*" - - return "$suite.$name" -} \ No newline at end of file diff --git a/clwb/src/com/google/idea/blaze/clwb/radler/RadTestContextProvider.kt b/clwb/src/com/google/idea/blaze/clwb/radler/RadTestContextProvider.kt new file mode 100644 index 00000000000..40f9bb89420 --- /dev/null +++ b/clwb/src/com/google/idea/blaze/clwb/radler/RadTestContextProvider.kt @@ -0,0 +1,88 @@ +/* + * Copyright 2024 The Bazel Authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.idea.blaze.clwb.radler + +import com.google.common.util.concurrent.Futures +import com.google.common.util.concurrent.ListenableFuture +import com.google.idea.blaze.base.dependencies.TargetInfo +import com.google.idea.blaze.base.model.primitives.RuleType +import com.google.idea.blaze.base.run.ExecutorType +import com.google.idea.blaze.base.run.SourceToTargetFinder +import com.google.idea.blaze.base.run.TestTargetHeuristic +import com.google.idea.blaze.base.run.producers.RunConfigurationContext +import com.google.idea.blaze.base.run.producers.TestContext +import com.google.idea.blaze.base.run.producers.TestContextProvider +import com.google.idea.blaze.base.util.pluginProjectScope +import com.google.idea.blaze.cpp.CppBlazeRules.RuleTypes +import com.intellij.execution.actions.ConfigurationContext +import com.intellij.util.asSafely +import com.jetbrains.cidr.radler.testing.RadTestPsiElement +import com.jetbrains.rider.model.RadTestElementModel +import com.jetbrains.rider.model.RadTestFramework +import kotlinx.coroutines.async +import kotlinx.coroutines.guava.asListenableFuture +import kotlinx.coroutines.guava.await +import java.io.File +import java.util.* + +abstract class RadTestContextProvider : TestContextProvider { + + override fun getTestContext(context: ConfigurationContext): RunConfigurationContext? { + val psiElement = context.psiLocation.asSafely() ?: return null + + if (psiElement.test.framework != testFramework) { + return null + } + + val target = pluginProjectScope(context.project).async { + chooseTargetForFile(context, findTargets(context).await()) + }.asListenableFuture() + + return TestContext.builder(psiElement, ExecutorType.DEBUG_SUPPORTED_TYPES) + .setTarget(target) + .setTestFilter(createTestFilter(psiElement.test)) + .build() + } + + protected abstract val testFramework: RadTestFramework + + protected abstract fun createTestFilter(test: RadTestElementModel): String? +} + +private fun findTargets(context: ConfigurationContext): ListenableFuture> { + val virtualFile = context.location?.virtualFile ?: return Futures.immediateFuture(emptyList()) + + return SourceToTargetFinder.findTargetInfoFuture( + context.project, + virtualFile.toNioPath().toFile(), + Optional.of(RuleType.TEST), + ) ?: Futures.immediateFuture(emptyList()) +} + +private fun chooseTargetForFile(context: ConfigurationContext, targets: Collection): TargetInfo? { + val psiFile = context.psiLocation?.containingFile ?: return null + val virtualFile = psiFile.virtualFile ?: return null + + val ccTargets = targets.filter { it -> it.kind == RuleTypes.CC_TEST.kind } + + return TestTargetHeuristic.chooseTestTargetForSourceFile( + context.project, + psiFile, + virtualFile.toNioPath().toFile(), + ccTargets, + null, + ) +} diff --git a/clwb/src/com/google/idea/blaze/clwb/radler/optional-plugin.xml b/clwb/src/com/google/idea/blaze/clwb/radler/optional-plugin.xml index 8d30985fd2a..e24036d1671 100644 --- a/clwb/src/com/google/idea/blaze/clwb/radler/optional-plugin.xml +++ b/clwb/src/com/google/idea/blaze/clwb/radler/optional-plugin.xml @@ -16,6 +16,7 @@ +