D5667: tests: handle string escaping/encoding on Python 3

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Thu Jan 24 01:58:22 UTC 2019


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5667

AFFECTED FILES
  tests/test-mq-missingfiles.t
  tests/test-mq-qimport.t

CHANGE DETAILS

diff --git a/tests/test-mq-qimport.t b/tests/test-mq-qimport.t
--- a/tests/test-mq-qimport.t
+++ b/tests/test-mq-qimport.t
@@ -1,14 +1,18 @@
   $ 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
   > 
   > f = open(path, 'wb')
   > 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()
   > 
diff --git a/tests/test-mq-missingfiles.t b/tests/test-mq-missingfiles.t
--- a/tests/test-mq-missingfiles.t
+++ b/tests/test-mq-missingfiles.t
@@ -5,15 +5,19 @@
 
   $ 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
   > 
   > f = open(path, 'wb')
   > 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



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list