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.
This commit is contained in:
truetype committed 2026-02-06 11:22:57 +01:00
1 parent 572d551a2c
commit b2ee220783
4 files changed
+29 -8

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Application package for the dashboard service."""
+13 -6
View File
@@ -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()
+3
View File
@@ -0,0 +1,3 @@
[pytest]
pythonpath = .
testpaths = tests
+12 -2
View File
@@ -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