D3509: tests: port test-context.py to Python 3

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Tue May 8 15:27:28 EDT 2018


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

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

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  contrib/python3-whitelist
  tests/test-context.py

CHANGE DETAILS

diff --git a/tests/test-context.py b/tests/test-context.py
--- a/tests/test-context.py
+++ b/tests/test-context.py
@@ -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,65 +38,66 @@
 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)
 
 # 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))
 
 # 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,28 +150,28 @@
 
 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)))
 
 os.chdir('..')
 
 # 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)
diff --git a/contrib/python3-whitelist b/contrib/python3-whitelist
--- a/contrib/python3-whitelist
+++ b/contrib/python3-whitelist
@@ -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



To: durin42, pulkit, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list