This commit is contained in:
+18
-38
@@ -1,46 +1,38 @@
|
||||
import logging
|
||||
import logzero
|
||||
from logzero import logger as log
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
import importlib
|
||||
import subprocess
|
||||
import textwrap
|
||||
import tempfile
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
from slic.utils.logcfg import *
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
import textwrap
|
||||
from logzero import logger as log
|
||||
from slic.utils.logcfg import add_log_Level, logcfg, setup_import_logging
|
||||
|
||||
# Register custom log levels once for the test module
|
||||
# Set for all the tests
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def setup_custom_levels():
|
||||
# Add custom levels
|
||||
add_log_Level(log, "LONG", logging.INFO + 5, func_name="long")
|
||||
add_log_Level(log, "ENLARGE", logging.INFO + 6, func_name="enlarge")
|
||||
|
||||
logcfg("LONG") # Enable all levels >= LONG
|
||||
logcfg("LONG")
|
||||
setup_import_logging()
|
||||
|
||||
# Test simple custom log
|
||||
|
||||
# Test: add_log_Level()
|
||||
@pytest.mark.parametrize("levelname, logfunc, message", [
|
||||
("LONG", lambda msg: log.long(msg), "This is a LONG message"),
|
||||
("ENLARGE", lambda msg: log.enlarge(msg), "Please ENLARGE this!"),
|
||||
])
|
||||
def test_custom_levels_are_logged(levelname, logfunc, message, capsys):
|
||||
# Call the custom log function
|
||||
logzero.setup_default_logger()
|
||||
def test_custom_log_outputs(levelname, logfunc, message, capsys):
|
||||
logfunc(message)
|
||||
|
||||
# Capture stderr where logzero outputs messages
|
||||
captured = capsys.readouterr().err
|
||||
assert message in captured, f"Expected message '{message}' not found in log"
|
||||
assert levelname in captured, f"Expected level '{levelname}' not present in log"
|
||||
|
||||
# Assert color, message, and ANSI reset code are present
|
||||
assert message in captured, f"Expected log message '{message}' not found in output"
|
||||
assert captured.strip().endswith(ANSI_RESET), "Missing ANSI reset code at end of log line"
|
||||
|
||||
# Test: setup_import_logging()
|
||||
# Test subprocess: top-level import logs only once
|
||||
|
||||
def test_import_logging_once_per_module():
|
||||
# Script to run in isolated subprocess
|
||||
code = textwrap.dedent("""
|
||||
import sys
|
||||
from slic.utils.logcfg import setup_import_logging, add_log_Level, logcfg
|
||||
@@ -57,37 +49,25 @@ def test_import_logging_once_per_module():
|
||||
import random
|
||||
""")
|
||||
|
||||
# Write the script to a temp file
|
||||
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as tmp:
|
||||
tmp.write(code)
|
||||
tmp_path = tmp.name
|
||||
|
||||
# Define the environment with correct PYTHONPATH
|
||||
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", "")
|
||||
|
||||
# Run the script
|
||||
result = subprocess.run([sys.executable, tmp_path], capture_output=True, text=True, env=env)
|
||||
|
||||
# Clean up temp file
|
||||
os.remove(tmp_path)
|
||||
|
||||
# Check for errors
|
||||
assert result.returncode == 0, f"Subprocess failed:\n{result.stderr}"
|
||||
assert result.returncode == 0, f"Script failed:\n{result.stderr}"
|
||||
|
||||
stderr = result.stderr
|
||||
|
||||
# Assert that each top-level import is logged only once
|
||||
#for mod in ["json", "math", "io", "random"]:
|
||||
# assert stderr.count(f"importing: {mod}") == 1, f"Module '{mod}' logged multiple times or missing"
|
||||
|
||||
top_level_lines = [
|
||||
line for line in stderr.splitlines()
|
||||
if "importing:" in line and "logcfg.py" not in line and "site-packages" not in line
|
||||
if "importing:" in line and "logcfg" not in line and "site-packages" not in line
|
||||
]
|
||||
|
||||
for mod in ["json", "math", "io", "random"]:
|
||||
count = sum(1 for line in top_level_lines if f"importing: {mod}" in line)
|
||||
assert count == 1, f"Top-level import of '{mod}' logged {count} times"
|
||||
|
||||
assert count == 1, f"Expected 1 import log for '{mod}', found {count}"
|
||||
|
||||
Reference in New Issue
Block a user