summaryrefslogtreecommitdiff
path: root/cherrypy/test/logtest.py
diff options
context:
space:
mode:
Diffstat (limited to 'cherrypy/test/logtest.py')
-rw-r--r--cherrypy/test/logtest.py89
1 files changed, 59 insertions, 30 deletions
diff --git a/cherrypy/test/logtest.py b/cherrypy/test/logtest.py
index cc59499a..b51a0e32 100644
--- a/cherrypy/test/logtest.py
+++ b/cherrypy/test/logtest.py
@@ -2,9 +2,11 @@
import sys
import time
+from uuid import UUID
-import cherrypy
-from cherrypy._cpcompat import basestring, ntob, unicodestr
+import six
+
+from cherrypy._cpcompat import text_or_bytes, ntob
try:
@@ -45,47 +47,47 @@ class LogCase(object):
logfile = None
lastmarker = None
- markerPrefix = ntob("test suite marker: ")
+ markerPrefix = ntob('test suite marker: ')
def _handleLogError(self, msg, data, marker, pattern):
- print("")
- print(" ERROR: %s" % msg)
+ print('')
+ print(' ERROR: %s' % msg)
if not self.interactive:
raise self.failureException(msg)
- p = (" Show: "
- "[L]og [M]arker [P]attern; "
- "[I]gnore, [R]aise, or sys.e[X]it >> ")
+ p = (' Show: '
+ '[L]og [M]arker [P]attern; '
+ '[I]gnore, [R]aise, or sys.e[X]it >> ')
sys.stdout.write(p + ' ')
# ARGH
sys.stdout.flush()
while True:
i = getchar().upper()
- if i not in "MPLIRX":
+ if i not in 'MPLIRX':
continue
print(i.upper()) # Also prints new line
- if i == "L":
+ if i == 'L':
for x, line in enumerate(data):
if (x + 1) % self.console_height == 0:
# The \r and comma should make the next line overwrite
- sys.stdout.write("<-- More -->\r ")
+ sys.stdout.write('<-- More -->\r ')
m = getchar().lower()
# Erase our "More" prompt
- sys.stdout.write(" \r ")
- if m == "q":
+ sys.stdout.write(' \r ')
+ if m == 'q':
break
print(line.rstrip())
- elif i == "M":
+ elif i == 'M':
print(repr(marker or self.lastmarker))
- elif i == "P":
+ elif i == 'P':
print(repr(pattern))
- elif i == "I":
+ elif i == 'I':
# return without raising the normal exception
return
- elif i == "R":
+ elif i == 'R':
raise self.failureException(msg)
- elif i == "X":
+ elif i == 'X':
self.exit()
sys.stdout.write(p + ' ')
@@ -94,7 +96,7 @@ class LogCase(object):
def emptyLog(self):
"""Overwrite self.logfile with 0 bytes."""
- open(self.logfile, 'wb').write("")
+ open(self.logfile, 'wb').write('')
def markLog(self, key=None):
"""Insert a marker line into the log and set self.lastmarker."""
@@ -103,7 +105,7 @@ class LogCase(object):
self.lastmarker = key
open(self.logfile, 'ab+').write(
- ntob("%s%s\n" % (self.markerPrefix, key), "utf-8"))
+ ntob('%s%s\n' % (self.markerPrefix, key), 'utf-8'))
def _read_marked_region(self, marker=None):
"""Return lines from self.logfile in the marked region.
@@ -119,13 +121,13 @@ class LogCase(object):
if marker is None:
return open(logfile, 'rb').readlines()
- if isinstance(marker, unicodestr):
+ if isinstance(marker, six.text_type):
marker = marker.encode('utf-8')
data = []
in_region = False
for line in open(logfile, 'rb'):
if in_region:
- if (line.startswith(self.markerPrefix) and not marker in line):
+ if line.startswith(self.markerPrefix) and marker not in line:
break
else:
data.append(line)
@@ -144,7 +146,7 @@ class LogCase(object):
for logline in data:
if line in logline:
return
- msg = "%r not found in log" % line
+ msg = '%r not found in log' % line
self._handleLogError(msg, data, marker, line)
def assertNotInLog(self, line, marker=None):
@@ -157,9 +159,36 @@ class LogCase(object):
data = self._read_marked_region(marker)
for logline in data:
if line in logline:
- msg = "%r found in log" % line
+ msg = '%r found in log' % line
self._handleLogError(msg, data, marker, line)
+ def assertValidUUIDv4(self, marker=None):
+ """Fail if the given UUIDv4 is not valid.
+
+ The log will be searched from the given marker to the next marker.
+ If marker is None, self.lastmarker is used. If the log hasn't
+ been marked (using self.markLog), the entire log will be searched.
+ """
+ data = self._read_marked_region(marker)
+ data = [
+ chunk.decode('utf-8').rstrip('\n').rstrip('\r')
+ for chunk in data
+ ]
+ for log_chunk in data:
+ try:
+ uuid_log = data[-1]
+ uuid_obj = UUID(uuid_log, version=4)
+ except (TypeError, ValueError):
+ pass # it might be in other chunk
+ else:
+ if str(uuid_obj) == uuid_log:
+ return
+ msg = '%r is not a valid UUIDv4' % uuid_log
+ self._handleLogError(msg, data, marker, log_chunk)
+
+ msg = 'UUIDv4 not found in log'
+ self._handleLogError(msg, data, marker, log_chunk)
+
def assertLog(self, sliceargs, lines, marker=None):
"""Fail if log.readlines()[sliceargs] is not contained in 'lines'.
@@ -172,13 +201,13 @@ class LogCase(object):
# Single arg. Use __getitem__ and allow lines to be str or list.
if isinstance(lines, (tuple, list)):
lines = lines[0]
- if isinstance(lines, unicodestr):
+ if isinstance(lines, six.text_type):
lines = lines.encode('utf-8')
if lines not in data[sliceargs]:
- msg = "%r not found on log line %r" % (lines, sliceargs)
+ msg = '%r not found on log line %r' % (lines, sliceargs)
self._handleLogError(
msg,
- [data[sliceargs], "--EXTRA CONTEXT--"] + data[
+ [data[sliceargs], '--EXTRA CONTEXT--'] + data[
sliceargs + 1:sliceargs + 6],
marker,
lines)
@@ -186,14 +215,14 @@ class LogCase(object):
# Multiple args. Use __getslice__ and require lines to be list.
if isinstance(lines, tuple):
lines = list(lines)
- elif isinstance(lines, basestring):
+ elif isinstance(lines, text_or_bytes):
raise TypeError("The 'lines' arg must be a list when "
"'sliceargs' is a tuple.")
start, stop = sliceargs
for line, logline in zip(lines, data[start:stop]):
- if isinstance(line, unicodestr):
+ if isinstance(line, six.text_type):
line = line.encode('utf-8')
if line not in logline:
- msg = "%r not found in log" % line
+ msg = '%r not found in log' % line
self._handleLogError(msg, data[start:stop], marker, line)