1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 07:57:52 +03:00
WeasyPrint/weasyprint/tests/testing_utils.py

90 lines
2.4 KiB
Python
Raw Normal View History

2011-12-16 14:19:56 +04:00
# coding: utf8
"""
weasyprint.tests.testing_utils
------------------------------
2011-12-16 14:19:56 +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.
: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-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
from .. import HTML, CSS
from ..logger import LOGGER
2011-12-16 14:19:56 +04:00
2011-12-16 14:19:56 +04:00
# TODO: find a way to not depend on a specific font
FONTS = 'Liberation Sans, Arial'
TEST_UA_STYLESHEET = CSS(filename=os.path.join(
os.path.dirname(__file__), '..', 'css', 'tests_ua.css'
))
2011-12-16 14:19:56 +04:00
class TestHTML(HTML):
"""Like weasyprint.HTML, but with a lighter UA stylesheet."""
def _ua_stylesheets(self):
return [TEST_UA_STYLESHEET]
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)
self.emit = callback
2011-12-16 14:19:56 +04:00
@contextlib.contextmanager
def capture_logs():
2011-12-16 14:19:56 +04:00
"""Return a context manager that captures all logged messages."""
logger = LOGGER
2011-12-16 14:19:56 +04:00
messages = []
def emit(record):
2012-02-21 17:13:34 +04:00
message = '%s: %s' % (record.levelname.upper(), record.getMessage())
messages.append(message)
previous_handlers = logger.handlers
logger.handlers = []
logger.addHandler(CallbackHandler(emit))
2011-12-16 14:19:56 +04:00
try:
yield messages
finally:
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)
raise
2012-02-20 16:04:35 +04:00
else:
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