[Piglit] [Patch v2 2/4] framework/log.py: Make self.__summary keys explicitly

Dylan Baker baker.dylan.c at gmail.com
Tue Feb 18 05:32:35 PST 2014


It would be an unwelcome surprise if some test returns 'fails' or 'pas',
so rather than allowing such a thing to happen, assert that the result
is actually viable.

v2: - Spelling corrections
    - Replace list comprehension with a generator
    - Remove tests that are too implementation specific

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/core.py            |  2 +-
 framework/log.py             | 10 +++++++---
 framework/tests/log_tests.py | 17 ++++++++++++-----
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index fc59e3c..f6ae80f 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -435,7 +435,6 @@ class Test(object):
         '''
 
         log_current = log.get_current()
-        test_result = None
         # Run the test
         if env.execute:
             try:
@@ -471,6 +470,7 @@ class Test(object):
             else:
                 json_writer.write_dict_item(path, result)
         else:
+            test_result = 'dry-run'
             log.log()
         log.mark_complete(log_current, test_result)
 
diff --git a/framework/log.py b/framework/log.py
index 25ecdf5..7cdc940 100644
--- a/framework/log.py
+++ b/framework/log.py
@@ -21,6 +21,7 @@
 #
 
 import sys
+import collections
 from .threads import synchronized_self
 
 
@@ -37,7 +38,9 @@ class Log(object):
         self.__running = []
         self.__generator = (x for x in xrange(self.__total))
         self.__pad = len(str(self.__total))
-        self.__summary = {}
+        self.__summary_keys = set(['pass', 'fail', 'warn', 'crash', 'skip',
+                                   'dmesg-warn', 'dmesg-fail', 'dry-run'])
+        self.__summary = collections.defaultdict(lambda: 0)
 
     def _summary(self):
         return ", ".join("{0}: {1}".format(k, self.__summary[k])
@@ -67,8 +70,8 @@ class Log(object):
         # increment the number of completed tests
         self.__complete += 1
 
-        if result:
-            self.__summary[result] = self.__summary.get(result, 0) + 1
+        assert result in self.__summary_keys
+        self.__summary[result] += 1
 
     @synchronized_self
     def log(self):
@@ -80,6 +83,7 @@ class Log(object):
         """
         sys.stdout.write("{0} {1} {2}\r".format(
             self._percent(), self._summary(), self._running()))
+
         # Need to flush explicitly, otherwise it all gets buffered without a
         # newline.
         sys.stdout.flush()
diff --git a/framework/tests/log_tests.py b/framework/tests/log_tests.py
index 5f0640f..5effa1f 100644
--- a/framework/tests/log_tests.py
+++ b/framework/tests/log_tests.py
@@ -20,10 +20,14 @@
 
 """ Module provides tests for log.py module """
 
+import sys
+import itertools
 from types import *  # This is a special * safe module
 import nose.tools as nt
 from framework.log import Log
 
+valid_statuses = ('pass', 'fail', 'crash', 'warn', 'dmesg-warn',
+                  'dmesg-fail', 'skip', 'dry-run')
 
 def test_initialize_log():
     """ Test that Log initializes with """
@@ -63,11 +67,6 @@ def check_mark_complete_increment_summary(stat):
 
 def test_mark_complete_increment_summary():
     """ Generator that creates tests for self.__summary """
-
-
-    valid_statuses = ('pass', 'fail', 'crash', 'warn', 'dmesg-warn',
-                      'dmesg-fail', 'skip')
-
     yieldable = check_mark_complete_increment_summary
 
     for stat in valid_statuses:
@@ -83,3 +82,11 @@ def test_mark_complete_removes_complete():
     log.mark_complete(ret, 'pass')
     nt.assert_not_in(ret, log._Log__running,
                      msg="Running tests not removed from running list")
+
+
+ at nt.raises(AssertionError)
+def test_mark_complete_increment_summary_bad():
+    """ Only statuses in self.__summary_keys are valid for mark_complete """
+    log = Log(100)
+    ret = log.get_current()
+    log.mark_complete(ret, 'fails')
-- 
1.9.0



More information about the Piglit mailing list