Skip to content

Commit

Permalink
cli debug
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick committed Dec 12, 2024
1 parent f845fc8 commit 0e99bed
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions tests/_cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def _patch_signals_win32() -> Iterator[None]:
signal.signal(signal.SIGINT, old_handler)


def _interrupt(process: subprocess.Popen) -> None:
def _interrupt(process: subprocess.Popen[Any]) -> 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:
def _confirm_shutdown(process: subprocess.Popen[Any]) -> None:
if _is_win32():
process.stdin.write(b"y\r\n")
else:
Expand All @@ -74,7 +74,8 @@ def _confirm_shutdown(process: subprocess.Popen) -> None:


def _check_shutdown(
process: subprocess.Popen, check_fn: Optional[Callable[[int], bool]] = None
process: subprocess.Popen[Any],
check_fn: Optional[Callable[[int], bool]] = None,
) -> None:
max_tries = 3
tries = 0
Expand All @@ -90,24 +91,23 @@ def _check_shutdown(
def _try_fetch(
port: int, host: str = "localhost", token: Optional[str] = None
) -> Optional[bytes]:
contents = None
for _ in range(10):
try:
url = f"http://{host}:{port}"
if token is not None:
url = f"{url}?access_token={token}"
contents = urllib.request.urlopen(url).read()
break
return urllib.request.urlopen(url).read()
except Exception:
time.sleep(0.5)
return contents
print("Failed to fetch contents")
return None


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:
def _temp_run_file(directory: tempfile.TemporaryDirectory[str]) -> str:
filecontents = codegen.generate_filecontents(
["import marimo as mo"], ["one"], cell_configs=[CellConfig()]
)
Expand All @@ -118,7 +118,7 @@ def _temp_run_file(directory: tempfile.TemporaryDirectory) -> str:


def _check_contents(
p: subprocess.Popen, # type: ignore
p: subprocess.Popen[Any], # type: ignore
phrase: bytes,
contents: Optional[bytes],
) -> None:
Expand All @@ -142,7 +142,7 @@ def _get_port() -> int:
raise OSError("Could not find an unused port.")


def _read_toml(filepath: str) -> Optional[dict]:
def _read_toml(filepath: str) -> Optional[dict[str, Any]]:
import tomlkit

if not os.path.exists(filepath):
Expand Down Expand Up @@ -191,19 +191,15 @@ def __(mo):
def test_cli_help_exit_code() -> None:
# smoke test: makes sure CLI starts
# helpful for catching issues related to
# Python 3.8 compatibility, such as forgetting `from __future__` import
# annotations
p = subprocess.run(["marimo", "--help"])
assert p.returncode == 0


def test_cli_edit_none() -> None:
# smoke test: makes sure CLI starts and has basic things we expect
# helpful for catching issues related to
# Python 3.8 compatibility, such as forgetting `from __future__` import
# annotations
port = _get_port()
p = subprocess.Popen(
with subprocess.Popen(
[
"marimo",
"edit",
Expand All @@ -213,20 +209,19 @@ def test_cli_edit_none() -> None:
"--no-token",
"--skip-update-check",
]
)
contents = _try_fetch(port)
_check_contents(p, b"marimo-mode data-mode='home'", contents)
_check_contents(
p, f"marimo-version data-version='{__version__}'".encode(), contents
)
_check_contents(p, b"marimo-server-token", contents)
):
contents = _try_fetch(port)
assert contents is not None
assert b"marimo-mode data-mode='home'" in contents
assert (
f"marimo-version data-version='{__version__}'".encode() in contents
)
assert b"marimo-server-token" in contents


def test_cli_edit_token() -> None:
# smoke test: makes sure CLI starts and has basic things we expect
# helpful for catching issues related to
# Python 3.8 compatibility, such as forgetting `from __future__` import
# annotations
port = _get_port()
p = subprocess.Popen(
[
Expand Down

0 comments on commit 0e99bed

Please sign in to comment.