From b2ee2207831a40bfb3fc4791225aca3dfc410b9a Mon Sep 17 00:00:00 2001 From: truetype Date: Fri, 6 Feb 2026 11:22:57 +0100 Subject: [PATCH] fix(project): stabilize python3 tests and app shutdown Use python3/venv consistently for local test execution and make package discovery deterministic for pytest. Also migrate FastAPI shutdown handling to lifespan to remove deprecation warnings while preserving cleanup behavior. --- app/__init__.py | 1 + app/main.py | 19 +++++++++++++------ pytest.ini | 3 +++ scripts/auto-test.sh | 14 ++++++++++++-- 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 app/__init__.py create mode 100644 pytest.ini diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..2f62bb5 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1 @@ +"""Application package for the dashboard service.""" diff --git a/app/main.py b/app/main.py index 04f5c9f..8dedc16 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,7 @@ from __future__ import annotations import dataclasses +from contextlib import asynccontextmanager from fastapi import FastAPI, HTTPException, Request from fastapi.responses import HTMLResponse @@ -13,12 +14,23 @@ from app.services import PortScanner from app.storage import Storage from app.tunnel_manager import TunnelManager -app = FastAPI(title="Gradio Tunnel Dashboard") policy = TunnelPolicy.load() storage = Storage.from_env() scanner = PortScanner(policy=policy, storage=storage) manager = TunnelManager(storage=storage) + +@asynccontextmanager +async def lifespan(_: FastAPI): + try: + yield + finally: + manager.shutdown() + storage.close() + + +app = FastAPI(title="Gradio Tunnel Dashboard", lifespan=lifespan) + app.mount("/static", StaticFiles(directory="app/static"), name="static") templates = Jinja2Templates(directory="app/templates") @@ -99,8 +111,3 @@ async def stop_tunnel(port: int) -> dict[str, bool]: raise HTTPException(status_code=404, detail="Tunnel not running.") return {"stopped": True} - -@app.on_event("shutdown") -def shutdown_event() -> None: - manager.shutdown() - storage.close() diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..c7b23ec --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +pythonpath = . +testpaths = tests diff --git a/scripts/auto-test.sh b/scripts/auto-test.sh index 5bb49ac..fb93df7 100755 --- a/scripts/auto-test.sh +++ b/scripts/auto-test.sh @@ -3,8 +3,18 @@ set -euo pipefail cd "$(dirname "$0")/.." +if ! command -v python3 >/dev/null 2>&1; then + echo "python3 non trovato nel PATH." >&2 + exit 1 +fi + +if [[ ! -x ".venv/bin/python3" ]]; then + echo "Creating virtual environment..." + python3 -m venv .venv +fi + echo "Installing dependencies..." -python -m pip install -r requirements.txt >/dev/null +.venv/bin/python3 -m pip install -r requirements.txt >/dev/null echo "Running pytest..." -python -m pytest tests +PYTHONPATH=. .venv/bin/python3 -m pytest tests