How can I write a test for a plugin editing Groovy files?

Answered

I'm developing a plugin which converts JUnit test to Spock Tests. Therefore I am modifying Groovy files. This seems to be a problem for my test.

I'm having the following test case

class ConvertJavaToGroovyTest : LightJavaCodeInsightFixtureTestCase() {

override fun getTestDataPath() = "src/test/resources/testdata"

fun testConversion() {
val psiFile = myFixture.configureByFile("BookTest.groovy") as GroovyFile
testAction(ConvertJavaToGroovy(), psiFile)
myFixture.checkResultByFile("BookTestTransformed.groovy")
}

/** inspired by myFixture.testAction */
private fun testAction(action: AnAction, psiFile: PsiFile): Presentation? {
val event = TestActionEvent(action)
if (event.presentation.isEnabled && event.presentation.isVisible) {
JUnitToSpockApplier(event, psiFile).transformToSpock()
}
return event.presentation
}
}

And I'm getting the following error:

class com.intellij.psi.impl.source.PsiPlainTextFileImpl cannot be cast to class org.jetbrains.plugins.groovy.lang.psi.GroovyFile (com.intellij.psi.impl.source.PsiPlainTextFileImpl and org.jetbrains.plugins.groovy.lang.psi.GroovyFile are in unnamed module of loader 'app')

The file is found, but is considered to be plain text as the Groovy file type is unknown. I think I have to activate the Groovy extension, but I don't know how to do that correctly.

I tried to copy some Groovy test files from the Community Edition to inherit from LightGroovyTestCase.

This results in the following error:

java.lang.RuntimeException: java.io.IOException: Cannot find IntelliJ IDEA project files at /home/mhofmannsobik/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2019.3.3/4c54deba9ff34a615b3072cd2def3558ff462987/ideaIC-2019.3.3

at com.intellij.testFramework.LightPlatformTestCase.initProject(LightPlatformTestCase.java:252)
at com.intellij.testFramework.LightPlatformTestCase.doSetup(LightPlatformTestCase.java:307)
at com.intellij.testFramework.fixtures.impl.LightIdeaTestFixtureImpl.setUp(LightIdeaTestFixtureImpl.java:38)
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.setUp(CodeInsightTestFixtureImpl.java:1188)
at com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.setUp(LightJavaCodeInsightFixtureTestCase.java:96)
at org.jetbrains.plugins.groovy.LightGroovyTestCase.setUp(LightGroovyTestCase.kt:20)
5 comments
Comment actions Permalink
Official comment

Hi Martin Hofmannsobik

Chris Campbell is right, this is the same problem. 
Groovy plugin depends on properties plugin, to make the test load the Groovy plugin please include "properties":

setPlugins("Groovy", "java", "properties") // Bundled plugin dependencies

 

Comment actions Permalink

Martin,

It looks like you need to add the Groovy plugin to your dependencies. You should declare it both in Gradle build file and plugin.xml.

Please check the documentation regarding the Plugin Dependencies.

 

build.gradle:

plugins 'Groovy', ...

plugin.xml

<depends>org.intellij.groovy</depends>
0
Comment actions Permalink

Hi Jakub,

Groovy plugin is already defined:

build.gradle.kts

intellij {
...

setPlugins("Groovy", "java") // Bundled plugin dependencies
}

plugin.xml

<depends>com.intellij.modules.java</depends>
<depends>org.intellij.groovy</depends>

My production code works fine, it's just a problem of the test.

If I missed s.th. relevant, here's complete the project code: https://github.com/masooh/groovyfier/tree/first-test

0
Comment actions Permalink

Hi Martin,

 

Not sure but this sounds exactly like a problem I just faced. Basically, for testing you need to explicitly include all plugins that you depend on - even transitive ones - even if your set up works in production. Details here

https://github.com/JetBrains/gradle-intellij-plugin/issues/38

For example, to depend on the JavaScript plugin this means also depending on "CSS" plugin. You'd have to figure out what other plugins the groovy plugin depends on and add that to the list.

 

Cheers,
Chris

0
Comment actions Permalink

Hi Chris, hi Daniil Ovchinnikov,

the transitive dependency did solve the problem. The Groovy file is now recognized. Thank you very much.

Regards,

Martin

0

Please sign in to leave a comment.