2011-12-16 14:19:56 +04:00
|
|
|
# coding: utf8
|
2012-03-22 02:19:27 +04:00
|
|
|
"""
|
|
|
|
weasyprint.tests.testing_utils
|
|
|
|
------------------------------
|
2011-12-16 14:19:56 +04:00
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
Helpers for tests.
|
2011-12-16 14:19:56 +04:00
|
|
|
|
2013-04-03 18:23:48 +04:00
|
|
|
:copyright: Copyright 2011-2013 Simon Sapin and contributors, see AUTHORS.
|
2012-03-22 02:19:27 +04:00
|
|
|
:license: BSD, see LICENSE for details.
|
2011-12-16 14:19:56 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2012-02-20 16:04:35 +04:00
|
|
|
from __future__ import division, unicode_literals, print_function
|
2012-02-17 21:49:58 +04:00
|
|
|
|
2012-01-30 20:54:51 +04:00
|
|
|
import sys
|
2011-12-16 14:19:56 +04:00
|
|
|
import os.path
|
|
|
|
import logging
|
|
|
|
import contextlib
|
2012-02-20 16:04:35 +04:00
|
|
|
import functools
|
2011-12-16 14:19:56 +04:00
|
|
|
|
2012-03-24 16:39:31 +04:00
|
|
|
from .. import HTML, CSS
|
2012-02-22 20:12:40 +04:00
|
|
|
from ..logger import LOGGER
|
2011-12-16 14:19:56 +04:00
|
|
|
|
2012-03-24 16:39:31 +04:00
|
|
|
|
2011-12-16 14:19:56 +04:00
|
|
|
# TODO: find a way to not depend on a specific font
|
2012-02-17 21:49:58 +04:00
|
|
|
FONTS = 'Liberation Sans, Arial'
|
2012-02-15 21:49:37 +04:00
|
|
|
|
2012-07-27 19:15:59 +04:00
|
|
|
TEST_UA_STYLESHEET = CSS(filename=os.path.join(
|
2012-02-15 21:49:37 +04:00
|
|
|
os.path.dirname(__file__), '..', 'css', 'tests_ua.css'
|
|
|
|
))
|
2011-12-16 14:19:56 +04:00
|
|
|
|
|
|
|
|
2012-09-25 18:01:47 +04:00
|
|
|
class TestHTML(HTML):
|
|
|
|
"""Like weasyprint.HTML, but with a lighter UA stylesheet."""
|
|
|
|
def _ua_stylesheets(self):
|
|
|
|
return [TEST_UA_STYLESHEET]
|
2012-05-29 21:40:38 +04:00
|
|
|
|
|
|
|
|
2011-12-16 14:19:56 +04:00
|
|
|
def resource_filename(basename):
|
|
|
|
"""Return the absolute path of the resource called ``basename``."""
|
|
|
|
return os.path.join(os.path.dirname(__file__), 'resources', basename)
|
|
|
|
|
|
|
|
|
|
|
|
class CallbackHandler(logging.Handler):
|
|
|
|
"""A logging handler that calls a function for every message."""
|
|
|
|
def __init__(self, callback):
|
2012-02-20 18:36:55 +04:00
|
|
|
logging.Handler.__init__(self)
|
2011-12-16 14:48:40 +04:00
|
|
|
self.emit = callback
|
2011-12-16 14:19:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2012-02-22 20:12:40 +04:00
|
|
|
def capture_logs():
|
2011-12-16 14:19:56 +04:00
|
|
|
"""Return a context manager that captures all logged messages."""
|
2012-05-22 19:41:26 +04:00
|
|
|
logger = LOGGER
|
2011-12-16 14:19:56 +04:00
|
|
|
messages = []
|
2011-12-16 14:48:40 +04:00
|
|
|
|
|
|
|
def emit(record):
|
2012-02-21 17:13:34 +04:00
|
|
|
message = '%s: %s' % (record.levelname.upper(), record.getMessage())
|
|
|
|
messages.append(message)
|
2011-12-16 14:48:40 +04:00
|
|
|
|
2012-05-22 19:41:26 +04:00
|
|
|
previous_handlers = logger.handlers
|
|
|
|
logger.handlers = []
|
|
|
|
logger.addHandler(CallbackHandler(emit))
|
2011-12-16 14:19:56 +04:00
|
|
|
try:
|
|
|
|
yield messages
|
|
|
|
finally:
|
2012-05-22 19:41:26 +04:00
|
|
|
logger.handlers = previous_handlers
|
2011-12-16 14:19:56 +04:00
|
|
|
|
|
|
|
|
2012-02-20 16:04:35 +04:00
|
|
|
def assert_no_logs(function):
|
|
|
|
"""Decorator that asserts that nothing is logged in a function."""
|
|
|
|
@functools.wraps(function)
|
2012-08-03 18:21:47 +04:00
|
|
|
def wrapper(*args, **kwargs):
|
2012-02-20 16:04:35 +04:00
|
|
|
with capture_logs() as logs:
|
|
|
|
try:
|
2012-08-03 18:21:47 +04:00
|
|
|
function(*args, **kwargs)
|
2012-04-02 16:45:44 +04:00
|
|
|
except Exception: # pragma: no cover
|
2012-02-21 17:13:34 +04:00
|
|
|
if logs:
|
|
|
|
print('%i errors logged:' % len(logs), file=sys.stderr)
|
2012-07-23 18:07:52 +04:00
|
|
|
for message in logs:
|
|
|
|
print(message, file=sys.stderr)
|
2012-02-21 15:59:06 +04:00
|
|
|
raise
|
2012-02-20 16:04:35 +04:00
|
|
|
else:
|
2013-03-19 21:29:58 +04:00
|
|
|
if logs:
|
|
|
|
print('%i errors logged:' % len(logs))
|
|
|
|
for message in logs:
|
|
|
|
print(message, file=sys.stderr)
|
|
|
|
assert 0
|
2012-02-20 16:04:35 +04:00
|
|
|
return wrapper
|