# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 2015-05-18 01:47:18
# Node ID 153b9c5235c2f3057f0faac1ce94df9ac02cd83f
# Parent  4d30467d944ea9628e5e6790477803feb84df736

run-tests: replace open-coded .decode()s on paths with a helper (issue4667)

This also cleans up the mkdtemp code mentioned in the previous patch.

At this point, the remaining callsites of .{en,de)code() are in the
following categories:

Handling escaped lines in .t files
-----------------------------------

It seems eminently reasonable to me for us to declare that .t files
are valid utf-8, and that any escape sequences we see in .t files
should be valid unicode_escape sequences.


Making error text safe for cdata blocks for xml error reports
-------------------------------------------------------------

This is a point where we're already basically screwed, and we're
simply trying to do something "good enough" that the xml output will
be vaguely useful to the user. Punting here seems fine, and we should
probably stick to the same encoding here that we used in the previous
section.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -83,15 +83,26 @@ if sys.version_info > (3, 5, 0):
     xrange = range # we use xrange in one place, and we'd rather not use range
     def _bytespath(p):
         return p.encode('utf-8')
+
+    def _strpath(p):
+        return p.decode('utf-8')
+
 elif sys.version_info >= (3, 0, 0):
     print('%s is only supported on Python 3.5+ and 2.6-2.7, not %s' %
           (sys.argv[0], '.'.join(str(v) for v in sys.version_info[:3])))
     sys.exit(70) # EX_SOFTWARE from `man 3 sysexit`
 else:
     PYTHON3 = False
+
+    # In python 2.x, path operations are generally done using
+    # bytestrings by default, so we don't have to do any extra
+    # fiddling there. We define the wrapper functions anyway just to
+    # help keep code consistent between platforms.
     def _bytespath(p):
         return p
 
+    _strpath = _bytespath
+
 def checkportisavailable(port):
     """return true if a port seems free to bind on localhost"""
     try:
@@ -443,7 +454,7 @@ class Test(unittest.TestCase):
         """
         self.path = path
         self.bname = os.path.basename(path)
-        self.name = self.bname.decode('utf-8')
+        self.name = _strpath(self.bname)
         self._testdir = os.path.dirname(path)
         self.errpath = os.path.join(self._testdir, b'%s.err' % self.bname)
 
@@ -1734,8 +1745,8 @@ class TestRunner(object):
                 # in all lowercase, which causes troubles with paths (issue3490)
                 d = osenvironb.get(b'TMP', None)
             # FILE BUG: mkdtemp works only on unicode in Python 3
-            tmpdir = tempfile.mkdtemp('', 'hgtests.',
-                                      d and d.decode('utf-8')).encode('utf-8')
+            tmpdir = tempfile.mkdtemp('', 'hgtests.', d and _strpath(d))
+            tmpdir = _bytespath(tmpdir)
 
         self._hgtmp = osenvironb[b'HGTMP'] = (
             os.path.realpath(tmpdir))
@@ -2126,7 +2137,7 @@ class TestRunner(object):
         cmd = b'%s -c "import mercurial; print (mercurial.__path__[0])"'
         cmd = cmd % PYTHON
         if PYTHON3:
-            cmd = cmd.decode('utf-8')
+            cmd = _strpath(cmd)
         pipe = os.popen(cmd)
         try:
             self._hgpath = _bytespath(pipe.read().strip())