tests: handle string escaping/encoding on Python 3
authorGregory Szorc <gregory.szorc@gmail.com>
Wed, 23 Jan 2019 17:41:46 -0800
changeset 41328 13ccb03f2145
parent 41327 1281b2265ff5
child 41329 84707d9e77a0
tests: handle string escaping/encoding on Python 3 This code was failing on Python 3 for a few reasons: 1) sys.argv is str and str doesn't have a .decode() 2) the "string_escape" encoding was renamed to "unicode_escape" It is wonky casting to bytes to str to bytes. But this is test code, so meh. I don't believe we exercise any code paths in these tests where the arguments aren't ascii. Differential Revision: https://phab.mercurial-scm.org/D5667
tests/test-mq-missingfiles.t
tests/test-mq-qimport.t
--- a/tests/test-mq-missingfiles.t	Wed Jan 23 16:21:36 2019 -0800
+++ b/tests/test-mq-missingfiles.t	Wed Jan 23 17:41:46 2019 -0800
@@ -5,6 +5,10 @@
 
   $ cat > writelines.py <<EOF
   > import sys
+  > if sys.version_info[0] >= 3:
+  >     encode = lambda x: x.encode('utf-8').decode('unicode_escape').encode('utf-8')
+  > else:
+  >     encode = lambda x: x.decode('string_escape')
   > path = sys.argv[1]
   > args = sys.argv[2:]
   > assert (len(args) % 2) == 0
@@ -13,7 +17,7 @@
   > for i in range(len(args) // 2):
   >    count, s = args[2*i:2*i+2]
   >    count = int(count)
-  >    s = s.decode('string_escape')
+  >    s = encode(s)
   >    f.write(s*count)
   > f.close()
   > EOF
--- a/tests/test-mq-qimport.t	Wed Jan 23 16:21:36 2019 -0800
+++ b/tests/test-mq-qimport.t	Wed Jan 23 17:41:46 2019 -0800
@@ -1,5 +1,9 @@
   $ cat > writelines.py <<EOF
   > import sys
+  > if sys.version_info[0] >= 3:
+  >     encode = lambda x: x.encode('utf-8').decode('unicode_escape').encode('utf-8')
+  > else:
+  >     encode = lambda x: x.decode('string_escape')
   > path = sys.argv[1]
   > args = sys.argv[2:]
   > assert (len(args) % 2) == 0
@@ -8,7 +12,7 @@
   > for i in range(len(args)//2):
   >    count, s = args[2*i:2*i+2]
   >    count = int(count)
-  >    s = s.decode('string_escape')
+  >    s = encode(s)
   >    f.write(s*count)
   > f.close()
   >