|
1 | | -from lsp_types.pyright.session import PyrightSession |
| 1 | +from pathlib import Path |
| 2 | + |
2 | 3 | import lsp_types |
| 4 | +from lsp_types.pyright.session import PyrightSession |
| 5 | + |
| 6 | + |
| 7 | +async def test_pyright_session_with_dynamic_environment(): |
| 8 | + """Test Pyright session with a dynamic temporary environment""" |
| 9 | + import tempfile |
| 10 | + |
| 11 | + # Create a temporary directory |
| 12 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 13 | + temp_path = Path(temp_dir) |
| 14 | + |
| 15 | + module_path = temp_path / "mymodule" |
| 16 | + module_path.mkdir() |
| 17 | + |
| 18 | + # Create a utils.py file with a simple function |
| 19 | + utils_file = module_path / "utils.py" |
| 20 | + utils_content = """\ |
| 21 | +def add_numbers(a: int, b: int) -> int: |
| 22 | + '''Add two numbers together.''' |
| 23 | + return a + b |
| 24 | +""" |
| 25 | + utils_file.write_text(utils_content) |
| 26 | + module_path.joinpath("__init__.py").touch() |
| 27 | + |
| 28 | + # Create code that imports from the utils.py file |
| 29 | + code = """\ |
| 30 | +from mymodule.utils import add_numbers |
| 31 | +
|
| 32 | +result = add_numbers(5, 10) |
| 33 | +print(f"The result is: {result}") |
| 34 | +""" |
| 35 | + |
| 36 | + # Create a Pyright session with the temporary directory as the base_path |
| 37 | + pyright_session = await PyrightSession.create( |
| 38 | + base_path=temp_path, |
| 39 | + initial_code=code, |
| 40 | + options={"include": ["."]}, # Include the current directory for imports |
| 41 | + ) |
| 42 | + |
| 43 | + # Get diagnostics to check for any errors |
| 44 | + diagnostics = await pyright_session.get_diagnostics() |
| 45 | + diags = diagnostics.get("diagnostics", []) |
| 46 | + |
| 47 | + # Verify no errors are reported |
| 48 | + assert len(diags) == 0, f"Expected no diagnostics, but got: {diags}" |
| 49 | + |
| 50 | + # Shutdown the Pyright session |
| 51 | + await pyright_session.shutdown() |
3 | 52 |
|
4 | 53 |
|
5 | 54 | async def test_pyright_session_diagnostics(): |
|
0 commit comments