Testing diagnostics for LSP integration plugin

I’m writing a plugin for a custom language server for YAML.
I’ve tried setting up a simple diagnostics and completion tests.
With completion tests, it works fine, however I can’t make the diagnostics work after numerous attempts.
I run the plugin and the diagnostics work just fine, even more I checked my server logs and it returns the diagnostics during tests. Can anyone point me in the right direction here?

My test looks like this:

fun testDiagnostics() {
    myFixture.configureByFile("diagnostics.yaml")
    val highlights = myFixture.doHighlighting()
    // Filter for errors and warnings
    val diagnostics = highlights.filter { it.severity.name in listOf("ERROR", "WARNING") }
    assertTrue("No diagnostics found", diagnostics.isNotEmpty())
    // Optionally, check for a specific diagnostic message
    assertTrue(diagnostics.any { it.description.contains("string must match regular expression") })
}

Not sure if it’s related but I found the following in logs, which kinda smells like a wrong URI?

WARN - #c.i.p.l.i.LspDiagnosticsCache - Could not find file with diagnostics: file:///src/diagnostics.yaml

Identical test for completion works well:

fun testCompletion() {
    myFixture.configureByFile("completion.yaml")
    myFixture.doHighlighting() // Triggers LSP server to start.
    val completions = myFixture.completeBasic()
    assertTrue("No completions returned", completions != null && completions.isNotEmpty())
    val completionStrings = completions!!.map { it.lookupString }
    assertSize(1, completionStrings)
    assertTrue("n9/v1alpha" in completionStrings)
}

This example might help. The same repository also has a LspTestUtil.kt file that can be copied to your plugin.

1 Like

Thank you! This worked just perfectly :wink: