Skip to content

Commit aca10f2

Browse files
authored
Drop pretend dependency (hynek#766)
1 parent 3800d40 commit aca10f2

File tree

11 files changed

+69
-42
lines changed

11 files changed

+69
-42
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Twitter = "https://twitter.com/hynek"
4242
[dependency-groups]
4343
tests = [
4444
"time-machine>=2.14.1",
45-
"pretend",
4645
"pytest-asyncio>=0.17",
4746
"pytest>=6.0",
4847
"simplejson",

tests/helpers.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-License-Identifier: MIT OR Apache-2.0
2+
# This file is dual licensed under the terms of the Apache License, Version
3+
# 2.0, and the MIT License. See the LICENSE file in the root of this
4+
# repository for complete details.
5+
6+
"""
7+
Shared test utilities.
8+
"""
9+
10+
from unittest.mock import Mock, seal
11+
12+
from structlog._log_levels import NAME_TO_LEVEL
13+
14+
15+
stdlib_log_methods = [m for m in NAME_TO_LEVEL if m != "notset"]
16+
17+
18+
class CustomError(Exception):
19+
"""
20+
Custom exception for testing purposes.
21+
"""
22+
23+
24+
def stub(**kwargs):
25+
"""
26+
Create a restrictive mock that prevents new attributes after creation.
27+
Similar to pretend.stub().
28+
"""
29+
m = Mock(**kwargs)
30+
seal(m)
31+
32+
return m
33+
34+
35+
def raiser(exception):
36+
"""
37+
Create a mock that raises the given exception when called.
38+
"""
39+
return Mock(side_effect=exception)
40+
41+
42+
def call_recorder(func):
43+
"""
44+
Create a mock that records calls while executing func.
45+
"""
46+
return Mock(side_effect=func)

tests/processors/test_renderers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525
from structlog.threadlocal import wrap_dict
2626

27-
from ..utils import CustomError
27+
from ..helpers import CustomError
2828

2929

3030
try:

tests/test_base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55

66
import pytest
77

8-
from pretend import raiser, stub
9-
108
from structlog import get_context
119
from structlog._base import BoundLoggerBase
1210
from structlog._config import _CONFIG
1311
from structlog.exceptions import DropEvent
1412
from structlog.processors import KeyValueRenderer
1513
from structlog.testing import ReturnLogger
16-
from tests.utils import CustomError
14+
15+
from .helpers import CustomError, raiser, stub
1716

1817

1918
def build_bl(logger=None, processors=None, context=None):

tests/test_config.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import pickle
88
import warnings
99

10-
import pytest
10+
from unittest.mock import call
1111

12-
from pretend import call, call_recorder, stub
12+
import pytest
1313

1414
import structlog
1515

@@ -28,6 +28,8 @@
2828
)
2929
from structlog.typing import BindableLogger
3030

31+
from .helpers import call_recorder, stub
32+
3133

3234
@pytest.fixture(name="proxy")
3335
def _proxy():
@@ -463,4 +465,4 @@ def test_get_logger_passes_positional_arguments_to_logger_factory(self):
463465

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

466-
assert [call("test")] == factory.calls
468+
assert [call("test")] == factory.call_args_list

tests/test_frames.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
import pytest
99

10-
from pretend import stub
11-
1210
from structlog._frames import (
1311
_find_first_app_frame_and_name,
1412
_format_exception,
1513
_format_stack,
1614
)
1715

16+
from .helpers import stub
17+
1818

1919
class TestFindFirstAppFrameAndName:
2020
def test_ignores_structlog_by_default(self):

tests/test_output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
from structlog._output import WRITE_LOCKS, stderr, stdout
2222

23-
from .utils import stdlib_log_methods
23+
from .helpers import stdlib_log_methods
2424

2525

2626
class TestLoggers:

tests/test_stdlib.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import pytest
1919
import pytest_asyncio
2020

21-
from pretend import call_recorder, stub
22-
2321
from structlog import (
2422
PrintLogger,
2523
ReturnLogger,
@@ -53,6 +51,7 @@
5351
from structlog.typing import BindableLogger, EventDict
5452

5553
from .additional_frame import additional_frame
54+
from .helpers import call_recorder, stub
5655

5756

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

1103-
event_dict = test_processor.calls[0].args[2]
1102+
event_dict = test_processor.call_args_list[0].args[2]
11041103

11051104
assert "positional_args" in event_dict
11061105
assert positional_args == event_dict["positional_args"]
@@ -1181,7 +1180,7 @@ def test_foreign_pre_chain_gets_exc_info(self):
11811180
except Exception:
11821181
logging.getLogger().exception("okay")
11831182

1184-
event_dict = test_processor.calls[0].args[2]
1183+
event_dict = test_processor.call_args_list[0].args[2]
11851184

11861185
assert "exc_info" in event_dict
11871186
assert isinstance(event_dict["exc_info"], tuple)
@@ -1209,7 +1208,7 @@ def add_excinfo(logger, log_method, event_dict):
12091208
except Exception:
12101209
logging.getLogger().error("okay")
12111210

1212-
event_dict = test_processor.calls[0].args[2]
1211+
event_dict = test_processor.call_args_list[0].args[2]
12131212

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

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

12331232
logger.info("meh")
12341233

1235-
assert 1 == len(handler2.handle.calls)
1234+
assert 1 == len(handler2.handle.call_args_list)
12361235

1237-
handler2_record = handler2.handle.calls[0].args[0]
1236+
handler2_record = handler2.handle.call_args_list[0].args[0]
12381237

12391238
assert "meh" == handler2_record.msg
12401239

tests/test_threadlocal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
unbind_threadlocal,
2929
wrap_dict,
3030
)
31-
from tests.utils import CustomError
31+
32+
from .helpers import CustomError
3233

3334

3435
try:

tests/test_twisted.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
import pytest
99

10-
from pretend import call_recorder
11-
1210
from structlog import ReturnLogger
1311
from structlog._config import _CONFIG
1412
from structlog.processors import KeyValueRenderer
1513

14+
from .helpers import call_recorder
15+
1616

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

330-
assert 1 == len(o.calls)
330+
assert 1 == len(o.call_args_list)
331331

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

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

0 commit comments

Comments
 (0)