14
0
Files
python-pyfuse3/fix_catch_log_handler.patch
Matej Cepl b8aa767c10 - Add fix_catch_log_handler.patch to make tests compatible with
pytest >= 6.0.0, which removed never documented attribute
  .catch_log_handler of log item (gh#libfuse/pyfuse3#27).

OBS-URL: https://build.opensuse.org/package/show/devel:languages:python/python-pyfuse3?expand=0&rev=13
2021-03-15 18:13:04 +00:00

114 lines
4.5 KiB
Diff

From 0070eddfc33fc2fba8eb4fe9353a2d2fa1ae575b Mon Sep 17 00:00:00 2001
From: Nikolaus Rath <Nikolaus@rath.org>
Date: Sat, 21 Nov 2020 11:39:46 +0000
Subject: [PATCH] Use fixtures instead of hooks for inspecting output.
This should make us less dependent on pytest internals and compatible
with pytest 6.
Fixes: #27.
---
setup.py | 2 -
test/pytest_checklogs.py | 54 +++++++++++++----------------------------------
2 files changed, 16 insertions(+), 40 deletions(-)
--- a/setup.py
+++ b/setup.py
@@ -136,7 +136,7 @@ def main():
platforms=[ 'Linux' ],
keywords=['FUSE', 'python' ],
install_requires=['trio'],
- tests_require=['pytest', 'pytest-trio'],
+ tests_require=['pytest >= 3.4.0', 'pytest-trio'],
python_requires='>=3.5',
package_dir={'': 'src'},
py_modules=['_pyfuse3', 'pyfuse3_asyncio'],
--- a/test/pytest_checklogs.py
+++ b/test/pytest_checklogs.py
@@ -19,20 +19,7 @@ import functools
import sys
import logging
from contextlib import contextmanager
-from distutils.version import LooseVersion
-def pytest_configure(config):
- # pytest-catchlog was integrated in pytest 3.3.0
- if (LooseVersion(pytest.__version__) < "3.3.0" and
- not config.pluginmanager.hasplugin('pytest_catchlog')):
- raise ImportError('pytest catchlog plugin not found')
-
-# Fail tests if they result in log messages of severity WARNING or more.
-def check_test_log(caplog):
- for record in caplog.records:
- if (record.levelno >= logging.WARNING and
- not getattr(record, 'checklogs_ignore', False)):
- raise AssertionError('Logger received warning messages')
class CountMessagesHandler(logging.Handler):
def __init__(self, level=logging.NOTSET):
@@ -75,16 +62,12 @@ def assert_logs(pattern, level=logging.W
logger.removeHandler(handler)
if count is not None and handler.count != count:
- raise AssertionError('Expected to catch %d %r messages, but got only %d'
- % (count, pattern, handler.count))
+ pytest.fail('Expected to catch %d %r messages, but got only %d'
+ % (count, pattern, handler.count))
def check_test_output(capfd, item):
(stdout, stderr) = capfd.readouterr()
- # Write back what we've read (so that it will still be printed)
- sys.stdout.write(stdout)
- sys.stderr.write(stderr)
-
# Strip out false positives
try:
false_pos = item.checklogs_fp
@@ -101,10 +84,10 @@ def check_test_output(capfd, item):
cp = re.compile(r'\b{}\b'.format(pattern), re.IGNORECASE | re.MULTILINE)
hit = cp.search(stderr)
if hit:
- raise AssertionError('Suspicious output to stderr (matched "%s")' % hit.group(0))
+ pytest.fail('Suspicious output to stderr (matched "%s")' % hit.group(0))
hit = cp.search(stdout)
if hit:
- raise AssertionError('Suspicious output to stdout (matched "%s")' % hit.group(0))
+ pytest.fail('Suspicious output to stdout (matched "%s")' % hit.group(0))
def register_output(item, pattern, count=1, flags=re.MULTILINE):
'''Register *pattern* as false positive for output checking
@@ -121,21 +104,14 @@ def reg_output(request):
request.node.checklogs_fp = []
return functools.partial(register_output, request.node)
-def check_output(item):
- pm = item.config.pluginmanager
- cm = pm.getplugin('capturemanager')
- capmethod = (getattr(cm, '_capturing', None) or
- getattr(item, '_capture_fixture', None) or
- getattr(cm, '_global_capturing', None))
- check_test_output(capmethod, item)
- check_test_log(item.catch_log_handler)
-
-@pytest.hookimpl(trylast=True)
-def pytest_runtest_setup(item):
- check_output(item)
-@pytest.hookimpl(trylast=True)
-def pytest_runtest_call(item):
- check_output(item)
-@pytest.hookimpl(trylast=True)
-def pytest_runtest_teardown(item, nextitem):
- check_output(item)
+# Autouse fixtures are instantiated before explicitly used fixtures, this should also
+# catch log messages emitted when e.g. initializing resources in other fixtures.
+@pytest.fixture(autouse=True)
+def check_output(caplog, capfd, request):
+ yield
+ for when in ("setup", "call", "teardown"):
+ for record in caplog.get_records(when):
+ if (record.levelno >= logging.WARNING and
+ not getattr(record, 'checklogs_ignore', False)):
+ pytest.fail('Logger received warning messages.')
+ check_test_output(capfd, request)