Skip to content

Commit

Permalink
Implement test logging, from instrumentation-1 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
pepoluan committed Jan 17, 2023
1 parent 9b5bf7f commit 1dd5276
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ prof/
~temp*
*.sw[a-p]
pyvenv.cfg
aiosmtpd_test.log
44 changes: 44 additions & 0 deletions aiosmtpd/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

import asyncio
import inspect
import logging
import os
import socket
import ssl
import warnings
from contextlib import suppress
from functools import wraps
from pathlib import Path
from smtplib import SMTP as SMTPClient
from typing import Any, Callable, Generator, NamedTuple, Optional, Type, TypeVar

Expand Down Expand Up @@ -37,6 +40,39 @@
]


# region #### Instrumentation (Logging) ###############################################

if os.environ.get("AIOSMTPD_TEST_LOG") != "1":
log_handler = logging.NullHandler()
else:
log_path = Path(".").expanduser().absolute()
while not (log_path / "pyproject.toml").exists():
log_path = log_path.parent
log_handler = logging.FileHandler(log_path / "aiosmtpd_test.log")
log_handler.setFormatter(
logging.Formatter("{name} {levelname} {message}", style="{")
)
log_level = int(os.environ.get("AIOSMTPD_TEST_LOGLEVEL", logging.DEBUG))
log_handler.setLevel(log_level)
# Attach to root logger
logging.getLogger().addHandler(log_handler)

for logname in (
"aiosmtpd",
"aiosmtpd.controller",
"aiosmtpd.tests",
"mail",
"mail.log",
"mail.debug",
):
_logger = logging.getLogger(logname)
_logger.propagate = True
_logger.setLevel(log_level)

log = logging.getLogger("aiosmtpd.tests")

# endregion

# region #### Aliases #################################################################

controller_data = pytest.mark.controller_data
Expand Down Expand Up @@ -99,6 +135,14 @@ def cache_fqdn(session_mocker: MockFixture):
# region #### Common Fixtures #########################################################


@pytest.fixture(autouse=True)
def log_case(request: pytest.FixtureRequest):
node_id = request.node.nodeid
log.debug("Entering %s", node_id)
yield
log.debug("Exiting %s", node_id)


@pytest.fixture
def get_controller(request: pytest.FixtureRequest) -> Callable[..., Controller]:
"""
Expand Down

0 comments on commit 1dd5276

Please sign in to comment.