From: Nikolaus Rath <Nikolaus@rath.org>
Date: Sat, 21 Nov 2020 11:45:44 +0000
Subject: Use fixtures instead of hooks for inspecting output.
Applied-Upstream: https://github.com/python-llfuse/python-llfuse/commit/1ed8b280d2544eedf8bf209761bef0d2519edd17

This should make us less dependent on pytest internals and compatible
with pytest 6.
---
 setup.py                 |  1 +
 test/pytest_checklogs.py | 54 ++++++++++++++----------------------------------
 2 files changed, 16 insertions(+), 39 deletions(-)

diff --git a/setup.py b/setup.py
index d105a13..b5e3d59 100755
--- a/setup.py
+++ b/setup.py
@@ -158,6 +158,7 @@ def main():
           keywords=['FUSE', 'python' ],
           package_dir={'': 'src'},
           packages=setuptools.find_packages('src'),
+          tests_require=['pytest >= 3.4.0'],
           provides=['llfuse'],
           ext_modules=[Extension('llfuse', c_sources,
                                   extra_compile_args=compile_args,
diff --git a/test/pytest_checklogs.py b/test/pytest_checklogs.py
index e3aeae1..dd560a1 100644
--- 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.WARNING, count=None):
         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)
