How can I write a test for a plugin editing Groovy files?
已回答
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)
请先登录再写评论。
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":
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:
plugin.xml
Hi Jakub,
Groovy plugin is already defined:
build.gradle.kts
plugin.xml
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
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
Hi Chris, hi Daniil Ovchinnikov,
the transitive dependency did solve the problem. The Groovy file is now recognized. Thank you very much.
Regards,
Martin