Cannot parse GradleBuildModel in plugin test
Answered
I am trying to access a test projects GradleBuildModel in a test, but it is coming back as a blank build model.
Simplified gist of what I am trying to do:
@RunWith(JUnit4::class)
class Test : LightJavaCodeInsightFixtureTestCase() {
@Test
fun simple() {
val gradlePath = Paths.get(project.basePath!!).resolve("build.gradle")
gradlePath.createFile()
Files.writeString(gradlePath, """
dependencies {
compile("com.test:test:release-1")
}
""".trimIndent())
val gradleFile = VfsUtil.findFileByIoFile(gradlePath.toFile(), true)!!
val buildModel = GradleModelProvider.get().getBuildModel(project)!!
assertTrue(buildModel.dependencies().artifacts().isNotEmpty())
}
}
The buildModel is not null, but does not contain anything (the artifacts().isNotEmpty() will fail). These messages are printed:
14:22:25,506 DEBUG .impl.FileTypeDetectionService - file:///private/var/folders/hj/6wjn3yv15n7dlmqc43dft_sh0000gp/T/unitTest_simple_1mfPbEwkLDfXzXM3XpFhzUeZYPD/build.gradle; type=Text; 0 14:22:25,533 DEBUG dsl.parser.files.GradleDslFile - cannot find converter factory for build.gradle(com.intellij.psi.impl.source.PsiPlainTextFileImpl)
Implying the file is being read as a plan text file, not a groovy or gradle file like to should be.
Everything works properly when installing the plugin into a real IDEA instance, so I assume something is not being set up correctly with the test, such as the gradle plugin not being applied perhaps. I could not find anything in the docs or forums about applying an ide plugin to a test.
Please sign in to leave a comment.
There are too many variables in this to diagnose with a simple standalone snippet.
Please try using myFixture.addFile() to setup build.gradle instead.
Hi Yann, thanks for replying.
I tried both myFixture.addFileToProject() and myFixture.copyFileToProject() but both resulted in the same empty build model and the same messages about
Which again implies that the groovy and gradle language support is not setup in the test. How can I add support for these within a test?
Please share your full project to reproduce
I have uploaded a zip of a project that reproduces:
Upload id: 2021_01_07_6q1LAWcyzLwGieNH (file: gradle-idea-test.zip)
Please run ./gradlew test and check the test report to see above failures and log messages.
Hi Twarner,
You also need to include "org.jetbrains.plugins.gradle" plugin to dependencies.
Otherwise test IDE instance does not know that ".gradle" file is actually a groovy file:
and gradle-dsl is not able to parse the `build.gradle` file of type PLAIN_TEXT.
Note that gradle-dsl can also manipulate "kts" files (org.jetbrains.idea.gradle.dsl.kotlin.impl plugin provides necessary extensions).
-- Thanks
Thank you Andrei! That worked.