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
test: test quitting the server (#711)
Akshay Agrawal4aebb28f3db9
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.pyindex 573ec876de..2351af9150 100644--- a/tests/_cli/test_cli.py+++ b/tests/_cli/test_cli.py@@ -4,19 +4,69 @@ Crude CLI testsRequires frontend to be built"""+import contextlibimport os+import signalimport socketimport subprocess+import sysimport tempfileimport timeimport urllib.request-from typing import Optional+from typing import Any, Callable, Iterator, Optional++import pytestfrom marimo import __version__from marimo._ast import codegenfrom marimo._ast.cell import CellConfig+def _is_win32() -> bool:+ return sys.platform == "win32"+++def _patch_signals_win32() -> Iterator[None]:+ old_handler: Any = None+ try:+ if _is_win32():+ old_handler = signal.signal(signal.SIGINT, lambda *_: ...)+ yield+ finally:+ if old_handler is not None:+ signal.signal(signal.SIGINT, old_handler)+++def _interrupt(process: subprocess.Popen) -> None:+ if _is_win32():+ os.kill(process.pid, signal.CTRL_C_EVENT)+ else:+ os.kill(process.pid, signal.SIGINT)+++def _confirm_shutdown(process: subprocess.Popen) -> None:+ if _is_win32():+ process.stdin.write(b"y\r\n")+ else:+ process.stdin.write(b"y\n")+ process.stdin.flush()+++def _check_shutdown(+ process, check_fn: Optional[Callable[[int], bool]] = None+) -> None:+ max_tries = 3+ tries = 0+ while process.poll() is None and tries < max_tries:+ time.sleep(1)+ tries += 1+ if check_fn is None:+ assert process.poll() == 0+ else:+ assert check_fn(process.poll())++def _try_fetch(port: int, host: str = "localhost") -> Optional[bytes]:contents = Nonefor _ in range(10):@@ -28,6 +78,20 @@ def _try_fetch(port: int, host: str = "localhost") -> Optional[bytes]:return contents+def _check_started(port: int, host: str = "localhost") -> Optional[bytes]:+ assert _try_fetch(port, host) is not None+++def _temp_run_file(directory: tempfile.TemporaryDirectory) -> str:+ filecontents = codegen.generate_filecontents(+ ["import marimo as mo"], ["one"], cell_configs=[CellConfig()]+ )+ path = os.path.join(directory.name, "run.py")+ with open(path, "w", encoding="utf-8") as f:+ f.write(filecontents)+ return path++def _check_contents(p: subprocess.Popen, # type: ignorephrase: bytes,@@ -155,3 +219,71 @@ def test_cli_custom_host() -> None:)contents = _try_fetch(port, host)_check_contents(p, b"marimo-mode data-mode='edit'", contents)++[email protected](condition=_is_win32(), reason="flaky on Windows")+def test_cli_edit_interrupt_twice() -> None:+ # two SIGINTs should kill the CLI+ port = _get_port()+ p = subprocess.Popen(["marimo", "edit", "-p", str(port), "--headless"])+ _check_started(port)+ with _patch_signals_win32():+ _interrupt(p)+ assert p.poll() is None+ _interrupt(p)+ # exit code is system dependent when killed by signal+ _check_shutdown(p, check_fn=lambda code: code is not None)++[email protected](condition=_is_win32(), reason="flaky on Windows")+def test_cli_run_interrupt_twice() -> None:+ # two SIGINTs should kill the CLI+ d = tempfile.TemporaryDirectory()+ path = _temp_run_file(d)+ port = _get_port()+ p = subprocess.Popen(+ ["marimo", "run", path, "-p", str(port), "--headless"]+ )+ _check_started(port)+ with _patch_signals_win32():+ _interrupt(p)+ assert p.poll() is None+ _interrupt(p)+ # exit code is system dependent when killed by signal+ _check_shutdown(p, check_fn=lambda code: code is not None)++[email protected](condition=_is_win32(), reason="flaky on Windows")+def test_cli_edit_shutdown() -> None:+ port = _get_port()+ p = subprocess.Popen(+ ["marimo", "edit", "-p", str(port), "--headless"],+ stdin=subprocess.PIPE,+ )+ _check_started(port)+ with _patch_signals_win32():+ _interrupt(p)+ assert p.poll() is None+ assert p.stdin is not None+ _confirm_shutdown(p)+ _check_shutdown(p)++[email protected](condition=_is_win32(), reason="flaky on Windows")+def test_cli_run_shutdown() -> None:+ d = tempfile.TemporaryDirectory()+ path = _temp_run_file(d)+ port = _get_port()+ p = subprocess.Popen(+ ["marimo", "run", path, "-p", str(port), "--headless"],+ stdin=subprocess.PIPE,+ )+ _check_started(port)+ with _patch_signals_win32():+ _interrupt(p)+ assert p.poll() is None+ assert p.stdin is not None+ p.stdin.write(b"y\n")+ p.stdin.flush()+ time.sleep(3)+ assert p.poll() == 0