This commit is contained in:
+11
-24
@@ -49,32 +49,19 @@ def test_custom_log_outputs(levelname, logfunc, message, capsys):
|
||||
|
||||
# Test subprocess: top-level import logs only once
|
||||
|
||||
def test_import_logging_once_per_module():
|
||||
code = textwrap.dedent("""
|
||||
import sys
|
||||
for m in ("math", "io", "random"):
|
||||
sys.modules.pop(m, None)
|
||||
from slic.utils.logcfg import *
|
||||
import math
|
||||
import io
|
||||
import random
|
||||
""")
|
||||
|
||||
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as tmp:
|
||||
tmp.write(code)
|
||||
tmp_path = tmp.name
|
||||
def test_import_logging_once_per_module(caplog):
|
||||
caplog.set_level("INFO")
|
||||
|
||||
env = os.environ.copy()
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||
env["PYTHONPATH"] = root_path + os.pathsep + env.get("PYTHONPATH", "")
|
||||
# Ensure modules are re-imported so the hook sees them
|
||||
for m in ("math", "io", "random"):
|
||||
sys.modules.pop(m, None)
|
||||
|
||||
result = subprocess.run([sys.executable, tmp_path], capture_output=True, text=True, env=env)
|
||||
os.remove(tmp_path)
|
||||
import math
|
||||
import io
|
||||
import random
|
||||
|
||||
assert result.returncode == 0, f"Script failed:\n{result.stderr}"
|
||||
logs = [rec.message for rec in caplog.records]
|
||||
|
||||
output = result.stdout + result.stderr
|
||||
lines = output.splitlines()
|
||||
for mod in ["math", "io", "random"]:
|
||||
count = sum(1 for line in lines if f"importing: {mod}" in line)
|
||||
assert count == 1, f"Expected 1 import log for '{mod}', found {count}"
|
||||
count = sum(1 for msg in logs if f"importing: {mod}" in msg)
|
||||
assert count == 1, f"Expected 1 import log for '{mod}', found {count}\nLogs: {logs}"
|
||||
Reference in New Issue
Block a user