Skip to content

Commit

Permalink
Add catch2 test support in CLion (#7141)
Browse files Browse the repository at this point in the history
Based on the changes from #7062 this adds support for catch2 run configurations.
  • Loading branch information
LeFrosch authored Dec 16, 2024
1 parent 7a84d8b commit 6a627b8
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RadTestPsiElement>() ?: 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<Collection<TargetInfo>> {
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>): 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"
}
Original file line number Diff line number Diff line change
@@ -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<RadTestPsiElement>() ?: 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<Collection<TargetInfo>> {
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>): 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,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<idea-plugin>
<extensions defaultExtensionNs="com.google.idea.blaze">
<TestContextProvider implementation="com.google.idea.blaze.clwb.radler.RadGoogleTestContextProvider"/>
<TestContextProvider implementation="com.google.idea.blaze.clwb.radler.RadCatchTestContextProvider"/>
<BinaryContextProvider implementation="com.google.idea.blaze.clwb.radler.RadBinaryContextProvider"/>
</extensions>
</idea-plugin>

0 comments on commit 6a627b8

Please sign in to comment.