tests: Add ensure_not_logged to TestProcess.

This commit is contained in:
Florian Bruhin 2015-11-13 23:27:33 +01:00
parent e9ece3d114
commit 384c753094
3 changed files with 72 additions and 8 deletions

View File

@ -131,6 +131,11 @@ def compare_session(quteproc, expected):
assert utils.partial_compare(data, expected) assert utils.partial_compare(data, expected)
@bdd.then(bdd.parsers.parse('"{pattern}" should not be logged'))
def ensure_not_logged(quteproc, pattern):
quteproc.ensure_not_logged(message=pattern)
@bdd.then("no crash should happen") @bdd.then("no crash should happen")
def no_crash(): def no_crash():
"""Don't do anything. """Don't do anything.

View File

@ -74,13 +74,14 @@ class PythonProcess(testprocess.Process):
return (sys.executable, ['-c', ';'.join(code)]) return (sys.executable, ['-c', ';'.join(code)])
class TestWaitFor: @pytest.yield_fixture
def pyproc():
proc = PythonProcess()
yield proc
proc.terminate()
@pytest.yield_fixture
def pyproc(self): class TestWaitFor:
proc = PythonProcess()
yield proc
proc.terminate()
def test_successful(self, pyproc): def test_successful(self, pyproc):
"""Using wait_for with the expected text.""" """Using wait_for with the expected text."""
@ -134,3 +135,36 @@ class TestWaitFor:
pyproc.wait_for(data="foobar") pyproc.wait_for(data="foobar")
with pytest.raises(testprocess.WaitForTimeout): with pytest.raises(testprocess.WaitForTimeout):
pyproc.wait_for(data="foobar", timeout=100) pyproc.wait_for(data="foobar", timeout=100)
class TestEnsureNotLogged:
@pytest.mark.parametrize('message, pattern', [
('blacklisted', 'blacklisted'),
('bl[a]cklisted', 'bl[a]cklisted'),
('blacklisted', 'black*'),
])
def test_existing_message(self, pyproc, message, pattern):
pyproc.code = "print('{}')".format(message)
pyproc.start()
with stopwatch(max_ms=1000):
with pytest.raises(testprocess.BlacklistedMessageError):
pyproc.ensure_not_logged(data=pattern, delay=2000)
def test_late_message(self, pyproc):
pyproc.code = "time.sleep(0.5); print('blacklisted')"
pyproc.start()
with pytest.raises(testprocess.BlacklistedMessageError):
pyproc.ensure_not_logged(data='blacklisted', delay=1000)
def test_no_matching_message(self, pyproc):
pyproc.code = "print('blacklisted... nope!')"
pyproc.start()
pyproc.ensure_not_logged(data='blacklisted', delay=100)
def test_wait_for_and_blacklist(self, pyproc):
pyproc.code = "print('blacklisted')"
pyproc.start()
pyproc.wait_for(data='blacklisted')
with pytest.raises(testprocess.BlacklistedMessageError):
pyproc.ensure_not_logged(data='blacklisted', delay=0)

View File

@ -49,6 +49,11 @@ class WaitForTimeout(Exception):
"""Raised when wait_for didn't get the expected message.""" """Raised when wait_for didn't get the expected message."""
class BlacklistedMessageError(Exception):
"""Raised when ensure_not_logged found a message."""
class Line: class Line:
"""Container for a line of data the process emits. """Container for a line of data the process emits.
@ -219,13 +224,18 @@ class Process(QObject):
else: else:
return value == expected return value == expected
def wait_for(self, timeout=None, **kwargs): def wait_for(self, timeout=None, *, override_waited_for=False, **kwargs):
"""Wait until a given value is found in the data. """Wait until a given value is found in the data.
Keyword arguments to this function get interpreted as attributes of the Keyword arguments to this function get interpreted as attributes of the
searched data. Every given argument is treated as a pattern which searched data. Every given argument is treated as a pattern which
the attribute has to match against. the attribute has to match against.
Args:
timeout: How long to wait for the message.
override_waited_for: If set, gets triggered by previous messages
again.
Return: Return:
The matched line. The matched line.
""" """
@ -246,7 +256,7 @@ class Process(QObject):
value = getattr(line, key) value = getattr(line, key)
matches.append(self._match_data(value, expected)) matches.append(self._match_data(value, expected))
if all(matches) and not line.waited_for: if all(matches) and (not line.waited_for or override_waited_for):
# If we waited for this line, chances are we don't mean the # If we waited for this line, chances are we don't mean the
# same thing the next time we use wait_for and it matches # same thing the next time we use wait_for and it matches
# this line again. # this line again.
@ -280,3 +290,18 @@ class Process(QObject):
# this line again. # this line again.
line.waited_for = True line.waited_for = True
return line return line
def ensure_not_logged(self, delay=500, **kwargs):
"""Make sure the data matching the given arguments is not logged.
If nothing is found in the log, we wait for delay ms to make sure
nothing arrives.
"""
__tracebackhide__ = True
try:
line = self.wait_for(timeout=delay, override_waited_for=True,
**kwargs)
except WaitForTimeout:
return
else:
raise BlacklistedMessageError(line)