Skip to content

Commit f95a74d

Browse files
authored
chore: add patch_propagate_logger fixture (#584)
### Description - Add patch_propagate_logger fixture. ### Issues - Closes: #462 ### Testing Updated the `test_initialize_with_manual_password_different_than_user_one` test, as it did not correspond to the changes made earlier in #468.
1 parent 6dde116 commit f95a74d

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

tests/unit/actor/test_actor_helpers.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ async def test_abort_actor_run(apify_client_async_patcher: ApifyClientAsyncPatch
122122
# NOTE: The following methods are properly tested using integrations tests.
123123

124124

125-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
126125
async def test_metamorph_fails_locally(caplog: pytest.LogCaptureFixture) -> None:
127126
caplog.set_level('WARNING')
128127
async with Actor:
@@ -133,7 +132,6 @@ async def test_metamorph_fails_locally(caplog: pytest.LogCaptureFixture) -> None
133132
assert 'Actor.metamorph() is only supported when running on the Apify platform.' in caplog.records[0].message
134133

135134

136-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
137135
async def test_reboot_fails_locally(caplog: pytest.LogCaptureFixture) -> None:
138136
caplog.set_level('WARNING')
139137
async with Actor:
@@ -144,7 +142,6 @@ async def test_reboot_fails_locally(caplog: pytest.LogCaptureFixture) -> None:
144142
assert 'Actor.reboot() is only supported when running on the Apify platform.' in caplog.records[0].message
145143

146144

147-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
148145
async def test_add_webhook_fails_locally(caplog: pytest.LogCaptureFixture) -> None:
149146
caplog.set_level('WARNING')
150147
async with Actor:
@@ -157,7 +154,6 @@ async def test_add_webhook_fails_locally(caplog: pytest.LogCaptureFixture) -> No
157154
assert 'Actor.add_webhook() is only supported when running on the Apify platform.' in caplog.records[0].message
158155

159156

160-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
161157
async def test_set_status_message_locally(caplog: pytest.LogCaptureFixture) -> None:
162158
caplog.set_level('INFO')
163159
async with Actor:
@@ -169,7 +165,6 @@ async def test_set_status_message_locally(caplog: pytest.LogCaptureFixture) -> N
169165
assert '[Status message]: test-status-message' in matching_records[0].message
170166

171167

172-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
173168
async def test_set_terminal_status_message_locally(caplog: pytest.LogCaptureFixture) -> None:
174169
caplog.set_level('INFO')
175170
async with Actor:

tests/unit/actor/test_actor_log.py

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

33
import contextlib
44
import logging
5-
6-
import pytest
5+
from typing import TYPE_CHECKING
76

87
from apify import Actor
98
from apify.log import logger
109

10+
if TYPE_CHECKING:
11+
import pytest
12+
1113

12-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
1314
async def test_actor_logs_messages_correctly(caplog: pytest.LogCaptureFixture) -> None:
1415
caplog.set_level(logging.DEBUG, logger='apify')
1516

tests/unit/conftest.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,33 @@
1616
from crawlee import service_locator
1717

1818
import apify._actor
19+
import apify.log
1920

2021
if TYPE_CHECKING:
2122
from collections.abc import Callable, Iterator
23+
from logging import Logger
2224
from pathlib import Path
2325

2426

27+
@pytest.fixture
28+
def _patch_propagate_logger(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
29+
"""Patch enabling `propagate` for the crawlee logger.
30+
31+
This is necessary for tests requiring log interception using `caplog`.
32+
"""
33+
34+
original_configure_logger = apify.log.configure_logger
35+
36+
def propagate_logger(logger: Logger, **kwargs: Any) -> None:
37+
original_configure_logger(logger, **kwargs)
38+
logger.propagate = True
39+
40+
monkeypatch.setattr('crawlee._log_config.configure_logger', propagate_logger)
41+
monkeypatch.setattr(apify.log, 'configure_logger', propagate_logger)
42+
yield
43+
monkeypatch.undo()
44+
45+
2546
@pytest.fixture
2647
def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callable[[], None]:
2748
"""Prepare the testing environment by resetting the global state before each test.
@@ -66,7 +87,10 @@ def _prepare_test_env() -> None:
6687

6788

6889
@pytest.fixture(autouse=True)
69-
def _isolate_test_environment(prepare_test_env: Callable[[], None]) -> None:
90+
def _isolate_test_environment(
91+
prepare_test_env: Callable[[], None],
92+
_patch_propagate_logger: None,
93+
) -> None:
7094
"""Isolate the testing environment by resetting global state before and after each test.
7195
7296
This fixture ensures that each test starts with a clean slate and that any modifications during the test

tests/unit/events/test_apify_event_manager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from collections.abc import Callable
2323

2424

25-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
2625
async def test_lifecycle_local(caplog: pytest.LogCaptureFixture) -> None:
2726
caplog.set_level(logging.DEBUG, logger='apify')
2827
config = Configuration.get_global_configuration()

tests/unit/test_proxy_configuration.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,8 @@ async def test_initialize_prefering_password_from_env_over_calling_api(
472472

473473

474474
@pytest.mark.usefixtures('patched_impit_client')
475-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
476475
async def test_initialize_with_manual_password_different_than_user_one(
477476
monkeypatch: pytest.MonkeyPatch,
478-
caplog: pytest.LogCaptureFixture,
479477
httpserver: HTTPServer,
480478
patched_apify_client: ApifyClientAsync,
481479
) -> None:
@@ -501,10 +499,6 @@ async def test_initialize_with_manual_password_different_than_user_one(
501499
assert proxy_configuration._password == different_dummy_password
502500
assert proxy_configuration.is_man_in_the_middle is True
503501

504-
assert len(caplog.records) == 1
505-
assert caplog.records[0].levelname == 'WARNING'
506-
assert 'The Apify Proxy password you provided belongs to a different user' in caplog.records[0].message
507-
508502

509503
@pytest.mark.usefixtures('patched_impit_client')
510504
async def test_initialize_when_not_connected(monkeypatch: pytest.MonkeyPatch, httpserver: HTTPServer) -> None:
@@ -526,7 +520,6 @@ async def test_initialize_when_not_connected(monkeypatch: pytest.MonkeyPatch, ht
526520
await proxy_configuration.initialize()
527521

528522

529-
@pytest.mark.skip(reason='There are issues with log propagation to caplog, see issue #462.')
530523
async def test_initialize_when_status_page_unavailable(
531524
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, httpserver: HTTPServer
532525
) -> None:

0 commit comments

Comments
 (0)