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.

0
6 comments

There are too many variables in this to diagnose with a simple standalone snippet.

Please try using myFixture.addFile() to setup build.gradle instead.

0

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

cannot find converter factory for build.gradle(com.intellij.psi.impl.source.PsiPlainTextFileImpl)

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?

0

Please share your full project to reproduce

0

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.

0

Hi Twarner,

You also need to include "org.jetbrains.plugins.gradle" plugin to dependencies.

intellij {
version = "2020.3"
setPlugins("java", "gradle", "org.jetbrains.idea.gradle.dsl.impl", "org.intellij.groovy", "org.jetbrains.plugins.gradle")
}

Otherwise test IDE instance does not know that ".gradle" file is actually a groovy file:

cannot find converter factory for build.gradle

and gradle-dsl is not able to parse the `build.gradle` file of type PLAIN_TEXT.

@Test
fun simple() {
val gradlePsiFile = myFixture.addFileToProject("../build.gradle", """
dependencies {
compile("com.sofi:test:release-1")
}
""".trimIndent())

assertTrue("Should be Groovy, but found: ${gradlePsiFile.language}", gradlePsiFile is GroovyFile)
val gradleFile = gradlePsiFile.virtualFile
println(gradleFile.contentsToByteArray().decodeToString())

val buildModel = GradleModelProvider.get().getBuildModel(myModule)
assertNotNull("buildModel is null", buildModel)
assertTrue("buildModel dependencies is empty", buildModel!!.dependencies().artifacts().isNotEmpty())
}

Note that gradle-dsl can also manipulate "kts" files (org.jetbrains.idea.gradle.dsl.kotlin.impl plugin provides necessary extensions).

-- Thanks

1

Thank you Andrei! That worked.

0

Please sign in to leave a comment.