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

105 lines
3.1 KiB
Python
Raw Normal View History

2011-12-16 14:19:56 +04:00
# coding: utf8
# WeasyPrint converts web documents (HTML, CSS, ...) to PDF.
# Copyright (C) 2011 Simon Sapin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
WeasyPrint testing suite.
"""
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
from attest import assert_hook
from .. import HTML
2011-12-16 14:19:56 +04:00
from ..document import PNGDocument
from ..css import PARSER as CSS_PARSER
2011-12-16 14:19:56 +04:00
# TODO: find a way to not depend on a specific font
FONTS = u"Liberation Sans, Arial"
TEST_UA_STYLESHEET = CSS_PARSER.parseFile(os.path.join(
os.path.dirname(__file__), '..', 'css', 'tests_ua.css'
))
2011-12-16 14:19:56 +04:00
class TestPNGDocument(PNGDocument):
"""Like PNGDocument, but with a different user-agent stylesheet.
This stylesheet is shorter, which makes tests faster.
"""
def __init__(self, html_source, base_url=None):
2011-12-16 14:19:56 +04:00
super(TestPNGDocument, self).__init__(
HTML(string=html_source, base_url=base_url).root_element,
user_stylesheets=[],
user_agent_stylesheets=[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):
super(CallbackHandler, self).__init__()
self.emit = callback
2011-12-16 14:19:56 +04:00
@contextlib.contextmanager
def capture_logs(logger_names=('WEASYPRINT', 'CSSUTILS')):
"""Return a context manager that captures all logged messages."""
previous_handlers = []
messages = []
def emit(record):
messages.append('%s: %s' % (record.levelname.upper(),
record.getMessage()))
2011-12-16 14:19:56 +04:00
for name in set(logger_names):
logger = logging.getLogger(name)
previous_handlers.append((logger, logger.handlers))
logger.handlers = []
logger.addHandler(CallbackHandler(emit))
2011-12-16 14:19:56 +04:00
try:
yield messages
finally:
for logger, handlers in previous_handlers:
logger.handlers = handlers
def assert_no_logs():
"""
When passed to ``attest.Tests.context()``, asserts that nothing is logged.
"""
with capture_logs() as logs:
2012-01-30 20:54:51 +04:00
try:
yield
except:
if logs:
sys.stderr.write('%i errors logged:\n%s\n' % (
len(logs), '\n'.join(logs)))
raise
else:
# The assert hook prints the log.
assert not logs, ('%i errors logged' % len(logs))