Skip to content

Commit

Permalink
Fix tests on Python < 3.10.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Aug 19, 2024
1 parent b05fa2c commit 09b1d8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
17 changes: 17 additions & 0 deletions tests/asyncio/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ async def asyncTearDown(self):
await self.remote_connection.close()
await self.connection.close()

if sys.version_info[:2] < (3, 10): # pragma: no cover

@contextlib.contextmanager
def assertNoLogs(self, logger="websockets", level=logging.ERROR):
"""
No message is logged on the given logger with at least the given level.
"""
with self.assertLogs(logger, level) as logs:
# We want to test that no log message is emitted
# but assertLogs expects at least one log message.
logging.getLogger(logger).log(level, "dummy")
yield

level_name = logging.getLevelName(level)
self.assertEqual(logs.output, [f"{level_name}:{logger}:dummy"])

# Test helpers built upon RecordingProtocol and InterceptingConnection.

async def assertFrameSent(self, frame):
Expand Down
31 changes: 16 additions & 15 deletions tests/legacy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,22 @@ def run_loop_once(self):
self.loop.call_soon(self.loop.stop)
self.loop.run_forever()

# Remove when dropping Python < 3.10
@contextlib.contextmanager
def assertNoLogs(self, logger="websockets", level=logging.ERROR):
"""
No message is logged on the given logger with at least the given level.
"""
with self.assertLogs(logger, level) as logs:
# We want to test that no log message is emitted
# but assertLogs expects at least one log message.
logging.getLogger(logger).log(level, "dummy")
yield

level_name = logging.getLevelName(level)
self.assertEqual(logs.output, [f"{level_name}:{logger}:dummy"])
if sys.version_info[:2] < (3, 10): # pragma: no cover

@contextlib.contextmanager
def assertNoLogs(self, logger="websockets", level=logging.ERROR):
"""
No message is logged on the given logger with at least the given level.
"""
with self.assertLogs(logger, level) as logs:
# We want to test that no log message is emitted
# but assertLogs expects at least one log message.
logging.getLogger(logger).log(level, "dummy")
yield

level_name = logging.getLevelName(level)
self.assertEqual(logs.output, [f"{level_name}:{logger}:dummy"])

def assertDeprecationWarnings(self, recorded_warnings, expected_warnings):
"""
Expand Down

0 comments on commit 09b1d8d

Please sign in to comment.