Skip to content

Commit

Permalink
Merge pull request #20 from nikolaymatrosov/feature/run-from-gherkin
Browse files Browse the repository at this point in the history
Enable running Features/Scenarios from Gherkin file.
  • Loading branch information
nikolaymatrosov authored May 17, 2024
2 parents 5672ca4 + 4647a38 commit e7accf9
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

## [Unreleased]
### Added
- Initial scaffold created from [IntelliJ Platform Plugin Template](https://github.com/JetBrains/intellij-platform-plugin-template)
- Ability to run tests from feature files

## [0.1.0] - 2024-04-30
### Added
- Step Definition Index
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.intellij.util.indexing.FileBasedIndex
import com.intellij.util.indexing.ID
import org.jetbrains.plugins.cucumber.CucumberStepIndex

class CucumberStepIndex : CucumberStepIndex() {
class CucumberGoStepIndex : CucumberStepIndex() {

override fun getName(): ID<Boolean, List<Int>> {
return INDEX_ID
Expand Down Expand Up @@ -52,4 +52,5 @@ class CucumberStepIndex : CucumberStepIndex() {
}

val INDEX_ID = ID.create<Boolean, List<Int>>("go.cucumber.step")
val PACKAGES = arrayOf("github.com/cucumber/godog")
val GODOG_PACKAGE = "github.com/cucumber/godog"
val PACKAGES = arrayOf(GODOG_PACKAGE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.nikolaymatrosov.cucumbergo.godog

import com.goide.execution.testing.GoTestEventsJsonConverter
import com.intellij.execution.testframework.TestConsoleProperties

class GodogEventsConverter(s: String, consoleProperties: TestConsoleProperties) : GoTestEventsJsonConverter(
"Godog", s, consoleProperties
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.nikolaymatrosov.cucumbergo.godog

import com.github.nikolaymatrosov.cucumbergo.GODOG_PACKAGE
import com.github.nikolaymatrosov.cucumbergo.run.GodogRunningState
import com.goide.GoFileType
import com.goide.execution.testing.GoTestFramework
import com.goide.execution.testing.GoTestRunConfiguration
import com.goide.execution.testing.GoTestRunningState
import com.goide.psi.GoFile
import com.goide.psi.GoFunctionOrMethodDeclaration
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.execution.testframework.TestConsoleProperties
import com.intellij.execution.testframework.sm.runner.OutputToGeneralTestEventsConverter
import com.intellij.openapi.module.Module
import com.intellij.psi.PsiFile


class GodogFramework : GoTestFramework() {
override fun getName(): String {
return "Godog"
}

override fun isAvailable(module: Module?): Boolean {
return true
}

override fun isAvailableOnFile(psiFile: PsiFile?): Boolean {
return psiFile != null
&& psiFile.fileType === GoFileType.INSTANCE
&& (psiFile as GoFile).imports.any { it.path == GODOG_PACKAGE }
}

override fun isAvailableOnFunction(goFunctionOrMethodDeclaration: GoFunctionOrMethodDeclaration?): Boolean {
return false
}

override fun supportsJsonTestsOutput(): Boolean {
return true
}

override fun newRunningState(
executionEnvironment: ExecutionEnvironment,
module: Module,
goTestRunConfiguration: GoTestRunConfiguration
): GoTestRunningState {
return GodogRunningState(executionEnvironment, module, goTestRunConfiguration)
}

override fun createTestEventsConverter(
s: String,
testConsoleProperties: TestConsoleProperties,
module: Module?
): OutputToGeneralTestEventsConverter {
return GodogEventsConverter(s, testConsoleProperties)
}

override fun getPackageConfigurationName(packageName: String): String {
return "Godog"
}

companion object {
val INSTANCE: GodogFramework = GodogFramework()

init {
all().add(INSTANCE)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.github.nikolaymatrosov.cucumbergo.run

import ai.grazie.utils.capitalize
import com.github.nikolaymatrosov.cucumbergo.CucumberExtension
import com.github.nikolaymatrosov.cucumbergo.godog.GodogFramework
import com.goide.execution.GoBuildingRunConfiguration.Kind
import com.goide.execution.GoRunUtil
import com.goide.execution.testing.GoTestRunConfiguration
import com.goide.execution.testing.GoTestRunConfigurationProducerBase
import com.goide.psi.GoFile
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.plugins.cucumber.psi.GherkinFile
import org.jetbrains.plugins.cucumber.psi.GherkinScenario
import org.jetbrains.plugins.cucumber.psi.GherkinScenarioOutline

class GodogRunConfigurationProducer protected constructor() :
GoTestRunConfigurationProducerBase(GodogFramework.INSTANCE) {
override fun setupConfigurationFromContext(
configuration: GoTestRunConfiguration,
context: ConfigurationContext,
sourceElement: Ref<*>
): Boolean {
val element = sourceElement.get() as PsiElement
if (element.containingFile is GherkinFile) {
val file = element.containingFile.virtualFile

// assuming all steps are in the same directory
CucumberExtension().getStepDefinitionContainers(element.containingFile as GherkinFile).first().let {
configuration.workingDirectory = it.virtualFile.parent.path
configuration.`package` = (it as GoFile).getImportPath(false).toString()
}

configuration.testFramework = GodogFramework.INSTANCE
configuration.kind = Kind.PACKAGE
configuration.goToolParams = GoRunUtil.filterOutInstallParameter(configuration.goToolParams)
val scenario = PsiTreeUtil.getParentOfType(
element,
GherkinScenario::class.java
)

val scenarioOutline = PsiTreeUtil.getParentOfType(
element,
GherkinScenarioOutline::class.java
)

var pattern = "^\\QTest" + file.nameWithoutExtension.capitalize() + "\\E\$"

if (scenario != null) {
pattern = pattern + "/^" + scenario.scenarioName + "$"
} else if (scenarioOutline != null) {
pattern = pattern + "/^" + scenarioOutline.scenarioName + "(#\\d+)?$"
}
configuration.pattern = pattern

configuration.setGeneratedName()
return true
}
// if (element is PsiDirectory) {
// val dir = element.virtualFile
// if (!FileTypeIndex.containsFileOfType(
// GherkinFileType.INSTANCE,
// GlobalSearchScopesCore.directoryScope(element.getProject(), dir, true)
// )
// ) {
// return false
// }
// val directoryPath = dir.path
// configuration.testFramework = GodogFramework.INSTANCE
// configuration.directoryPath = directoryPath
// configuration.workingDirectory = directoryPath
// configuration.kind = Kind.DIRECTORY
// configuration.goToolParams = GoRunUtil.filterOutInstallParameter(configuration.goToolParams)
// configuration.setGeneratedName()
//
// return true
// }
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.nikolaymatrosov.cucumbergo.run

import com.goide.execution.testing.GoTestRunConfiguration
import com.goide.execution.testing.GoTestRunningState
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.openapi.module.Module


class GodogRunningState(env: ExecutionEnvironment, module: Module, configuration: GoTestRunConfiguration) :
GoTestRunningState(env, module, configuration)
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<extensions defaultExtensionNs="com.intellij">

<fileBasedIndex implementation="com.github.nikolaymatrosov.cucumbergo.CucumberStepIndex"/>
<fileBasedIndex implementation="com.github.nikolaymatrosov.cucumbergo.CucumberGoStepIndex"/>

<pom.declarationSearcher implementation="com.github.nikolaymatrosov.cucumbergo.search.StepDeclarationSearcher"/>
<referencesSearch implementation="com.github.nikolaymatrosov.cucumbergo.search.StepDefinitionUsageSearcher"/>
Expand All @@ -23,6 +23,8 @@
<codeInsight.lineMarkerProvider
language="go"
implementationClass="com.github.nikolaymatrosov.cucumbergo.search.CucumberLineMarkerProvider"/>
<runConfigurationProducer
implementation="com.github.nikolaymatrosov.cucumbergo.run.GodogRunConfigurationProducer"/>
</extensions>


Expand Down

0 comments on commit e7accf9

Please sign in to comment.