FindUsageProvider implementation is not called in tests Follow
Answered
I've registered a findUsagesProvider as follows. It works well in running the IDE/plugin.
<!-- Enables "Find Usages" -->
<lang.findUsagesProvider language="FSD"
implementationClass="io.github.facilityapi.intellij.FsdFindUsagesProvider" />
However, the following code does not hit breakpoints in my usage provider. From stepping through in the debugger, the test fixture appears to be unaware of my find usage provider. I've set a caret via <caret> in the test data file, which is located in src/test/testData/findUsages/ExampleApi.fsd. I've noticed a similar project with rename tests via BasePlatformTestCase.
class FsdFindUsageProviderTest: BasePlatformTestCase() {
override fun getTestDataPath() = "src/test/testData/findUsages"
fun `test find usages works`() {
val usageInfos = myFixture.testFindUsages("ExampleApi.fsd")
assertEquals(1, usageInfos.size)
}
}
The testing docs suggest this should work. What am I missing?
Please sign in to leave a comment.
Update: It does call into the registered provider and instantiates the given DefaultWordsScanner. It finds the element under the caret, but turns up no results. Running in the IDE returns 6 results (IIRC). I'm unclear on what cause of the disparity between the tests and running plugin might be.
Does your test setup all necessary additional files/code/libraries/project configuration/facets etc. etc. compared to real IDE?
I think I've accounted for everything in Part IV of the guide docs (https://plugins.jetbrains.com/docs/intellij/testing-plugins.html) and in the custom language testing tutorial. I might be missing something, but I think for my case all I needed to setup is test data. There's only one file involved in the test.
As far as project configuration goes, I used the JetBrains template. I can get some simpler tests to run just fine, like these: https://github.com/FacilityApi/FacilityIntellij/blob/37052bbc9681969cdbf92ca79c66ada0b719dbde/src/test/kotlin/io/github/facilityapi/intellij/FacilityApiPluginTests.kt
I've stepped through the internals of testFindUsages a little bit, but there's a lot going on under the hood. I haven't been able to nail down exactly what's going wrong.
Perhaps something isn't setup correctly after all. I used FileTypeIndex.getFiles method in my reference implementation, similar to https://plugins.jetbrains.com/docs/intellij/psi-helper-and-utilities.html#define-a-utility-to-search-properties
https://github.com/FacilityApi/FacilityIntellij/blob/4122e757712f8c56c5a99e9027bdccfdf2828fe7/src/main/kotlin/io/github/facilityapi/intellij/reference/FsdReferenceUtils.kt#L20
Stepping through in a debugger that call returns zero virtual files, even though testFindUsages seems to initialize the test fixture with the file specified by the parameter. It is at least dealing with the correct test data file, the element under the caret is correct.
Perhaps the test fixture isn't populating the appropriate index? If I place a breakpoint on the FileTypeIndex.getFiles line, the test passes. Maybe there's a data race in the test fixture? Or maybe the FileTypeIndex isn't guaranteed to be populated immediately. Either way, I'm interested in a way to get it working reliably.
Workaround
Initializing the test fixture with CodeInsightTestFixture.configureByText, calling findUsages on the test fixture, and verifying the returned usages works reliably. I'll probably use configureByText for tests (at least for now).
Maybe that corroborates my suspicion of the test fixture's issue querying the file index?
FileTypeIndex is guaranteed to work the same in tests as in production, given the test data/project setup is correct.