Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ repos:
- id: codespell
args: [-L, alog, -L, abl]

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.24.1
hooks:
- id: validate-pyproject
# Optional extra validations from SchemaStore:
additional_dependencies: ["validate-pyproject-schema-store[all]"]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Twitter = "https://twitter.com/hynek"
[dependency-groups]
tests = [
"time-machine>=2.14.1",
"pretend",
"pytest-asyncio>=0.17",
"pytest>=6.0",
"simplejson",
Expand Down
46 changes: 46 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-License-Identifier: MIT OR Apache-2.0
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the MIT License. See the LICENSE file in the root of this
# repository for complete details.

"""
Shared test utilities.
"""

from unittest.mock import Mock, seal

from structlog._log_levels import NAME_TO_LEVEL


stdlib_log_methods = [m for m in NAME_TO_LEVEL if m != "notset"]


class CustomError(Exception):
"""
Custom exception for testing purposes.
"""


def stub(**kwargs):
"""
Create a restrictive mock that prevents new attributes after creation.
Similar to pretend.stub().
"""
m = Mock(**kwargs)
seal(m)

return m


def raiser(exception):
"""
Create a mock that raises the given exception when called.
"""
return Mock(side_effect=exception)


def call_recorder(func):
"""
Create a mock that records calls while executing func.
"""
return Mock(side_effect=func)
2 changes: 1 addition & 1 deletion tests/processors/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from structlog.threadlocal import wrap_dict

from ..utils import CustomError
from ..helpers import CustomError


try:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

import pytest

from pretend import raiser, stub

from structlog import get_context
from structlog._base import BoundLoggerBase
from structlog._config import _CONFIG
from structlog.exceptions import DropEvent
from structlog.processors import KeyValueRenderer
from structlog.testing import ReturnLogger
from tests.utils import CustomError

from .helpers import CustomError, raiser, stub


def build_bl(logger=None, processors=None, context=None):
Expand Down
8 changes: 5 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import pickle
import warnings

import pytest
from unittest.mock import call

from pretend import call, call_recorder, stub
import pytest

import structlog

Expand All @@ -28,6 +28,8 @@
)
from structlog.typing import BindableLogger

from .helpers import call_recorder, stub


@pytest.fixture(name="proxy")
def _proxy():
Expand Down Expand Up @@ -463,4 +465,4 @@ def test_get_logger_passes_positional_arguments_to_logger_factory(self):

get_logger("test").bind(x=42)

assert [call("test")] == factory.calls
assert [call("test")] == factory.call_args_list
4 changes: 2 additions & 2 deletions tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import pytest

from pretend import stub

from structlog._frames import (
_find_first_app_frame_and_name,
_format_exception,
_format_stack,
)

from .helpers import stub


class TestFindFirstAppFrameAndName:
def test_ignores_structlog_by_default(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from structlog._output import WRITE_LOCKS, stderr, stdout

from .utils import stdlib_log_methods
from .helpers import stdlib_log_methods


class TestLoggers:
Expand Down
13 changes: 6 additions & 7 deletions tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import pytest
import pytest_asyncio

from pretend import call_recorder, stub

from structlog import (
PrintLogger,
ReturnLogger,
Expand Down Expand Up @@ -53,6 +51,7 @@
from structlog.typing import BindableLogger, EventDict

from .additional_frame import additional_frame
from .helpers import call_recorder, stub


def build_bl(logger=None, processors=None, context=None):
Expand Down Expand Up @@ -1100,7 +1099,7 @@ def test_pass_foreign_args_true_sets_positional_args_key(self):
positional_args = {"foo": "bar"}
logging.getLogger().info("okay %(foo)s", positional_args)

event_dict = test_processor.calls[0].args[2]
event_dict = test_processor.call_args_list[0].args[2]

assert "positional_args" in event_dict
assert positional_args == event_dict["positional_args"]
Expand Down Expand Up @@ -1181,7 +1180,7 @@ def test_foreign_pre_chain_gets_exc_info(self):
except Exception:
logging.getLogger().exception("okay")

event_dict = test_processor.calls[0].args[2]
event_dict = test_processor.call_args_list[0].args[2]

assert "exc_info" in event_dict
assert isinstance(event_dict["exc_info"], tuple)
Expand Down Expand Up @@ -1209,7 +1208,7 @@ def add_excinfo(logger, log_method, event_dict):
except Exception:
logging.getLogger().error("okay")

event_dict = test_processor.calls[0].args[2]
event_dict = test_processor.call_args_list[0].args[2]

assert MyError is event_dict["exc_info"][0]

Expand All @@ -1232,9 +1231,9 @@ def test_other_handlers_get_original_record(self):

logger.info("meh")

assert 1 == len(handler2.handle.calls)
assert 1 == len(handler2.handle.call_args_list)

handler2_record = handler2.handle.calls[0].args[0]
handler2_record = handler2.handle.call_args_list[0].args[0]

assert "meh" == handler2_record.msg

Expand Down
3 changes: 2 additions & 1 deletion tests/test_threadlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
unbind_threadlocal,
wrap_dict,
)
from tests.utils import CustomError

from .helpers import CustomError


try:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import pytest

from pretend import call_recorder

from structlog import ReturnLogger
from structlog._config import _CONFIG
from structlog.processors import KeyValueRenderer

from .helpers import call_recorder


try:
from twisted.python.failure import Failure
Expand Down Expand Up @@ -327,7 +327,7 @@ def test_callsWrappedObserver(self):
o = call_recorder(lambda *a, **kw: None)
JSONLogObserverWrapper(o)({"message": ("hello",)})

assert 1 == len(o.calls)
assert 1 == len(o.call_args_list)

def test_jsonifiesPlainLogEntries(self):
"""
Expand All @@ -336,7 +336,7 @@ def test_jsonifiesPlainLogEntries(self):
"""
o = call_recorder(lambda *a, **kw: None)
JSONLogObserverWrapper(o)({"message": ("hello",), "system": "-"})
msg = json.loads(o.calls[0].args[0]["message"][0])
msg = json.loads(o.call_args_list[0].args[0]["message"][0])

assert msg == {"event": "hello", "system": "-"}

Expand Down
19 changes: 0 additions & 19 deletions tests/utils.py

This file was deleted.

Loading