Commits

jordilin committed 279692c

tests for filter notification

Comments (0)

Files changed (2)

log4tailer/notifications.py

             Print.notify(self, message, log)
 
 class CornerMark(object):
+    """Displays a 5 char colored empty string 
+    at the bottom right corner of terminal in case an error, fatal or warning
+    is found."""
+    
     MARK = 5 * " "
     markable = {'FATAL' : 'backgroundemph', 
             'ERROR' : 'backgroundemph', 
         return self.corner_time
 
     def __term_num_cols(self):
+        """Returns the number columns in the current terminal using the Linux tputs
+        command line tool.
+
+        :return: The number of columns currently in the terminal.
+        """ 
         termcols = os.popen("tput cols")
         ttcols = termcols.readline()
         termcols.close()
         return ttcols
 
     def notify(self, message, log):
+        """Displays a 5 char colored empty string in case a message comes in
+        with the level specified in the class attribute markable. First time we
+        get a markable level, a timer is started with the number of seconds
+        specified in self.corner_time and a colored string will be displayed
+        for that number of seconds. The timer will not be restarted during that
+        time.
+        
+        :param message: the message object wrapping the current log trace
+        :param log: the log associated with the current message
+        """ 
         level = message.getMessageLevel().upper()
         if level in self.markable:
             self.flagged = True

test/test_filternotifier.py

+#!/usr/bin/env python
+
+import unittest
+import sys
+import os
+import re
+from os.path import dirname, abspath, join as pjoin
+sys.path.append(pjoin(abspath(dirname(__file__)), os.pardir))
+from log4tailer import notifications
+from log4tailer.Message import Message
+from log4tailer.LogColors import LogColors
+from log4tailer.Log import Log
+from log4tailer.TermColorCodes import TermColorCodes
+
+SYSOUT = sys.stdout
+
+class Writer:
+    def __init__(self):
+        self.captured = []
+    
+    def __len__(self):
+        return len(self.captured)
+
+    def write(self, txt):
+        self.captured.append(txt)
+
+class TestFilterNotifier(unittest.TestCase):
+    def setUp(self):
+        pass
+    
+    def testimplementsFilter(self):
+        filterRegexPat = re.compile(r'this not to be printed')
+        filterNotifier = notifications.Filter(filterRegexPat)
+        self.assertTrue(isinstance(filterNotifier, notifications.Filter))
+        self.assertTrue(hasattr(filterNotifier, 'notify'))
+
+    def testnotify(self):
+        pattern = re.compile(r'hi, this line to be notified')
+        trace = "info hi, this line to be notified"
+        level = "INFO"
+        notifier = notifications.Filter(pattern)
+        sys.stdout = Writer()
+        logcolors = LogColors()
+        termcolors = TermColorCodes()
+        message = Message(logcolors)
+        anylog = Log('out.log')
+        message.parse(trace, anylog)
+        notifier.notify(message, anylog)
+        output = logcolors.getLevelColor(level)+trace+termcolors.reset
+        self.assertEqual(output, sys.stdout.captured[0])
+
+    def testnoNotification(self):
+        pattern = re.compile(r'hi, this line to be notified')
+        trace = "info this is just a log trace"
+        notifier = notifications.Filter(pattern)
+        sys.stdout = Writer()
+        logcolors = LogColors()
+        message = Message(logcolors)
+        anylog = Log('out.log')
+        message.parse(trace, anylog)
+        notifier.notify(message, anylog)
+        # assert is empty
+        self.assertFalse(sys.stdout.captured)
+
+    def tearDown(self):
+        sys.stdout = SYSOUT
+
+if __name__ == '__main__':
+    unittest.main()
+