A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
from __future__ import annotations
import signal
from pathlib import Path
from typing import TYPE_CHECKING, Literal
from unittest import mock
import pytest
from marimo._config.config import (
CompletionConfig,
LanguageServersConfig,
MarimoConfig,
merge_default_config,
)
from marimo._config.manager import (
MarimoConfigManager,
get_default_config_manager,
)
from marimo._loggers import get_log_directory
from marimo._messaging.notification import AlertNotification
from marimo._server.lsp import (
BasedpyrightServer,
BaseLspServer,
CompositeLspServer,
CopilotLspServer,
PyLspServer,
PyreflyServer,
TyServer,
any_lsp_server_running,
)
from marimo._utils.platform import is_windows
if TYPE_CHECKING:
from collections.abc import Iterator
@pytest.fixture
def mock_process():
process = mock.MagicMock()
process.returncode = None
return process
@pytest.fixture
def mock_popen(mock_process: mock.MagicMock) -> Iterator[mock.MagicMock]:
with mock.patch("subprocess.Popen", return_value=mock_process) as popen:
yield popen
@pytest.fixture
def mock_which():
with mock.patch("shutil.which", return_value="/mock/path") as which:
yield which
class MockLspServer(BaseLspServer):
id = "mock"
def validate_requirements(self) -> str | Literal[True]:
return True
def get_command(self) -> list[str]:
return ["mock_binary", "--port", str(self.port)]
def missing_binary_alert(self) -> AlertNotification:
return AlertNotification(
title="Mock Alert", description="Mock missing binary"
)
async def test_base_lsp_server_start_stop(
mock_popen: mock.MagicMock,
mock_process: mock.MagicMock,
):
server = MockLspServer(port=8000)
server.validate_requirements = mock.MagicMock(return_value=False)
# Without binary install
alert = await server.start()
assert alert is not None
assert alert.title == "Mock Alert"
assert alert.description == "Mock missing binary"
server.validate_requirements = mock.MagicMock(return_value=True)
# Skip readiness polling: the mock process doesn't listen on a TCP
# port, so _wait_until_ready would poll for its full 5 s timeout.
with mock.patch.object(server, "_wait_until_ready", return_value=True):
alert = await server.start()
assert alert is None
mock_popen.assert_called_once()
assert (
mock_popen.call_args.kwargs["env"]["MARIMO_LSP_TOKEN"]
== server.auth_token
)
assert (
server.is_running() is True
) # Process exists and is running (returncode is None)
# Test stop - on Unix, should kill the process group
if is_windows():
server.stop()
mock_process.terminate.assert_called_once()
else:
mock_process.pid = 12345
with (
mock.patch("os.getpgid", return_value=12345) as mock_getpgid,
mock.patch("os.killpg") as mock_killpg,
):
server.stop()
mock_getpgid.assert_called_once_with(12345)
mock_killpg.assert_called_once_with(12345, signal.SIGTERM)
# Verify wait was called with timeout for graceful shutdown
mock_process.wait.assert_called_once_with(timeout=2)
assert server.process is None
@pytest.mark.parametrize(
"server_type",
[
PyLspServer,
CopilotLspServer,
BasedpyrightServer,
TyServer,
PyreflyServer,
],
)
async def test_lsp_launch_keeps_token_out_of_argv(
server_type: type[BaseLspServer],
tmp_path: Path,
mock_popen: mock.MagicMock,
):
server = server_type(port=8000)
lsp_dir = tmp_path / "_lsp"
lsp_dir.mkdir()
(lsp_dir / "index.cjs").touch()
ty_module = mock.MagicMock()
ty_module.find_ty_bin.return_value = "/path/to/ty"
pyrefly_module = mock.MagicMock()
pyrefly_module.get_pyrefly_bin.return_value = "/path/to/pyrefly"
# Stub installed dependencies, but exercise each real get_command()
# and the actual argv passed to Popen.
with (
mock.patch("marimo._server.lsp.DependencyManager"),
mock.patch(
"marimo._server.lsp.marimo_package_path", return_value=tmp_path
),
mock.patch.dict(
"sys.modules",
{"ty.__main__": ty_module, "pyrefly.__main__": pyrefly_module},
),
mock.patch.object(server, "validate_requirements", return_value=True),
mock.patch.object(server, "_wait_until_ready", return_value=True),
mock.patch.object(server, "_signal_process_tree"),
):
try:
assert await server.start() is None
mock_popen.assert_called_once()
command = mock_popen.call_args.args[0]
assert command
assert all(server.auth_token not in arg for arg in command)
assert (
mock_popen.call_args.kwargs["env"]["MARIMO_LSP_TOKEN"]
== server.auth_token
)
finally:
server.stop()
async def test_base_lsp_server_stop_force_kill_on_timeout(
mock_popen: mock.MagicMock,
mock_process: mock.MagicMock,
):
"""Test that stop() force kills the process if it doesn't respond to terminate."""
del mock_popen
import subprocess
server = MockLspServer(port=8000)
server.validate_requirements = mock.MagicMock(return_value=True)
# Skip readiness polling: the mock process doesn't listen on a TCP
# port, so _wait_until_ready would poll for its full 5 s timeout.
with mock.patch.object(server, "_wait_until_ready", return_value=True):
await server.start()
# Make wait() raise TimeoutExpired to simulate hung process
mock_process.wait.side_effect = [
subprocess.TimeoutExpired("cmd", 2), # First wait (terminate)
None, # Second wait (after kill)
]
if is_windows():
server.stop()
mock_process.terminate.assert_called_once()
mock_process.kill.assert_called_once()
else:
mock_process.pid = 12345
with (
mock.patch("os.getpgid", return_value=12345),
mock.patch("os.killpg") as mock_killpg,
):
server.stop()
# Should have been called twice: SIGTERM then SIGKILL
assert mock_killpg.call_count == 2
mock_killpg.assert_any_call(12345, signal.SIGTERM)
mock_killpg.assert_any_call(12345, signal.SIGKILL)
# wait should be called twice: once after terminate, once after kill
assert mock_process.wait.call_count == 2
assert server.process is None
@pytest.mark.skipif(is_windows(), reason="Unix-only process group test")
async def test_base_lsp_server_stop_noop_on_process_lookup_error(
mock_popen: mock.MagicMock,
mock_process: mock.MagicMock,
):
"""Test that stop() treats ProcessLookupError as a no-op (already exited)."""
del mock_popen
server = MockLspServer(port=8000)
server.validate_requirements = mock.MagicMock(return_value=True)
with mock.patch.object(server, "_wait_until_ready", return_value=True):
await server.start()
mock_process.pid = 12345
with mock.patch("os.getpgid", side_effect=ProcessLookupError):
server.stop()
# ProcessLookupError means process already exited; should not
# fall back to terminate/kill.
mock_process.terminate.assert_not_called()
assert server.process is None
async def test_base_lsp_server_start_lock_prevents_concurrent_starts(
mock_popen: mock.MagicMock,
mock_process: mock.MagicMock,
):
"""Test that start() uses a lock to prevent race conditions."""
del mock_process
import asyncio
server = MockLspServer(port=8000)
server.validate_requirements = mock.MagicMock(return_value=True)
# Skip readiness polling: the mock process doesn't listen on a TCP
# port, so _wait_until_ready would poll for its full 5 s timeout.
with mock.patch.object(server, "_wait_until_ready", return_value=True):
results = await asyncio.gather(
server.start(),
server.start(),
server.start(),
)
# All should succeed (return None), but Popen should only be called once
assert all(r is None for r in results)
# Only one Popen call due to the lock
mock_popen.assert_called_once()
async def test_base_lsp_server_missing_binary(mock_which: mock.MagicMock):
server = MockLspServer(port=8000)
mock_which.return_value = None
server.validate_requirements = mock.MagicMock(return_value=False)
alert = await server.start()
assert alert is not None
assert alert.title == "Mock Alert"
async def test_pylsp_server():
import sys
server = PyLspServer(port=8000)
assert isinstance(server.validate_requirements(), (str, bool))
assert server.get_command() == [
sys.executable,
"-m",
"marimo._server._pylsp",
"--port",
"8000",
"--log-file",
str(get_log_directory() / "pylsp.log"),
]
alert = server.missing_binary_alert()
assert "Python LSP" in alert.title
def test_copilot_server():
server = CopilotLspServer(port=8000)
assert isinstance(server.validate_requirements(), (str, bool))
if server._lsp_bin().exists():
assert "node" in server.get_command()
assert str(8000) in server.get_command()
else:
assert server.get_command() == []
alert = server.missing_binary_alert()
assert "GitHub Copilot" in alert.title
def test_copilot_server_uses_typed_format(tmp_path: Path) -> None:
"""Test that copilot uses the new typed format instead of shell commands."""
from unittest import mock
server = CopilotLspServer(port=8000)
# Create LSP directory with spaces
lsp_dir_with_spaces = tmp_path / "my folder with spaces"
lsp_dir_with_spaces.mkdir(parents=True)
# Create the required files
lsp_bin = lsp_dir_with_spaces / "index.cjs"
lsp_bin.touch()
copilot_dir = lsp_dir_with_spaces / "copilot"
copilot_dir.mkdir()
copilot_bin = copilot_dir / "language-server.cjs"
copilot_bin.touch()
# Mock the _lsp_dir method to return our test directory
with mock.patch.object(
server, "_lsp_dir", return_value=lsp_dir_with_spaces
):
command = server.get_command()
# Find the --lsp argument
lsp_arg_index = command.index("--lsp")
lsp_command = command[lsp_arg_index + 1]
# The command should use the new typed format
assert lsp_command.startswith("copilot:")
assert str(copilot_bin) in lsp_command
# Should NOT contain shell commands like "node" or "--stdio"
assert "node" not in lsp_command
assert "--stdio" not in lsp_command
def test_copilot_server_node_version_validation():
server = CopilotLspServer(port=8000)
# Test missing node
with mock.patch(
"marimo._dependencies.dependencies.DependencyManager.which",
return_value=None,
):
result = server.validate_requirements()
assert isinstance(result, str)
assert "node.js binary is missing" in result
# Test node version < 20
with (
mock.patch(
"marimo._dependencies.dependencies.DependencyManager.which",
return_value="/usr/bin/node",
),
mock.patch("subprocess.run") as mock_run,
):
mock_run.return_value = mock.MagicMock(
returncode=0, stdout="v18.17.0\n"
)
result = server.validate_requirements()
assert isinstance(result, str)
assert "Node.js version 18.17.0 is too old" in result
assert "requires Node.js version 20 or higher" in result
# Test node version >= 20
with (
mock.patch(
"marimo._dependencies.dependencies.DependencyManager.which",
return_value="/usr/bin/node",
),
mock.patch("subprocess.run") as mock_run,
):
mock_run.return_value = mock.MagicMock(
returncode=0, stdout="v20.10.0\n"
)
result = server.validate_requirements()
assert result is True
# Test subprocess failure (fail open)
with (
mock.patch(
"marimo._dependencies.dependencies.DependencyManager.which",
return_value="/usr/bin/node",
),
mock.patch("subprocess.run") as mock_run,
):
mock_run.return_value = mock.MagicMock(
returncode=1, stderr="Command failed"
)
result = server.validate_requirements()
assert result is True # Should fail open
# Test exception during version check (fail open)
with (
mock.patch(
"marimo._dependencies.dependencies.DependencyManager.which",
return_value="/usr/bin/node",
),
mock.patch("subprocess.run", side_effect=Exception("Network error")),
):
result = server.validate_requirements()
assert result is True # Should fail open
def test_composite_server_probes_loopback_ports():
config_reader = get_default_config_manager(current_path=None)
min_port = 8000
num_servers = len(CompositeLspServer.LANGUAGE_SERVERS)
ports = range(min_port, min_port + num_servers + 1)
with mock.patch(
"marimo._server.lsp.find_free_port", side_effect=ports
) as find_free_port:
server = CompositeLspServer(config_reader, min_port=min_port)
assert find_free_port.call_args_list == [
mock.call(port, addr="127.0.0.1") for port in ports
]
assert [lsp.port for lsp in server.servers.values()] == list(ports)[1:]
def test_composite_server():
def as_reader(
completion_config: CompletionConfig, config: LanguageServersConfig
) -> MarimoConfigManager:
return get_default_config_manager(current_path=None).with_overrides(
{
"completion": completion_config,
"language_servers": config,
"experimental": {"lsp": True},
}
)
with mock.patch("marimo._server.lsp.DependencyManager") as mock_dm:
mock_dm.pylsp = mock.MagicMock()
mock_dm.pylsp.has.return_value = True
total_lsp_servers = 5
config = LanguageServersConfig(
{
"pylsp": {"enabled": True},
"ty": {"enabled": True},
"pyrefly": {"enabled": True},
"basedpyright": {"enabled": True},
}
)
completion_config = CompletionConfig(
{
"copilot": True,
"activate_on_typing": True,
"signature_hint_on_typing": False,
}
)
config_reader = as_reader(completion_config, config)
config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert (
len(server.servers) == total_lsp_servers
) # Both pylsp and copilot enabled
assert server._is_enabled(config, "pylsp") is True
assert server._is_enabled(config, "copilot") is True
assert server._is_enabled(config, "ty") is True
assert server._is_enabled(config, "pyrefly") is True
assert server._is_enabled(config, "basedpyright") is True
# Test with only pylsp
config = LanguageServersConfig({"pylsp": {"enabled": True}})
completion_config = CompletionConfig(
{
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
}
)
config_reader = as_reader(completion_config, config)
config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
assert server._is_enabled(config, "pylsp") is True
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is False
assert server._is_enabled(config, "pyrefly") is False
# Test with only ty enabled
config = LanguageServersConfig(
{
"ty": {"enabled": True},
"pylsp": {"enabled": False},
"basedpyright": {"enabled": False},
"pyrefly": {"enabled": False},
}
)
completion_config = CompletionConfig(
{
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
}
)
config_reader = as_reader(completion_config, config)
config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
assert server._is_enabled(config, "pylsp") is False
assert server._is_enabled(config, "basedpyright") is False
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is True
assert server._is_enabled(config, "pyrefly") is False
# Test with only basedpyright enabled
config = LanguageServersConfig(
{
"basedpyright": {"enabled": True},
"pylsp": {"enabled": False},
"ty": {"enabled": False},
"pyrefly": {"enabled": False},
}
)
completion_config = CompletionConfig(
{
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
}
)
config_reader = as_reader(completion_config, config)
config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
assert server._is_enabled(config, "pylsp") is False
assert server._is_enabled(config, "basedpyright") is True
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is False
assert server._is_enabled(config, "pyrefly") is False
# Test with only pyrefly enabled
config = LanguageServersConfig(
{
"pyrefly": {"enabled": True},
"pylsp": {"enabled": False},
"basedpyright": {"enabled": False},
"ty": {"enabled": False},
}
)
completion_config = CompletionConfig(
{
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
}
)
config_reader = as_reader(completion_config, config)
config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
assert server._is_enabled(config, "pylsp") is False
assert server._is_enabled(config, "basedpyright") is False
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is False
assert server._is_enabled(config, "pyrefly") is True
# Test with nothing enabled
config = LanguageServersConfig({"pylsp": {"enabled": False}})
completion_config = CompletionConfig(
{
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
}
)
config_reader = as_reader(completion_config, config)
config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
assert server._is_enabled(config, "pylsp") is False
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is False
def test_basedpyright_server_uses_typed_format():
"""Test that basedpyright uses the new typed format."""
server = BasedpyrightServer(port=8000)
command = server.get_command()
# Find the --lsp argument
lsp_arg_index = command.index("--lsp")
lsp_command = command[lsp_arg_index + 1]
# Should use typed format
assert lsp_command == "basedpyright:basedpyright-langserver"
assert lsp_command.startswith("basedpyright:")
# Should NOT contain shell arguments
assert "--stdio" not in lsp_command
def test_pyrefly_server_uses_typed_format():
"""Test that pyrefly server uses the new typed format."""
from unittest import mock
# Mock the entire pyrefly module and its find_pyrefly_bin function
mock_pyrefly_module = mock.MagicMock()
mock_pyrefly_module.get_pyrefly_bin.return_value = (
"/path/to/pyrefly/binary"
)
with mock.patch.dict(
"sys.modules",
{
"pyrefly": mock_pyrefly_module,
"pyrefly.__main__": mock_pyrefly_module,
},
):
server = PyreflyServer(port=8000)
command = server.get_command()
# Find the --lsp argument
lsp_arg_index = command.index("--lsp")
lsp_command = command[lsp_arg_index + 1]
# Should use typed format
assert lsp_command == "pyrefly:/path/to/pyrefly/binary"
assert lsp_command.startswith("pyrefly:")
# Should NOT contain shell arguments
assert "server" not in lsp_command
def test_ty_server_uses_typed_format():
"""Test that ty server uses the new typed format."""
from unittest import mock
# Mock the entire ty module and its find_ty_bin function
mock_ty_module = mock.MagicMock()
mock_ty_module.find_ty_bin.return_value = "/path/to/ty/binary"
with (
mock.patch.dict(
"sys.modules",
{"ty": mock_ty_module, "ty.__main__": mock_ty_module},
),
mock.patch("shutil.which", return_value="/global/bin/ty"),
):
server = TyServer(port=8000)
command = server.get_command()
# Find the --lsp argument
lsp_arg_index = command.index("--lsp")
lsp_command = command[lsp_arg_index + 1]
# Should use typed format
assert lsp_command == "ty:/path/to/ty/binary"
assert lsp_command.startswith("ty:")
# Should NOT contain shell arguments
assert "server" not in lsp_command
def test_ty_server_handles_spaces_in_path():
"""Test that ty server handles paths with spaces correctly."""
from unittest import mock
# Mock the entire ty module and its find_ty_bin function
mock_ty_module = mock.MagicMock()
mock_ty_module.find_ty_bin.return_value = "/path/with spaces/ty"
with mock.patch.dict(
"sys.modules", {"ty": mock_ty_module, "ty.__main__": mock_ty_module}
):
server = TyServer(port=8000)
command = server.get_command()
# Find the --lsp argument
lsp_arg_index = command.index("--lsp")
lsp_command = command[lsp_arg_index + 1]
# Should use typed format and preserve the path with spaces
assert lsp_command == "ty:/path/with spaces/ty"
assert "/path/with spaces/ty" in lsp_command
@pytest.mark.parametrize("installed", [False, True])
@pytest.mark.parametrize("on_path", [False, True])
@pytest.mark.parametrize("node_available", [False, True])
async def test_ty_server_availability(
installed: bool, on_path: bool, node_available: bool
) -> None:
server = TyServer(port=8000)
with (
mock.patch(
"marimo._server.lsp.DependencyManager.ty.has",
return_value=installed,
),
mock.patch(
"shutil.which",
side_effect={
"ty": "/global/bin/ty" if on_path else None,
"node": "/bin/node" if node_available else None,
}.get,
),
mock.patch.object(
BaseLspServer, "start", new_callable=mock.AsyncMock
) as start,
):
validation = server.validate_requirements()
if not (installed or on_path):
assert (
validation
== "ty is missing. Install it with `pip install ty`."
)
elif not node_available:
assert (
validation
== "node.js binary is missing. Install node at https://nodejs.org/."
)
else:
assert validation is True
await server.start()
assert start.await_count == int(installed or on_path)
def test_ty_server_missing_binary() -> None:
server = TyServer(port=8000)
with (
mock.patch.dict("sys.modules", {"ty": None, "ty.__main__": None}),
mock.patch("shutil.which", return_value=None),
pytest.raises(
FileNotFoundError, match="ty executable not found on PATH"
),
):
server.get_command()
async def test_ty_server_starts_with_binary_on_path(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
mock_popen: mock.MagicMock,
):
bin_dir = tmp_path / "global tools"
bin_dir.mkdir()
binary = bin_dir / ("ty.exe" if is_windows() else "ty")
binary.touch(mode=0o755)
monkeypatch.setenv("PATH", str(bin_dir))
server = TyServer(port=8000)
with (
mock.patch.dict("sys.modules", {"ty": None, "ty.__main__": None}),
mock.patch(
"marimo._server.lsp.DependencyManager.which",
return_value=True,
),
mock.patch.object(server, "_wait_until_ready", return_value=True),
mock.patch.object(server, "_signal_process_tree"),
):
try:
assert await server.start() is None
mock_popen.assert_called_once()
command = mock_popen.call_args.args[0]
lsp_command = command[command.index("--lsp") + 1]
server_type, _, executable = lsp_command.partition(":")
assert server_type == "ty"
assert Path(executable) == binary
finally:
server.stop()
def test_any_lsp_server_running():
# Test any_lsp_server_running function
config: MarimoConfig = merge_default_config(
{
"completion": {
"copilot": True,
"activate_on_typing": True,
"signature_hint_on_typing": False,
},
"language_servers": {"pylsp": {"enabled": False}},
}
)
assert any_lsp_server_running(config) is True
config: MarimoConfig = merge_default_config(
{
"completion": {
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
},
"language_servers": {"pylsp": {"enabled": True}},
}
)
assert any_lsp_server_running(config) is True
config: MarimoConfig = merge_default_config(
{
"completion": {
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
},
"language_servers": {"pylsp": {"enabled": False}},
}
)
assert any_lsp_server_running(config) is False
config: MarimoConfig = merge_default_config(
{
"completion": {
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": False,
},
"language_servers": {}, # default is false
}
)
assert any_lsp_server_running(config) is False
config: MarimoConfig = merge_default_config(
{
"completion": {
"copilot": False,
"activate_on_typing": True,
"signature_hint_on_typing": True,
},
"language_servers": {}, # default is false
}
)
assert any_lsp_server_running(config) is False