Merge pull request #101 from oraios/tests/more_languages

Tests for java, minor extension for multilspy
This commit is contained in:
Michael Panchenko
2025-04-27 14:41:14 +02:00
committed by GitHub
8 changed files with 144 additions and 17 deletions
+35 -16
View File
@@ -594,7 +594,7 @@ class LanguageServer:
:return: A list of locations where the symbol is referenced (excluding ignored directories)
"""
if not self.server_started:
self.logger.log(
"request_references called before Language Server started",
@@ -604,21 +604,30 @@ class LanguageServer:
with self.open_file(relative_file_path):
# sending request to the language server and waiting for response
response = await self.server.send.references(
{
"context": {"includeDeclaration": False},
"textDocument": {
"uri": pathlib.Path(os.path.join(self.repository_root_path, relative_file_path)).as_uri()
},
"position": {"line": line, "character": column},
}
)
try:
response = await self.server.send.references(
{
"textDocument": {"uri": PathUtils.path_to_uri(os.path.join(self.repository_root_path, relative_file_path))},
"position": {"line": line, "character": column},
"context": {"includeDeclaration": False},
}
)
except Exception as e:
# Catch LSP internal error (-32603) and raise a more informative exception
from multilspy.lsp_protocol_handler.server import Error
if isinstance(e, Error) and getattr(e, 'code', None) == -32603:
raise RuntimeError(
f"LSP internal error (-32603) when requesting references for {relative_file_path}:{line}:{column}. "
"This often occurs when requesting references for a symbol not referenced in the expected way. "
) from e
raise
if response is None:
return []
ret: List[multilspy_types.Location] = []
assert isinstance(response, list), f"Unexpected response from Language Server: {response}"
assert isinstance(response, list), f"Unexpected response from Language Server (expected list, got {type(response)}): {response}"
for item in response:
assert isinstance(item, dict)
assert isinstance(item, dict), f"Unexpected response from Language Server (expected dict, got {type(item)}): {item}"
assert LSPConstants.URI in item
assert LSPConstants.RANGE in item
@@ -1720,11 +1729,21 @@ class SyncLanguageServer:
:return List[multilspy_types.Location]: A list of locations where the symbol is referenced
"""
result = asyncio.run_coroutine_threadsafe(
self.language_server.request_references(file_path, line, column), self.loop
).result(timeout=self.timeout)
try:
result = asyncio.run_coroutine_threadsafe(
self.language_server.request_references(file_path, line, column), self.loop
).result(timeout=self.timeout)
except Exception as e:
from multilspy.lsp_protocol_handler.server import Error
if isinstance(e, Error) and getattr(e, 'code', None) == -32603:
raise RuntimeError(
f"LSP internal error (-32603) when requesting references for {file_path}:{line}:{column}. "
"This often occurs when requesting references for a symbol not referenced in the expected way. "
) from e
raise
return result
def request_references_with_content(
self, relative_file_path: str, line: int, column: int, context_lines_before: int = 0, context_lines_after: int = 0
) -> List[MatchedConsecutiveLines]:
+8 -1
View File
@@ -90,7 +90,14 @@ class PathUtils:
parsed = urlparse(uri)
host = "{0}{0}{mnt}{0}".format(os.path.sep, mnt=parsed.netloc)
return os.path.normpath(os.path.join(host, url2pathname(unquote(parsed.path))))
@staticmethod
def path_to_uri(path: str) -> str:
"""
Converts a file path to a file URI (file:///...).
"""
return str(Path(path).absolute().as_uri())
@staticmethod
def is_glob_pattern(pattern: str) -> bool:
"""Check if a pattern contains glob-specific characters."""
+54
View File
@@ -0,0 +1,54 @@
import os
import pytest
from multilspy.multilspy_config import Language
def find_symbol_recursive(symbols, name):
for symbol in symbols:
if symbol.get("name") == name:
return True
if symbol.get("children"):
if find_symbol_recursive(symbol["children"], name):
return True
return False
class TestJavaLanguageServer:
@pytest.mark.parametrize("language_server", [Language.JAVA], indirect=True)
def test_find_symbol(self, language_server):
symbols = language_server.request_full_symbol_tree()
assert find_symbol_recursive(symbols, "Main"), "Main class not found in symbol tree"
assert find_symbol_recursive(symbols, "Utils"), "Utils class not found in symbol tree"
assert find_symbol_recursive(symbols, "Model"), "Model class not found in symbol tree"
@pytest.mark.parametrize("language_server", [Language.JAVA], indirect=True)
def test_find_referencing_symbols(self, language_server):
# Use correct Maven/Java file paths
file_path = os.path.join("src", "main", "java", "test_repo", "Utils.java")
refs = language_server.request_references(file_path, 4, 20)
print(f"References for Utils.printHello: {refs}")
assert any("Main.java" in ref.get("relativePath", "") for ref in refs), "Main should reference Utils.printHello"
# Dynamically determine the correct line/column for the 'Model' class name
file_path = os.path.join("src", "main", "java", "test_repo", "Model.java")
symbols = language_server.request_document_symbols(file_path)
model_symbol = None
for sym in symbols[0]:
if sym.get("name") == "Model" and sym.get("kind") == 5: # 5 = Class
model_symbol = sym
break
assert model_symbol is not None, "Could not find 'Model' class symbol in Model.java"
rng = model_symbol["range"]["start"]
print(f"Model symbol range: {model_symbol['range']}")
refs = language_server.request_references(file_path, rng["line"], rng["character"])
print(f"References for Model class: {refs}")
assert any("Main.java" in ref.get("relativePath", "") for ref in refs), "Main should reference Model"
@pytest.mark.parametrize("language_server", [Language.JAVA], indirect=True)
def test_overview_methods(self, language_server):
symbols = language_server.request_full_symbol_tree()
assert find_symbol_recursive(symbols, "Main"), "Main missing from overview"
assert find_symbol_recursive(symbols, "Utils"), "Utils missing from overview"
assert find_symbol_recursive(symbols, "Model"), "Model missing from overview"
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test_repo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Java Test Repo</name>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>
@@ -0,0 +1,13 @@
package test_repo;
public class Main {
public static void main(String[] args) {
Utils.printHello();
Model model = new Model("Cascade");
System.out.println(model.getName());
acceptModel(model);
}
public static void acceptModel(Model m) {
// Do nothing, just for LSP reference
}
}
@@ -0,0 +1,13 @@
package test_repo;
public class Model {
private String name;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
@@ -0,0 +1,7 @@
package test_repo;
public class Utils {
public static void printHello() {
System.out.println("Hello from Utils!");
}
}