|
| 1 | +# Copyright 2021- Python Language Server Contributors. |
| 2 | + |
| 3 | +from pathlib import PureWindowsPath |
| 4 | +from types import SimpleNamespace |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pylsp import uris |
| 9 | +from pylsp.plugins.definition import pylsp_definitions |
| 10 | +from pylsp.plugins.references import pylsp_references |
| 11 | +from pylsp.workspace import Document |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize("operation", ["definition", "references"]) |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "document_uri,target_path,expected_uri", |
| 17 | + [ |
| 18 | + ( |
| 19 | + "file:///c:/project/main.py", |
| 20 | + r"\\server\share\target.py", |
| 21 | + "file://server/share/target.py", |
| 22 | + ), |
| 23 | + ( |
| 24 | + "file://server/share/main.py", |
| 25 | + r"\\other\share\target.py", |
| 26 | + "file://other/share/target.py", |
| 27 | + ), |
| 28 | + ( |
| 29 | + "file://server/share/main.py", |
| 30 | + r"C:\project\target.py", |
| 31 | + "file:///c:/project/target.py", |
| 32 | + ), |
| 33 | + ( |
| 34 | + "file://server/share/main.py", |
| 35 | + r"\\server\share\target.py", |
| 36 | + "file://server/share/target.py", |
| 37 | + ), |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_location_uri( |
| 41 | + config, workspace, monkeypatch, operation, document_uri, target_path, expected_uri |
| 42 | +) -> None: |
| 43 | + monkeypatch.setattr(uris, "IS_WIN", True) |
| 44 | + name = SimpleNamespace( |
| 45 | + module_path=PureWindowsPath(target_path), |
| 46 | + line=1, |
| 47 | + column=0, |
| 48 | + name="target", |
| 49 | + is_definition=lambda: True, |
| 50 | + in_builtin_module=lambda: False, |
| 51 | + ) |
| 52 | + script = SimpleNamespace( |
| 53 | + goto=lambda **kwargs: [name], get_references=lambda **kwargs: [name] |
| 54 | + ) |
| 55 | + document = Document(document_uri, workspace, "target()") |
| 56 | + monkeypatch.setattr(document, "jedi_script", lambda **kwargs: script) |
| 57 | + position = {"line": 0, "character": 0} |
| 58 | + |
| 59 | + if operation == "definition": |
| 60 | + locations = pylsp_definitions(config, document, position) |
| 61 | + else: |
| 62 | + locations = pylsp_references(document, position, exclude_declaration=False) |
| 63 | + |
| 64 | + assert len(locations) == 1 |
| 65 | + assert locations[0]["uri"] == expected_uri |
0 commit comments