Skip to contentSkip to conversation

tests/data: Add log parsing test code

PATCH Superseded Richard Purdie 2 participants 4 messages May 26, 2015 – May 27, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/data: Add log parsing test code

This allows us to write tests which ensure a particular action generates a particular log message.

bitbake/lib/bb/tests/data.py +22 -0
index 7994a88..6ed80fb 100644
@@ -25,6 +25,28 @@ import bb
 import bb.data
 import bb.parse
 
+class LogRecord():
+    def __enter__(self):
+        import logging
+        logs = []
+        class LogHandler(logging.Handler):
+            def emit(self, record):
+                logs.append(record)
+
+        logger = logging.getLogger("BitBake")
+        handler = LogHandler()
+        logger.addHandler(handler)
+        return logs
+    def __exit__(self, type, value, traceback):
+        return
+
+def LogContains(item, logs):
+    for l in logs:
+        m = l.getMessage()
+        if item in m:
+            return True
+    return False
+
 class DataExpansions(unittest.TestCase):
     def setUp(self):
         self.d = bb.data.init()
#1 lore raw
Quoted 5 lines · Richard Purdie
bitbake/lib/bb/tests/data.py +4 -0
+class LogRecord():
+    def __enter__(self):
+        import logging
+        logs = []

bitbake/lib/bb/tests/data.py import bb · line 41

Quoted 10 lines
bitbake/lib/bb/tests/data.py +10 -0
+        class LogHandler(logging.Handler):
+            def emit(self, record):
+                logs.append(record)
+
+        logger = logging.getLogger("BitBake")
+        handler = LogHandler()
+        logger.addHandler(handler)
+        return logs
+    def __exit__(self, type, value, traceback):
+        return

I realize it probably doesn’t matter in the testing case specifically, but this lies to the user of the class. Using a context manager that doesn’t undo what it’s done on exit.. :)

Signature · 5 lines
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics
#2 lore raw
Quoted 18 lines · Christopher Larson

On Tue, May 26, 2015 at 8:17 AM, Richard Purdie
<[email protected]> wrote:
+class LogRecord():
+ def __enter__(self):
+ import logging
+ logs = []
+ class LogHandler(logging.Handler):
+ def emit(self, record):
+ logs.append(record)
+
+ logger = logging.getLogger("BitBake")
+ handler = LogHandler()
+ logger.addHandler(handler)
+ return logs
+ def __exit__(self, type, value, traceback):
+ return

Quoted 3 lines

I realize it probably doesn’t matter in the testing case specifically, but this lies to the user of the class. Using a context manager that doesn’t undo what it’s done on exit.. :)

Er, yes, quite. I knew there was something I'd meant to go back and do once it worked. I've posted a v2 with that fixed.

Cheers,

Richard

#3 lore raw
In reply to Richard Purdie

bitbake/lib/bb/tests/data.py import bb · line 48

Quoted 7 lines · Richard Purdie
bitbake/lib/bb/tests/data.py +6 -0
+def LogContains(item, logs):
+    for l in logs:
+        m = l.getMessage()
+        if item in m:
+            return True
+    return False

I didn’t notice at first, but this doesn’t comply with PEP8 — camelcase is for use with class names, not function names. Minor, obviously :)

Signature · 5 lines
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics