#!/usr/bin/python
import cubicweb.devtools
from cubes.apycot.testutils import ApycotBaseTC
from apycotlib import FAILURE, SUCCESS
from apycotlib.writer import TestDataWriter
from narvalbot import HTTPConnectionHandler
from cubes.narval.logformat import log_to_html
import os.path
import narvalbot
##override default ini file
test_dir = os.path.dirname(__file__)
narvalbot._CW_SOURCES_FILE = os.path.join(test_dir, 'data', 'narval-cw-sources.ini')
CW_NAMESPACE_DIV = '<div xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">%s</div>'
class MockChecker(object):
def __init__(self, id, options):
self.id = id
self.options = options
self.options_def = dict( (k, {}) for k in options )
class ApycotTC(ApycotBaseTC):
def setUp(self):
super(ApycotBaseTC, self).setUp()
te = self.lgc.start(self.lgce)
self.commit()
with self.login('narval', password='narval0') as cu:
cnxh = HTTPConnectionHandler('narval')
writer = TestDataWriter(cnxh, te.cwuri)
writer.start()
cwriter = writer.make_check_writer()
cwriter.start(MockChecker(u'pylint', {}))
cwriter.raw('pylint_version', '0.18.1', type=u'version')
cwriter.debug('hip', path='/tmp/something', line=12)
cwriter.info('hop', path='/tmp/something')
cwriter.warning('''momo\n\n<br/>''')
cwriter.end(SUCCESS)
cwriter = writer.make_check_writer()
cwriter.start(MockChecker(u'lintian', {'option': 'value'}))
cwriter.raw('lintian_version', '1.0')
cwriter.error('bouh')
cwriter.fatal('di&d')
cwriter.end(FAILURE)
writer.end(FAILURE)
self.checks = self.execute('Any X, N ORDERBY N WHERE X is CheckResult, X name N')
def test_writer_log_content(self):
checks = self.checks
self.assertEqual(len(checks), 2)
self.assertEqual(checks.get_entity(0, 0).log_file[0].data.getvalue(),
'20\t\t\toption=value<br/>40\t\t\tbouh<br/>50\t\t\tdi&d<br/>')
self.assertEqual(checks.get_entity(1, 0).log_file[0].data.getvalue(),
'10\t/tmp/something\t12\thip<br/>20\t/tmp/something\t\thop<br/>30\t\t\tmomo\n\n<br/><br/>')
def test_log_formatting_first_check(self):
stream = []
log_to_html(self.request(), '',
self.checks.get_entity(0, 0).log_file[0].data.read(),
stream.append)
log_html = '\n'.join(stream)
self.assertWellFormed(self.get_validator(content_type='application/xml'),
CW_NAMESPACE_DIV % log_html)
for pattern, count in (
('<table class="listing" id="">', 1),
('<tr class="logError"', 1),
('<tr class="logFatal"', 1),
('<tr class="logInfo"', 1),
('<td class="logSeverity"', 3),
('<td class="logPath"', 3),
('<td class="logMsg"', 3),
('<div class="rawtext"', 3),
('bouh', 1),
('di&d',1),
('option=value', 1),
):
self.assertIn(pattern, log_html)
self.assertEqual(log_html.count(pattern), count)
def test_log_formatting_second_check(self):
stream = []
log_to_html(self.request(), '',
self.checks.get_entity(1, 0).log_file[0].data.read(),
stream.append)
log_html = '\n'.join(stream)
self.assertWellFormed(self.get_validator(content_type='application/xml'),
CW_NAMESPACE_DIV % log_html)
for pattern, count in (
('<table class="listing" id="">', 1),
('<tr class="logDebug"', 1),
('<tr class="logInfo"', 1),
('<tr class="logWarning"', 1),
('<td class="logSeverity"', 3),
('<td class="logPath"', 3),
('<td class="logMsg"', 3),
('<div class="rawtext"', 3),
('hip', 1),
('hop', 1),
('momo', 1),
):
self.assertIn(pattern, log_html)
self.assertEqual(log_html.count(pattern), count)
if __name__ == '__main__':
from logilab.common.testlib import unittest_main
unittest_main()