"Fossies" - the Fresh Open Source Software Archive

Member "fuse-3.2.3/test/conftest.py" (11 May 2018, 3129 Bytes) of package /linux/misc/fuse-3.2.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 import sys
    2 import pytest
    3 import time
    4 import re
    5 
    6 # If a test fails, wait a moment before retrieving the captured
    7 # stdout/stderr. When using a server process, this makes sure that we capture
    8 # any potential output of the server that comes *after* a test has failed. For
    9 # example, if a request handler raises an exception, the server first signals an
   10 # error to FUSE (causing the test to fail), and then logs the exception. Without
   11 # the extra delay, the exception will go into nowhere.
   12 @pytest.mark.hookwrapper
   13 def pytest_pyfunc_call(pyfuncitem):
   14     outcome = yield
   15     failed = outcome.excinfo is not None
   16     if failed:
   17         time.sleep(1)
   18 
   19 @pytest.fixture()
   20 def pass_capfd(request, capfd):
   21     '''Provide capfd object to UnitTest instances'''
   22     request.instance.capfd = capfd
   23 
   24 def check_test_output(capfd):
   25     (stdout, stderr) = capfd.readouterr()
   26 
   27     # Write back what we've read (so that it will still be printed.
   28     sys.stdout.write(stdout)
   29     sys.stderr.write(stderr)
   30 
   31     # Strip out false positives
   32     for (pattern, flags, count) in capfd.false_positives:
   33         cp = re.compile(pattern, flags)
   34         (stdout, cnt) = cp.subn('', stdout, count=count)
   35         if count == 0 or count - cnt > 0:
   36             stderr = cp.sub('', stderr, count=count - cnt)
   37 
   38     patterns = [ r'\b{}\b'.format(x) for x in
   39                  ('exception', 'error', 'warning', 'fatal', 'traceback',
   40                     'fault', 'crash(?:ed)?', 'abort(?:ed)',
   41                     'uninitiali[zs]ed') ]
   42     patterns += ['^==[0-9]+== ']
   43     for pattern in patterns:
   44         cp = re.compile(pattern, re.IGNORECASE | re.MULTILINE)
   45         hit = cp.search(stderr)
   46         if hit:
   47             raise AssertionError('Suspicious output to stderr (matched "%s")' % hit.group(0))
   48         hit = cp.search(stdout)
   49         if hit:
   50             raise AssertionError('Suspicious output to stdout (matched "%s")' % hit.group(0))
   51 
   52 def register_output(self, pattern, count=1, flags=re.MULTILINE):
   53     '''Register *pattern* as false positive for output checking
   54 
   55     This prevents the test from failing because the output otherwise
   56     appears suspicious.
   57     '''
   58 
   59     self.false_positives.append((pattern, flags, count))
   60 
   61 # This is a terrible hack that allows us to access the fixtures from the
   62 # pytest_runtest_call hook. Among a lot of other hidden assumptions, it probably
   63 # relies on tests running sequential (i.e., don't dare to use e.g. the xdist
   64 # plugin)
   65 current_capfd = None
   66 @pytest.yield_fixture(autouse=True)
   67 def save_cap_fixtures(request, capfd):
   68     global current_capfd
   69     capfd.false_positives = []
   70 
   71     # Monkeypatch in a function to register false positives
   72     type(capfd).register_output = register_output
   73 
   74     if request.config.getoption('capture') == 'no':
   75         capfd = None
   76     current_capfd = capfd
   77     bak = current_capfd
   78     yield
   79 
   80     # Try to catch problems with this hack (e.g. when running tests
   81     # simultaneously)
   82     assert bak is current_capfd
   83     current_capfd = None
   84 
   85 @pytest.hookimpl(trylast=True)
   86 def pytest_runtest_call(item):
   87     capfd = current_capfd
   88     if capfd is not None:
   89         check_test_output(capfd)