from __future__ import annotations import os import stat import subprocess from pathlib import Path def _run_start_server( tmp_path: Path, *, reload_enabled: bool, ) -> subprocess.CompletedProcess[str]: fake_python = tmp_path / "fake-python.sh" fake_python.write_text( """#!/usr/bin/env sh printf 'ARG:%s\n' "$@" """, encoding="utf-8", ) fake_python.chmod(fake_python.stat().st_mode | stat.S_IEXEC) script_path = Path(__file__).resolve().parents[1] / "server_start.sh" script_prefix = script_path.read_text(encoding="utf-8").split('case "$MODE" in', 1)[0] command = f"""{script_prefix} PYTHON_BIN="{fake_python}" SERVER_HOST="127.0.0.1" SERVER_PORT="8765" SERVER_RELOAD="{'true' if reload_enabled else 'false'}" SERVER_WORKERS="1" start_server """ return subprocess.run( ["bash", "-c", command], capture_output=True, text=True, env={**os.environ, "MODE": "test"}, cwd=script_path.parent, check=False, ) def test_dependencies_ready_fails_when_jwt_is_missing(tmp_path: Path) -> None: fake_python = tmp_path / "fake-python.sh" fake_python.write_text( """#!/usr/bin/env bash if [ "$1" = "-c" ]; then case "$2" in *jwt*) exit 1 ;; *) exit 0 ;; esac fi exit 0 """, encoding="utf-8", ) fake_python.chmod(fake_python.stat().st_mode | stat.S_IEXEC) script_path = Path(__file__).resolve().parents[1] / "server_start.sh" script_prefix = script_path.read_text(encoding="utf-8").split('case "$MODE" in', 1)[0] command = f"""{script_prefix} PYTHON_BIN="{fake_python}" dependencies_ready """ result = subprocess.run( ["bash", "-c", command], capture_output=True, text=True, env={**os.environ, "MODE": "test"}, cwd=script_path.parent, check=False, ) assert result.returncode != 0 def test_reload_watches_only_server_source_directory(tmp_path: Path) -> None: result = _run_start_server(tmp_path, reload_enabled=True) assert result.returncode == 0, result.stderr assert "ARG:--reload" in result.stdout assert "ARG:--reload-dir\nARG:src" in result.stdout assert "ARG:--app-dir\nARG:src" in result.stdout def test_non_reload_server_command_remains_unchanged(tmp_path: Path) -> None: result = _run_start_server(tmp_path, reload_enabled=False) assert result.returncode == 0, result.stderr assert "ARG:app.main:app" in result.stdout assert "ARG:--host\nARG:127.0.0.1" in result.stdout assert "ARG:--port\nARG:8765" in result.stdout assert "ARG:--reload" not in result.stdout assert "ARG:--reload-dir" not in result.stdout