run-tests: move string escaping to TTest
authorGregory Szorc <gregory.szorc@gmail.com>
Sun, 20 Apr 2014 10:34:52 -0700
changeset 21384 a36cc85a5b7b
parent 21383 772ed56e2519
child 21385 28414e5ac9ec
run-tests: move string escaping to TTest With this patch, TTest is almost fully self-contained and extractable. Only logging functions remain outside of its class.
tests/run-tests.py
--- a/tests/run-tests.py	Sun Apr 20 10:28:35 2014 -0700
+++ b/tests/run-tests.py	Sun Apr 20 10:34:52 2014 -0700
@@ -618,21 +618,16 @@
         return run(cmd, testtmp, self._options, replacements, env,
                    self._runner.abort)
 
-
-needescape = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
-escapesub = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
-escapemap = dict((chr(i), r'\x%02x' % i) for i in range(256))
-escapemap.update({'\\': '\\\\', '\r': r'\r'})
-def escapef(m):
-    return escapemap[m.group(0)]
-def stringescape(s):
-    return escapesub(escapef, s)
-
 class TTest(Test):
     """A "t test" is a test backed by a .t file."""
 
     SKIPPED_PREFIX = 'skipped: '
     FAILED_PREFIX = 'hghave check failed: '
+    NEEDESCAPE = re.compile(r'[\x00-\x08\x0b-\x1f\x7f-\xff]').search
+
+    ESCAPESUB = re.compile(r'[\x00-\x08\x0b-\x1f\\\x7f-\xff]').sub
+    ESCAPEMAP = dict((chr(i), r'\x%02x' % i) for i in range(256)).update(
+                     {'\\': '\\\\', '\r': r'\r'})
 
     def _run(self, testtmp, replacements, env):
         f = open(self._path)
@@ -818,8 +813,9 @@
                 if r:
                     postout.append('  ' + el)
                 else:
-                    if needescape(lout):
-                        lout = stringescape(lout.rstrip('\n')) + ' (esc)\n'
+                    if self.NEEDESCAPE(lout):
+                        lout = TTest.stringescape('%s (esc)\n' %
+                                                  lout.rstrip('\n'))
                     postout.append('  ' + lout) # Let diff deal with it.
                     if r != '': # If line failed.
                         warnonly = 3 # for sure not
@@ -918,6 +914,15 @@
 
         return missing, failed
 
+    @staticmethod
+    def _escapef(m):
+        return TTest.ESCAPEMAP[m.group(0)]
+
+    @staticmethod
+    def _stringescape(s):
+        return TTest.ESCAPESUB(TTest._escapef, s)
+
+
 wifexited = getattr(os, "WIFEXITED", lambda x: False)
 def run(cmd, wd, options, replacements, env, abort):
     """Run command in a sub-process, capturing the output (stdout and stderr).