How can I create a unit test for my language support plugin to emulate opening a file from outside of the project?

Answered

Hi!

I'm developing language support for AWK. 

It appears, that when you open an external file (that resides outside of the current project root), the functions in it are reported as unused and their auto-complete doesn't work.

https://github.com/xonixx/intellij-awk/issues/155

https://github.com/xonixx/intellij-awk/issues/174

I assume this is because the project indices, obviously are not used for the external file.

However this is not my primary question. 

At the beginning I want to add (failing) unit-tests to capture this specific situation -- opening the file in editor from outside of the project.

However, looks like all methods in `CodeInsightTestFixture myFixture`, like for example, 

myFixture.configureByFile(fileName)

configure a file that is inside the test project.

When I'll be able to add a failing unit test I'll think how can I make autocompletion and usages inspection work correctly for this case.

1
4 comments

If a file is not present in the project by simply not adding it anywhere in your test project setup, that would be the same result?

0

Hey Yann! Thank you for your reply. 

I'm not sure I've understood your question. 

Let me please rephrase a bit my problem to hopefully make it a bit more clear. 

So, my AWK support plugin works just fine (as intended) when the *.awk files being edited are a part of the project. But occasionally users (and myself) can need to edit some unrelated external awk script, while working on unrelated IDEA project (for example, in Java). You do this by simply draggind a file onto IDEA window.

This is where some functionality starts failing for editing such files. I'm pretty sure I know what is the reasons behind it. One of them is that I use stubs PSI elements (like https://github.com/xonixx/intellij-awk/blob/main/src/main/java/intellij_awk/psi/AwkFunctionNameStub.java), and it appears that for external files the (stubbed) elements (like function names) are not added to the project stub index. Hence, unused function inspection doesn't work correctly, since I've implemented it via quering the Stub index. 

I believe I know how I could easily fix this. I need to add a if-else somewhere in the inspection that will turn the inspection off if it sees the current file is out of the project. But I don't want to just add the fix without the unit test for this particular case. I need a test similar to the already existing tests:

https://github.com/xonixx/intellij-awk/blob/main/src/test/java/intellij_awk/AwkInspectionTests.java

(the actual test *.awk files are in https://github.com/xonixx/intellij-awk/tree/main/src/test/testData/inspection

So basically I need to add logic very similar to checkByFileNoProblemAtCaret:

https://github.com/xonixx/intellij-awk/blob/main/src/test/java/intellij_awk/AwkInspectionTests.java#L228

with the exception that the line myFixture.configureByFile(before); needs to configure the project-external file instead of project-local. If only it's possible.

So I'll make sure that the test below is not showing incorrect 'function usedFunction is never used'

# file: test.awk
{
print usedFunction()
}

function <caret>usedFunction() {
}

I absolutelly don't like to rely on manual testing. 

 By the way, a tangent question. Are there any recommendations (for me, as a language support plugin implementor) to supporting the editing functionality for external files? Does it make sense to somehow highlight this subject in docs (https://plugins.jetbrains.com/docs/intellij/developing-plugins.html)?

Thank you!

0

Ok got it, thanks for sharing your solution

0

Please sign in to leave a comment.