[GRASS-SVN] r65782 - grass/trunk/lib/python/gunittest

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jul 27 04:56:12 PDT 2015


Author: zarch
Date: 2015-07-27 04:56:12 -0700 (Mon, 27 Jul 2015)
New Revision: 65782

Modified:
   grass/trunk/lib/python/gunittest/case.py
   grass/trunk/lib/python/gunittest/invoker.py
   grass/trunk/lib/python/gunittest/main.py
   grass/trunk/lib/python/gunittest/reporters.py
   grass/trunk/lib/python/gunittest/runner.py
   grass/trunk/lib/python/gunittest/utils.py
Log:
gunittest: Support also Python3

Modified: grass/trunk/lib/python/gunittest/case.py
===================================================================
--- grass/trunk/lib/python/gunittest/case.py	2015-07-27 10:15:03 UTC (rev 65781)
+++ grass/trunk/lib/python/gunittest/case.py	2015-07-27 11:56:12 UTC (rev 65782)
@@ -8,10 +8,11 @@
 
 :authors: Vaclav Petras
 """
+from __future__ import print_function
 
 import os
 import subprocess
-import StringIO
+import sys
 import hashlib
 import uuid
 import unittest
@@ -27,6 +28,12 @@
 from .gutils import is_map_in_mapset
 
 
+if sys.version_info[0] == 2:
+    import StringIO
+else:
+    from io import StringIO
+
+
 class TestCase(unittest.TestCase):
     # we dissable R0904 for all TestCase classes because their purpose is to
     # provide a lot of assert methods
@@ -624,7 +631,7 @@
         name and should be always provided.
         """
         # TODO: possible improvement is to require some descriptive name
-        # and ensure uniqueness by add UUID 
+        # and ensure uniqueness by add UUID
         if self.readable_names:
             return 'tmp_' + self.id().replace('.', '_') + '_' + name
         else:
@@ -1117,8 +1124,8 @@
             module.run()
             self.grass_modules.append(module.name)
         except CalledModuleError:
-            print module.outputs.stdout
-            print module.outputs.stderr
+            print(module.outputs.stdout)
+            print(module.outputs.stderr)
             # TODO: message format
             # TODO: stderr?
             stdmsg = ('Running <{m.name}> module ended'
@@ -1130,8 +1137,8 @@
                           errors=module.outputs.stderr
                       ))
             self.fail(self._formatMessage(msg, stdmsg))
-        print module.outputs.stdout
-        print module.outputs.stderr
+        print(module.outputs.stdout)
+        print(module.outputs.stderr)
         # log these to final report
         # TODO: always or only if the calling test method failed?
         # in any case, this must be done before self.fail()
@@ -1151,11 +1158,11 @@
             module.run()
             self.grass_modules.append(module.name)
         except CalledModuleError:
-            print module.outputs.stdout
-            print module.outputs.stderr
+            print(module.outputs.stdout)
+            print(module.outputs.stderr)
         else:
-            print module.outputs.stdout
-            print module.outputs.stderr
+            print(module.outputs.stdout)
+            print(module.outputs.stderr)
             stdmsg = ('Running <%s> ended with zero (successful) return code'
                       ' when expecting module to fail' % module.get_python())
             self.fail(self._formatMessage(msg, stdmsg))

Modified: grass/trunk/lib/python/gunittest/invoker.py
===================================================================
--- grass/trunk/lib/python/gunittest/invoker.py	2015-07-27 10:15:03 UTC (rev 65781)
+++ grass/trunk/lib/python/gunittest/invoker.py	2015-07-27 11:56:12 UTC (rev 65782)
@@ -12,7 +12,6 @@
 import os
 import sys
 import shutil
-import string
 import subprocess
 
 from .checkers import text_to_keyvalue
@@ -25,6 +24,11 @@
                         NoopFileAnonymizer, keyvalue_to_text)
 from .utils import silent_rmtree, ensure_dir
 
+try:
+    from string import maketrans
+except ImportError:
+    maketrans = str.maketrans
+
 # needed for write_gisrc
 # TODO: it would be good to find some way of writing rc without the need to
 # have GRASS proprly set (anything from grass.script requires translations to
@@ -104,7 +108,7 @@
         # replace . to get rid of unclean path
         # TODO: clean paths
         # note that backslash cannot be at the end of raw string
-        dir_as_name = module.tested_dir.translate(string.maketrans(r'/\.', '___'))
+        dir_as_name = module.tested_dir.translate(maketrans(r'/\.', '___'))
         mapset = dir_as_name + '_' + module.name
         # TODO: use grass module to do this? but we are not in the right gisdbase
         mapset_dir = os.path.join(gisdbase, location, mapset)

Modified: grass/trunk/lib/python/gunittest/main.py
===================================================================
--- grass/trunk/lib/python/gunittest/main.py	2015-07-27 10:15:03 UTC (rev 65781)
+++ grass/trunk/lib/python/gunittest/main.py	2015-07-27 11:56:12 UTC (rev 65782)
@@ -15,6 +15,7 @@
 
 from unittest.main import TestProgram
 
+
 from .loader import GrassTestLoader
 from .runner import (GrassTestRunner, MultiTestResult,
                      TextTestResult, KeyValueTestResult)
@@ -55,16 +56,15 @@
                                        failfast=failfast,
                                        buffer=buffer_stdout_stderr,
                                        result=result)
-
         super(GrassTestProgram, self).__init__(module=module,
-                                               argv=unittest_argv,
-                                               testLoader=grass_loader,
-                                               testRunner=grass_runner,
-                                               exit=exit_at_end,
-                                               verbosity=verbosity,
-                                               failfast=failfast,
-                                               catchbreak=catchbreak,
-                                               buffer=buffer_stdout_stderr)
+                                                   argv=unittest_argv,
+                                                   testLoader=grass_loader,
+                                                   testRunner=grass_runner,
+                                                   exit=exit_at_end,
+                                                   verbosity=verbosity,
+                                                   failfast=failfast,
+                                                   catchbreak=catchbreak,
+                                                   buffer=buffer_stdout_stderr)
         keyval_file.close()
 
 

Modified: grass/trunk/lib/python/gunittest/reporters.py
===================================================================
--- grass/trunk/lib/python/gunittest/reporters.py	2015-07-27 10:15:03 UTC (rev 65781)
+++ grass/trunk/lib/python/gunittest/reporters.py	2015-07-27 11:56:12 UTC (rev 65782)
@@ -14,15 +14,21 @@
 import xml.sax.saxutils as saxutils
 import xml.etree.ElementTree as et
 import subprocess
-import StringIO
+import sys
 import collections
-import types
 import re
 
 from .utils import ensure_dir
 from .checkers import text_to_keyvalue
 
 
+if sys.version_info[0] == 2:
+    from StringIO import StringIO
+else:
+    from io import StringIO
+    basestring = str
+
+
 # TODO: change text_to_keyvalue to same sep as here
 # TODO: create keyvalue file and move it there together with things from checkers
 def keyvalue_to_text(keyvalue, sep='=', vsep='\n', isep=',',
@@ -30,9 +36,9 @@
     if not last_vertical:
         last_vertical = vsep == '\n'
     items = []
-    for key, value in keyvalue.iteritems():
+    for key, value in keyvalue.items():
         # TODO: use isep for iterables other than strings
-        if (not isinstance(value, types.StringTypes)
+        if (not isinstance(value, basestring)
                 and isinstance(value, collections.Iterable)):
             # TODO: this does not work for list of non-strings
             value = isep.join(value)
@@ -444,7 +450,7 @@
     if not size:
         return '<p style="color: red>File %s is empty<p>' % filename
     max_size = 10000
-    html = StringIO.StringIO()
+    html = StringIO()
     html.write(before)
     if size < max_size:
         with open(filename) as text:
@@ -582,7 +588,7 @@
                      st=self.successes, ft=self.failures + self.errors,
                      total=self.total, pt=pass_per
                      ))
-        
+
         # this is the second place with this function
         # TODO: provide one implementation
         def format_percentage(percentage):
@@ -835,7 +841,7 @@
         # TODO: add some general metadata here (passed in constructor)
 
         # add additional information
-        for key, value in self._info.iteritems():
+        for key, value in self._info.items():
             summary[key] = value
 
         summary_filename = os.path.join(self.result_dir,
@@ -1150,7 +1156,7 @@
         page.write(head)
         page.write(tests_table_head)
 
-        for directory, test_files in directories.iteritems():
+        for directory, test_files in directories.items():
             row = self.report_for_dir(root=root, directory=directory,
                                       test_files=test_files)
             page.write(row)

Modified: grass/trunk/lib/python/gunittest/runner.py
===================================================================
--- grass/trunk/lib/python/gunittest/runner.py	2015-07-27 10:15:03 UTC (rev 65781)
+++ grass/trunk/lib/python/gunittest/runner.py	2015-07-27 11:56:12 UTC (rev 65782)
@@ -43,8 +43,9 @@
     # where are also unused, so perhaps we can remove them
     # stream set to None and not included in interface, it would not make sense
     def __init__(self, stream=None, descriptions=None, verbosity=None):
-        super(TestResult, self).__init__(
-            stream=stream, descriptions=descriptions, verbosity=verbosity)
+        super(TestResult, self).__init__(stream=stream,
+                                         descriptions=descriptions,
+                                         verbosity=verbosity)
         self.successes = []
 
     def addSuccess(self, test):

Modified: grass/trunk/lib/python/gunittest/utils.py
===================================================================
--- grass/trunk/lib/python/gunittest/utils.py	2015-07-27 10:15:03 UTC (rev 65781)
+++ grass/trunk/lib/python/gunittest/utils.py	2015-07-27 11:56:12 UTC (rev 65782)
@@ -54,7 +54,10 @@
 
     sys.displayhook = new_displayhook
 
-    import __builtin__
+    try:
+        import __builtin__
+    except ImportError:
+        import builtins as __builtin__
     __builtin__._ = new_translator
 
 



More information about the grass-commit mailing list