Skip to content

Commit 5ece3e7

Browse files
authored
chore: remove the logic of Actor._get_default_exit_process for pytest from production code (#654)
### Issues Closes: #641
1 parent 0e03e0e commit 5ece3e7

File tree

2 files changed

+70
-81
lines changed

2 files changed

+70
-81
lines changed

src/apify/_actor.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import asyncio
4-
import os
54
import sys
65
from contextlib import suppress
76
from datetime import datetime, timedelta, timezone
@@ -1288,16 +1287,11 @@ def _raise_if_not_initialized(self) -> None:
12881287
raise RuntimeError('The Actor was not initialized!')
12891288

12901289
def _get_default_exit_process(self) -> bool:
1291-
"""Return False for IPython, Pytest, and Scrapy environments, True otherwise."""
1290+
"""Return False for IPython and Scrapy environments, True otherwise."""
12921291
if is_running_in_ipython():
12931292
self.log.debug('Running in IPython, setting default `exit_process` to False.')
12941293
return False
12951294

1296-
# Check if running in Pytest by detecting the relevant environment variable.
1297-
if os.getenv('PYTEST_CURRENT_TEST'):
1298-
self.log.debug('Running in Pytest, setting default `exit_process` to False.')
1299-
return False
1300-
13011295
# Check if running in Scrapy by attempting to import it.
13021296
with suppress(ImportError):
13031297
import scrapy # noqa: F401 PLC0415

tests/unit/actor/test_actor_log.py

Lines changed: 69 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async def test_actor_logs_messages_correctly(caplog: pytest.LogCaptureFixture) -
1515
caplog.set_level(logging.DEBUG, logger='apify')
1616

1717
with contextlib.suppress(RuntimeError):
18-
async with Actor(configure_logging=False):
18+
async with Actor(configure_logging=False, exit_process=False):
1919
# Test Actor.log
2020
Actor.log.debug('Debug message')
2121
Actor.log.info('Info message')
@@ -36,77 +36,72 @@ async def test_actor_logs_messages_correctly(caplog: pytest.LogCaptureFixture) -
3636
# Test that exception in Actor.main is logged with the traceback
3737
raise RuntimeError('Dummy RuntimeError')
3838

39+
# We skip the first entry, as it is related to the initialization of `lazy_object_proxy.Proxy` for `Actor`.
40+
records = caplog.records[1:]
41+
3942
# Updated expected number of log records (additional debug messages added)
40-
assert len(caplog.records) == 16
41-
42-
# Record 0: First Pytest context log
43-
assert caplog.records[0].levelno == logging.DEBUG
44-
assert caplog.records[0].message.startswith('Running in Pytest')
45-
46-
# Record 1: Duplicate Pytest context log
47-
assert caplog.records[1].levelno == logging.DEBUG
48-
assert caplog.records[1].message.startswith('Running in Pytest')
49-
50-
# Record 2: Logging configured
51-
assert caplog.records[2].levelno == logging.DEBUG
52-
assert caplog.records[2].message == 'Logging configured'
53-
54-
# Record 3: Initializing Actor
55-
assert caplog.records[3].levelno == logging.INFO
56-
assert caplog.records[3].message == 'Initializing Actor'
57-
58-
# Record 4: Configuration initialized
59-
assert caplog.records[4].levelno == logging.DEBUG
60-
assert caplog.records[4].message == 'Configuration initialized'
61-
62-
# Record 5: Storage client initialized
63-
assert caplog.records[5].levelno == logging.DEBUG
64-
assert caplog.records[5].message == 'Storage client initialized'
65-
66-
# Record 6: Event manager initialized
67-
assert caplog.records[6].levelno == logging.DEBUG
68-
assert caplog.records[6].message == 'Event manager initialized'
69-
70-
# Record 7: Charging manager initialized
71-
assert caplog.records[7].levelno == logging.DEBUG
72-
assert caplog.records[7].message == 'Charging manager initialized'
73-
74-
# Record 8: Debug message
75-
assert caplog.records[8].levelno == logging.DEBUG
76-
assert caplog.records[8].message == 'Debug message'
77-
78-
# Record 9: Info message
79-
assert caplog.records[9].levelno == logging.INFO
80-
assert caplog.records[9].message == 'Info message'
81-
82-
# Record 10: Warning message
83-
assert caplog.records[10].levelno == logging.WARNING
84-
assert caplog.records[10].message == 'Warning message'
85-
86-
# Record 11: Error message
87-
assert caplog.records[11].levelno == logging.ERROR
88-
assert caplog.records[11].message == 'Error message'
89-
90-
# Record 12: Exception message with traceback (ValueError)
91-
assert caplog.records[12].levelno == logging.ERROR
92-
assert caplog.records[12].message == 'Exception message'
93-
assert caplog.records[12].exc_info is not None
94-
assert caplog.records[12].exc_info[0] is ValueError
95-
assert isinstance(caplog.records[12].exc_info[1], ValueError)
96-
assert str(caplog.records[12].exc_info[1]) == 'Dummy ValueError'
97-
98-
# Record 13: Multiline log message
99-
assert caplog.records[13].levelno == logging.INFO
100-
assert caplog.records[13].message == 'Multi\nline\nlog\nmessage'
101-
102-
# Record 14: Actor failed with an exception (RuntimeError)
103-
assert caplog.records[14].levelno == logging.ERROR
104-
assert caplog.records[14].message == 'Actor failed with an exception'
105-
assert caplog.records[14].exc_info is not None
106-
assert caplog.records[14].exc_info[0] is RuntimeError
107-
assert isinstance(caplog.records[14].exc_info[1], RuntimeError)
108-
assert str(caplog.records[14].exc_info[1]) == 'Dummy RuntimeError'
109-
110-
# Record 15: Exiting Actor
111-
assert caplog.records[15].levelno == logging.INFO
112-
assert caplog.records[15].message == 'Exiting Actor'
43+
assert len(records) == 14
44+
45+
# Record 0: Logging configured
46+
assert records[0].levelno == logging.DEBUG
47+
assert records[0].message == 'Logging configured'
48+
49+
# Record 1: Initializing Actor
50+
assert records[1].levelno == logging.INFO
51+
assert records[1].message == 'Initializing Actor'
52+
53+
# Record 2: Configuration initialized
54+
assert records[2].levelno == logging.DEBUG
55+
assert records[2].message == 'Configuration initialized'
56+
57+
# Record 3: Storage client initialized
58+
assert records[3].levelno == logging.DEBUG
59+
assert records[3].message == 'Storage client initialized'
60+
61+
# Record 4: Event manager initialized
62+
assert records[4].levelno == logging.DEBUG
63+
assert records[4].message == 'Event manager initialized'
64+
65+
# Record 5: Charging manager initialized
66+
assert records[5].levelno == logging.DEBUG
67+
assert records[5].message == 'Charging manager initialized'
68+
69+
# Record 6: Debug message
70+
assert records[6].levelno == logging.DEBUG
71+
assert records[6].message == 'Debug message'
72+
73+
# Record 7: Info message
74+
assert records[7].levelno == logging.INFO
75+
assert records[7].message == 'Info message'
76+
77+
# Record 8: Warning message
78+
assert records[8].levelno == logging.WARNING
79+
assert records[8].message == 'Warning message'
80+
81+
# Record 9: Error message
82+
assert records[9].levelno == logging.ERROR
83+
assert records[9].message == 'Error message'
84+
85+
# Record 10: Exception message with traceback (ValueError)
86+
assert records[10].levelno == logging.ERROR
87+
assert records[10].message == 'Exception message'
88+
assert records[10].exc_info is not None
89+
assert records[10].exc_info[0] is ValueError
90+
assert isinstance(records[10].exc_info[1], ValueError)
91+
assert str(records[10].exc_info[1]) == 'Dummy ValueError'
92+
93+
# Record 11: Multiline log message
94+
assert records[11].levelno == logging.INFO
95+
assert records[11].message == 'Multi\nline\nlog\nmessage'
96+
97+
# Record 12: Actor failed with an exception (RuntimeError)
98+
assert records[12].levelno == logging.ERROR
99+
assert records[12].message == 'Actor failed with an exception'
100+
assert records[12].exc_info is not None
101+
assert records[12].exc_info[0] is RuntimeError
102+
assert isinstance(records[12].exc_info[1], RuntimeError)
103+
assert str(records[12].exc_info[1]) == 'Dummy RuntimeError'
104+
105+
# Record 13: Exiting Actor
106+
assert records[13].levelno == logging.INFO
107+
assert records[13].message == 'Exiting Actor'

0 commit comments

Comments
 (0)