changeset 37922:0d95ad9fc5f4

tests: port test-context.py to Python 3 This is a weird "minimally invasive" port. I think there's room to do better here, but I'm also not sure how often we'll touch this code... Differential Revision: https://phab.mercurial-scm.org/D3509
author Augie Fackler <augie@google.com>
date Fri, 27 Apr 2018 11:50:24 -0400
parents a8a7ccec1783
children 40381a88bab4
files contrib/python3-whitelist tests/test-context.py
diffstat 2 files changed, 58 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/python3-whitelist	Fri Apr 27 11:49:49 2018 -0400
+++ b/contrib/python3-whitelist	Fri Apr 27 11:50:24 2018 -0400
@@ -63,6 +63,7 @@
 test-config.t
 test-conflict.t
 test-confused-revert.t
+test-context.py
 test-contrib-check-code.t
 test-contrib-check-commit.t
 test-convert-authormap.t
--- a/tests/test-context.py	Fri Apr 27 11:49:49 2018 -0400
+++ b/tests/test-context.py	Fri Apr 27 11:50:24 2018 -0400
@@ -1,6 +1,7 @@
 from __future__ import absolute_import, print_function
 import os
 import stat
+import sys
 from mercurial.node import hex
 from mercurial import (
     context,
@@ -10,9 +11,24 @@
     ui as uimod,
 )
 
+print_ = print
+def print(*args, **kwargs):
+    """print() wrapper that flushes stdout buffers to avoid py3 buffer issues
+
+    We could also just write directly to sys.stdout.buffer the way the
+    ui object will, but this was easier for porting the test.
+    """
+    print_(*args, **kwargs)
+    sys.stdout.flush()
+
+def printb(data, end=b'\n'):
+    out = getattr(sys.stdout, 'buffer', sys.stdout)
+    out.write(data + end)
+    out.flush()
+
 u = uimod.ui.load()
 
-repo = hg.repository(u, 'test1', create=1)
+repo = hg.repository(u, b'test1', create=1)
 os.chdir('test1')
 
 # create 'foo' with fixed time stamp
@@ -22,10 +38,10 @@
 os.utime('foo', (1000, 1000))
 
 # add+commit 'foo'
-repo[None].add(['foo'])
-repo.commit(text='commit1', date="0 0")
+repo[None].add([b'foo'])
+repo.commit(text=b'commit1', date=b"0 0")
 
-d = repo[None]['foo'].date()
+d = repo[None][b'foo'].date()
 if os.name == 'nt':
     d = d[:2]
 print("workingfilectx.date = (%d, %d)" % d)
@@ -33,27 +49,28 @@
 # test memctx with non-ASCII commit message
 
 def filectxfn(repo, memctx, path):
-    return context.memfilectx(repo, memctx, "foo", "")
+    return context.memfilectx(repo, memctx, b"foo", b"")
 
-ctx = context.memctx(repo, ['tip', None],
-                     encoding.tolocal("Gr\xc3\xbcezi!"),
-                     ["foo"], filectxfn)
+ctx = context.memctx(repo, [b'tip', None],
+                     encoding.tolocal(b"Gr\xc3\xbcezi!"),
+                     [b"foo"], filectxfn)
 ctx.commit()
 for enc in "ASCII", "Latin-1", "UTF-8":
     encoding.encoding = enc
-    print("%-8s: %s" % (enc, repo["tip"].description()))
+    printb(b"%-8s: %s" % (enc.encode('ascii'), repo[b"tip"].description()))
 
 # test performing a status
 
 def getfilectx(repo, memctx, f):
     fctx = memctx.parents()[0][f]
     data, flags = fctx.data(), fctx.flags()
-    if f == 'foo':
-        data += 'bar\n'
-    return context.memfilectx(repo, memctx, f, data, 'l' in flags, 'x' in flags)
+    if f == b'foo':
+        data += b'bar\n'
+    return context.memfilectx(
+        repo, memctx, f, data, b'l' in flags, b'x' in flags)
 
 ctxa = repo[0]
-ctxb = context.memctx(repo, [ctxa.node(), None], "test diff", ["foo"],
+ctxb = context.memctx(repo, [ctxa.node(), None], b"test diff", [b"foo"],
                       getfilectx, ctxa.user(), ctxa.date())
 
 print(ctxb.status(ctxa))
@@ -61,26 +78,26 @@
 # test performing a diff on a memctx
 
 for d in ctxb.diff(ctxa, git=True):
-    print(d, end='')
+    printb(d, end=b'')
 
 # test safeness and correctness of "ctx.status()"
 print('= checking context.status():')
 
 # ancestor "wcctx ~ 2"
-actx2 = repo['.']
+actx2 = repo[b'.']
 
-repo.wwrite('bar-m', 'bar-m\n', '')
-repo.wwrite('bar-r', 'bar-r\n', '')
-repo[None].add(['bar-m', 'bar-r'])
-repo.commit(text='add bar-m, bar-r', date="0 0")
+repo.wwrite(b'bar-m', b'bar-m\n', b'')
+repo.wwrite(b'bar-r', b'bar-r\n', b'')
+repo[None].add([b'bar-m', b'bar-r'])
+repo.commit(text=b'add bar-m, bar-r', date=b"0 0")
 
 # ancestor "wcctx ~ 1"
-actx1 = repo['.']
+actx1 = repo[b'.']
 
-repo.wwrite('bar-m', 'bar-m bar-m\n', '')
-repo.wwrite('bar-a', 'bar-a\n', '')
-repo[None].add(['bar-a'])
-repo[None].forget(['bar-r'])
+repo.wwrite(b'bar-m', b'bar-m bar-m\n', b'')
+repo.wwrite(b'bar-a', b'bar-a\n', b'')
+repo[None].add([b'bar-a'])
+repo[None].forget([b'bar-r'])
 
 # status at this point:
 #   M bar-m
@@ -97,10 +114,10 @@
 
 print('=== with "pattern match":')
 print(actx1.status(other=wctx,
-                   match=scmutil.matchfiles(repo, ['bar-m', 'foo'])))
+                   match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])))
 print('wctx._status=%s' % (str(wctx._status)))
 print(actx2.status(other=wctx,
-                   match=scmutil.matchfiles(repo, ['bar-m', 'foo'])))
+                   match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])))
 print('wctx._status=%s' % (str(wctx._status)))
 
 print('=== with "always match" and "listclean=True":')
@@ -112,11 +129,11 @@
 print("== checking workingcommitctx.status:")
 
 wcctx = context.workingcommitctx(repo,
-                                 scmutil.status(['bar-m'],
-                                                ['bar-a'],
+                                 scmutil.status([b'bar-m'],
+                                                [b'bar-a'],
                                                 [],
                                                 [], [], [], []),
-                                 text='', date='0 0')
+                                 text=b'', date=b'0 0')
 print('wcctx._status=%s' % (str(wcctx._status)))
 
 print('=== with "always match":')
@@ -133,19 +150,19 @@
 
 print('=== with "pattern match":')
 print(actx1.status(other=wcctx,
-                   match=scmutil.matchfiles(repo, ['bar-m', 'foo'])))
+                   match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])))
 print('wcctx._status=%s' % (str(wcctx._status)))
 print(actx2.status(other=wcctx,
-                   match=scmutil.matchfiles(repo, ['bar-m', 'foo'])))
+                   match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])))
 print('wcctx._status=%s' % (str(wcctx._status)))
 
 print('=== with "pattern match" and "listclean=True":')
 print(actx1.status(other=wcctx,
-                   match=scmutil.matchfiles(repo, ['bar-r', 'foo']),
+                   match=scmutil.matchfiles(repo, [b'bar-r', b'foo']),
                    listclean=True))
 print('wcctx._status=%s' % (str(wcctx._status)))
 print(actx2.status(other=wcctx,
-                   match=scmutil.matchfiles(repo, ['bar-r', 'foo']),
+                   match=scmutil.matchfiles(repo, [b'bar-r', b'foo']),
                    listclean=True))
 print('wcctx._status=%s' % (str(wcctx._status)))
 
@@ -154,7 +171,7 @@
 # test manifestlog being changed
 print('== commit with manifestlog invalidated')
 
-repo = hg.repository(u, 'test2', create=1)
+repo = hg.repository(u, b'test2', create=1)
 os.chdir('test2')
 
 # make some commits
@@ -166,12 +183,12 @@
                                    date=(0, 0))
     ctx.p1().manifest() # side effect: cache manifestctx
     n = repo.commitctx(ctx)
-    print('commit %s: %s' % (i, hex(n)))
+    printb(b'commit %s: %s' % (i, hex(n)))
 
     # touch 00manifest.i mtime so storecache could expire.
     # repo.__dict__['manifestlog'] is deleted by transaction releasefn.
-    st = repo.svfs.stat('00manifest.i')
-    repo.svfs.utime('00manifest.i',
+    st = repo.svfs.stat(b'00manifest.i')
+    repo.svfs.utime(b'00manifest.i',
                     (st[stat.ST_MTIME] + 1, st[stat.ST_MTIME] + 1))
 
     # read the file just committed
@@ -181,11 +198,11 @@
     except Exception as ex:
         print('cannot read data: %r' % ex)
 
-with repo.wlock(), repo.lock(), repo.transaction('test'):
+with repo.wlock(), repo.lock(), repo.transaction(b'test'):
     with open(b'4', 'wb') as f:
         f.write(b'4')
-    repo.dirstate.normal('4')
-    repo.commit('4')
+    repo.dirstate.normal(b'4')
+    repo.commit(b'4')
     revsbefore = len(repo.changelog)
     repo.invalidate(clearfilecache=True)
     revsafter = len(repo.changelog)