|
| 1 | +import json |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +SRC = str(Path(__file__).resolve().parents[1] / "src") |
| 8 | + |
| 9 | +PROBE = """ |
| 10 | +import json, logging, sys |
| 11 | +{setup} |
| 12 | +root = logging.getLogger() |
| 13 | +before = (root.level, len(root.handlers)) |
| 14 | +
|
| 15 | +from openrouter import OpenRouter |
| 16 | +OpenRouter(api_key="x") |
| 17 | +OpenRouter(api_key="x") # twice: handlers must not stack |
| 18 | +
|
| 19 | +sdk = logging.getLogger("openrouter") |
| 20 | +print(json.dumps({{ |
| 21 | + "root_before": before, |
| 22 | + "root_after": (root.level, len(root.handlers)), |
| 23 | + "sdk_level": sdk.level, |
| 24 | + "sdk_handlers": len(sdk.handlers), |
| 25 | +}})) |
| 26 | +""" |
| 27 | + |
| 28 | + |
| 29 | +def _probe(setup="", **env): |
| 30 | + """Run a fresh interpreter: logging config is process-global.""" |
| 31 | + out = subprocess.run( |
| 32 | + [sys.executable, "-c", PROBE.format(setup=setup)], |
| 33 | + capture_output=True, |
| 34 | + text=True, |
| 35 | + check=True, |
| 36 | + env={**os.environ, "PYTHONPATH": SRC, **env}, |
| 37 | + ).stdout |
| 38 | + return json.loads(out.strip().splitlines()[-1]) |
| 39 | + |
| 40 | + |
| 41 | +def test_debug_mode_leaves_the_root_logger_alone(): |
| 42 | + r = _probe(OPENROUTER_DEBUG="1") |
| 43 | + |
| 44 | + # basicConfig used to set root to DEBUG and attach a StreamHandler to it. |
| 45 | + assert r["root_after"] == r["root_before"] |
| 46 | + assert r["root_after"] == [30, 0] # WARNING, no handlers |
| 47 | + |
| 48 | + # The SDK's own logger is what gets configured. |
| 49 | + assert r["sdk_level"] == 10 # DEBUG |
| 50 | + assert r["sdk_handlers"] == 1 # not 2 — repeated construction must not stack |
| 51 | + |
| 52 | + |
| 53 | +def test_no_logging_is_touched_without_the_env_var(): |
| 54 | + r = _probe() |
| 55 | + |
| 56 | + assert r["root_after"] == r["root_before"] |
| 57 | + assert r["sdk_level"] == 0 # NOTSET |
| 58 | + assert r["sdk_handlers"] == 0 |
| 59 | + |
| 60 | + |
| 61 | +def test_an_application_handler_is_not_duplicated(): |
| 62 | + r = _probe( |
| 63 | + setup="logging.basicConfig(level=logging.INFO)", |
| 64 | + OPENROUTER_DEBUG="1", |
| 65 | + ) |
| 66 | + |
| 67 | + # The app configured root itself; we must not add a competing handler, |
| 68 | + # which would print every record twice. |
| 69 | + assert r["sdk_handlers"] == 0 |
| 70 | + assert r["sdk_level"] == 10 |
| 71 | + assert r["root_after"] == r["root_before"] |
0 commit comments