test that 4 occurences of log "skipping url replacement" are found

This commit is contained in:
Bruno Binet 2012-11-30 15:10:51 +01:00
commit a6dd3178b1
2 changed files with 26 additions and 0 deletions

View file

@ -8,6 +8,8 @@ import os
import re import re
import subprocess import subprocess
import sys import sys
import logging
from logging.handlers import BufferingHandler
from functools import wraps from functools import wraps
from contextlib import contextmanager from contextlib import contextmanager
@ -157,3 +159,18 @@ def get_settings():
settings['DIRECT_TEMPLATES'] = ['archives'] settings['DIRECT_TEMPLATES'] = ['archives']
settings['filenames'] = {} settings['filenames'] = {}
return settings return settings
class LogCountHandler(BufferingHandler):
"""
Capturing and counting logged messages.
"""
def __init__(self, capacity=1000):
logging.handlers.BufferingHandler.__init__(self, capacity)
def count_logs(self, msg=None, level=None):
return len([l for l in self.buffer
if (msg is None or re.match(msg, l.getMessage()))
and (level is None or l.levelno == level)
])

View file

@ -8,11 +8,13 @@ from filecmp import dircmp
from tempfile import mkdtemp from tempfile import mkdtemp
from shutil import rmtree from shutil import rmtree
import locale import locale
import logging
from mock import patch from mock import patch
from pelican import Pelican from pelican import Pelican
from pelican.settings import read_settings from pelican.settings import read_settings
from .support import LogCountHandler
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
SAMPLES_PATH = os.path.abspath(os.sep.join((CURRENT_DIR, "..", "samples"))) SAMPLES_PATH = os.path.abspath(os.sep.join((CURRENT_DIR, "..", "samples")))
@ -42,6 +44,8 @@ class TestPelican(unittest.TestCase):
# to run pelican in different situations and see how it behaves # to run pelican in different situations and see how it behaves
def setUp(self): def setUp(self):
self.logcount_handler = LogCountHandler()
logging.getLogger().addHandler(self.logcount_handler)
self.temp_path = mkdtemp() self.temp_path = mkdtemp()
self.old_locale = locale.setlocale(locale.LC_ALL) self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, 'C') locale.setlocale(locale.LC_ALL, 'C')
@ -49,6 +53,7 @@ class TestPelican(unittest.TestCase):
def tearDown(self): def tearDown(self):
rmtree(self.temp_path) rmtree(self.temp_path)
locale.setlocale(locale.LC_ALL, self.old_locale) locale.setlocale(locale.LC_ALL, self.old_locale)
logging.getLogger().removeHandler(self.logcount_handler)
def assertFilesEqual(self, diff): def assertFilesEqual(self, diff):
msg = "some generated files differ from the expected functional " \ msg = "some generated files differ from the expected functional " \
@ -73,6 +78,10 @@ class TestPelican(unittest.TestCase):
pelican.run() pelican.run()
dcmp = dircmp(self.temp_path, os.sep.join((OUTPUT_PATH, "basic"))) dcmp = dircmp(self.temp_path, os.sep.join((OUTPUT_PATH, "basic")))
self.assertFilesEqual(recursiveDiff(dcmp)) self.assertFilesEqual(recursiveDiff(dcmp))
self.assertEqual(self.logcount_handler.count_logs(
msg="Unable to find.*skipping url replacement",
level=logging.WARNING,
), 4, msg="bad number of occurences found for this log")
def test_custom_generation_works(self): def test_custom_generation_works(self):
# the same thing with a specified set of settings should work # the same thing with a specified set of settings should work