py3: almost fix test-trusted.py
authorAugie Fackler <augie@google.com>
Thu, 24 Jan 2019 15:04:30 -0500
changeset 41352 73ccba60aaa1
parent 41351 0ae3ddb4fbed
child 41353 07ade2dc41db
py3: almost fix test-trusted.py The test now only fails due to output format changes on the repr of mercurial.error.ParseError. I'm not really sure how to approach that for now. Differential Revision: https://phab.mercurial-scm.org/D5685
tests/test-trusted.py
tests/test-trusted.py.out
--- a/tests/test-trusted.py	Fri Jan 25 18:55:23 2019 +0900
+++ b/tests/test-trusted.py	Thu Jan 24 15:04:30 2019 -0500
@@ -5,19 +5,34 @@
 from __future__ import absolute_import, print_function
 
 import os
+import sys
+
 from mercurial import (
     error,
+    pycompat,
     ui as uimod,
     util,
 )
+from mercurial.utils import stringutil
 
 hgrc = os.environ['HGRCPATH']
-f = open(hgrc)
+f = open(hgrc, 'rb')
 basehgrc = f.read()
 f.close()
 
-def testui(user='foo', group='bar', tusers=(), tgroups=(),
-           cuser='foo', cgroup='bar', debug=False, silent=False,
+def _maybesysstr(v):
+    if isinstance(v, bytes):
+        return pycompat.sysstr(v)
+    return pycompat.sysstr(stringutil.pprint(v))
+
+def bprint(*args, **kwargs):
+    print(*[_maybesysstr(a) for a in args],
+          **{k: _maybesysstr(v) for k, v in kwargs.items()})
+    # avoid awkward interleaving with ui object's output
+    sys.stdout.flush()
+
+def testui(user=b'foo', group=b'bar', tusers=(), tgroups=(),
+           cuser=b'foo', cgroup=b'bar', debug=False, silent=False,
            report=True):
     # user, group => owners of the file
     # tusers, tgroups => trusted users/groups
@@ -25,17 +40,17 @@
 
     # write a global hgrc with the list of trusted users/groups and
     # some setting so that we can be sure it was read
-    f = open(hgrc, 'w')
+    f = open(hgrc, 'wb')
     f.write(basehgrc)
-    f.write('\n[paths]\n')
-    f.write('global = /some/path\n\n')
+    f.write(b'\n[paths]\n')
+    f.write(b'global = /some/path\n\n')
 
     if tusers or tgroups:
-        f.write('[trusted]\n')
+        f.write(b'[trusted]\n')
         if tusers:
-            f.write('users = %s\n' % ', '.join(tusers))
+            f.write(b'users = %s\n' % b', '.join(tusers))
         if tgroups:
-            f.write('groups = %s\n' % ', '.join(tgroups))
+            f.write(b'groups = %s\n' % b', '.join(tgroups))
     f.close()
 
     # override the functions that give names to uids and gids
@@ -47,7 +62,7 @@
 
     def groupname(gid=None):
         if gid is None:
-            return 'bar'
+            return b'bar'
         return group
     util.groupname = groupname
 
@@ -58,13 +73,14 @@
     # try to read everything
     #print '# File belongs to user %s, group %s' % (user, group)
     #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)
-    kind = ('different', 'same')
-    who = ('', 'user', 'group', 'user and the group')
+    kind = (b'different', b'same')
+    who = (b'', b'user', b'group', b'user and the group')
     trusted = who[(user in tusers) + 2*(group in tgroups)]
     if trusted:
-        trusted = ', but we trust the ' + trusted
-    print('# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
-                                     trusted))
+        trusted = b', but we trust the ' + trusted
+    bprint(b'# %s user, %s group%s' % (kind[user == cuser],
+                                       kind[group == cgroup],
+                                       trusted))
 
     u = uimod.ui.load()
     # disable the configuration registration warning
@@ -72,33 +88,33 @@
     # the purpose of this test is to check the old behavior, not to validate the
     # behavior from registered item. so we silent warning related to unregisted
     # config.
-    u.setconfig('devel', 'warn-config-unknown', False, 'test')
-    u.setconfig('devel', 'all-warnings', False, 'test')
-    u.setconfig('ui', 'debug', str(bool(debug)))
-    u.setconfig('ui', 'report_untrusted', str(bool(report)))
-    u.readconfig('.hg/hgrc')
+    u.setconfig(b'devel', b'warn-config-unknown', False, b'test')
+    u.setconfig(b'devel', b'all-warnings', False, b'test')
+    u.setconfig(b'ui', b'debug', pycompat.bytestr(bool(debug)))
+    u.setconfig(b'ui', b'report_untrusted', pycompat.bytestr(bool(report)))
+    u.readconfig(b'.hg/hgrc')
     if silent:
         return u
-    print('trusted')
-    for name, path in u.configitems('paths'):
-        print('   ', name, '=', util.pconvert(path))
-    print('untrusted')
-    for name, path in u.configitems('paths', untrusted=True):
-        print('.', end=' ')
-        u.config('paths', name) # warning with debug=True
-        print('.', end=' ')
-        u.config('paths', name, untrusted=True) # no warnings
-        print(name, '=', util.pconvert(path))
+    bprint(b'trusted')
+    for name, path in u.configitems(b'paths'):
+        bprint(b'   ', name, b'=', util.pconvert(path))
+    bprint(b'untrusted')
+    for name, path in u.configitems(b'paths', untrusted=True):
+        bprint(b'.', end=b' ')
+        u.config(b'paths', name) # warning with debug=True
+        bprint(b'.', end=b' ')
+        u.config(b'paths', name, untrusted=True) # no warnings
+        bprint(name, b'=', util.pconvert(path))
     print()
 
     return u
 
-os.mkdir('repo')
-os.chdir('repo')
-os.mkdir('.hg')
-f = open('.hg/hgrc', 'w')
-f.write('[paths]\n')
-f.write('local = /another/path\n\n')
+os.mkdir(b'repo')
+os.chdir(b'repo')
+os.mkdir(b'.hg')
+f = open(b'.hg/hgrc', 'wb')
+f.write(b'[paths]\n')
+f.write(b'local = /another/path\n\n')
 f.close()
 
 #print '# Everything is run by user foo, group bar\n'
@@ -106,120 +122,120 @@
 # same user, same group
 testui()
 # same user, different group
-testui(group='def')
+testui(group=b'def')
 # different user, same group
-testui(user='abc')
+testui(user=b'abc')
 # ... but we trust the group
-testui(user='abc', tgroups=['bar'])
+testui(user=b'abc', tgroups=[b'bar'])
 # different user, different group
-testui(user='abc', group='def')
+testui(user=b'abc', group=b'def')
 # ... but we trust the user
-testui(user='abc', group='def', tusers=['abc'])
+testui(user=b'abc', group=b'def', tusers=[b'abc'])
 # ... but we trust the group
-testui(user='abc', group='def', tgroups=['def'])
+testui(user=b'abc', group=b'def', tgroups=[b'def'])
 # ... but we trust the user and the group
-testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
+testui(user=b'abc', group=b'def', tusers=[b'abc'], tgroups=[b'def'])
 # ... but we trust all users
-print('# we trust all users')
-testui(user='abc', group='def', tusers=['*'])
+bprint(b'# we trust all users')
+testui(user=b'abc', group=b'def', tusers=[b'*'])
 # ... but we trust all groups
-print('# we trust all groups')
-testui(user='abc', group='def', tgroups=['*'])
+bprint(b'# we trust all groups')
+testui(user=b'abc', group=b'def', tgroups=[b'*'])
 # ... but we trust the whole universe
-print('# we trust all users and groups')
-testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
+bprint(b'# we trust all users and groups')
+testui(user=b'abc', group=b'def', tusers=[b'*'], tgroups=[b'*'])
 # ... check that users and groups are in different namespaces
-print("# we don't get confused by users and groups with the same name")
-testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
+bprint(b"# we don't get confused by users and groups with the same name")
+testui(user=b'abc', group=b'def', tusers=[b'def'], tgroups=[b'abc'])
 # ... lists of user names work
-print("# list of user names")
-testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
-       tgroups=['bar', 'baz', 'qux'])
+bprint(b"# list of user names")
+testui(user=b'abc', group=b'def', tusers=[b'foo', b'xyz', b'abc', b'bleh'],
+       tgroups=[b'bar', b'baz', b'qux'])
 # ... lists of group names work
-print("# list of group names")
-testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
-       tgroups=['bar', 'def', 'baz', 'qux'])
+bprint(b"# list of group names")
+testui(user=b'abc', group=b'def', tusers=[b'foo', b'xyz', b'bleh'],
+       tgroups=[b'bar', b'def', b'baz', b'qux'])
 
-print("# Can't figure out the name of the user running this process")
-testui(user='abc', group='def', cuser=None)
+bprint(b"# Can't figure out the name of the user running this process")
+testui(user=b'abc', group=b'def', cuser=None)
 
-print("# prints debug warnings")
-u = testui(user='abc', group='def', cuser='foo', debug=True)
+bprint(b"# prints debug warnings")
+u = testui(user=b'abc', group=b'def', cuser=b'foo', debug=True)
 
-print("# report_untrusted enabled without debug hides warnings")
-u = testui(user='abc', group='def', cuser='foo', report=False)
+bprint(b"# report_untrusted enabled without debug hides warnings")
+u = testui(user=b'abc', group=b'def', cuser=b'foo', report=False)
 
-print("# report_untrusted enabled with debug shows warnings")
-u = testui(user='abc', group='def', cuser='foo', debug=True, report=False)
+bprint(b"# report_untrusted enabled with debug shows warnings")
+u = testui(user=b'abc', group=b'def', cuser=b'foo', debug=True, report=False)
 
-print("# ui.readconfig sections")
-filename = 'foobar'
-f = open(filename, 'w')
-f.write('[foobar]\n')
-f.write('baz = quux\n')
+bprint(b"# ui.readconfig sections")
+filename = b'foobar'
+f = open(filename, 'wb')
+f.write(b'[foobar]\n')
+f.write(b'baz = quux\n')
 f.close()
-u.readconfig(filename, sections=['foobar'])
-print(u.config('foobar', 'baz'))
+u.readconfig(filename, sections=[b'foobar'])
+bprint(u.config(b'foobar', b'baz'))
 
 print()
-print("# read trusted, untrusted, new ui, trusted")
+bprint(b"# read trusted, untrusted, new ui, trusted")
 u = uimod.ui.load()
 # disable the configuration registration warning
 #
 # the purpose of this test is to check the old behavior, not to validate the
 # behavior from registered item. so we silent warning related to unregisted
 # config.
-u.setconfig('devel', 'warn-config-unknown', False, 'test')
-u.setconfig('devel', 'all-warnings', False, 'test')
-u.setconfig('ui', 'debug', 'on')
+u.setconfig(b'devel', b'warn-config-unknown', False, b'test')
+u.setconfig(b'devel', b'all-warnings', False, b'test')
+u.setconfig(b'ui', b'debug', b'on')
 u.readconfig(filename)
 u2 = u.copy()
 def username(uid=None):
-    return 'foo'
+    return b'foo'
 util.username = username
-u2.readconfig('.hg/hgrc')
-print('trusted:')
-print(u2.config('foobar', 'baz'))
-print('untrusted:')
-print(u2.config('foobar', 'baz', untrusted=True))
+u2.readconfig(b'.hg/hgrc')
+bprint(b'trusted:')
+bprint(u2.config(b'foobar', b'baz'))
+bprint(b'untrusted:')
+bprint(u2.config(b'foobar', b'baz', untrusted=True))
 
 print()
-print("# error handling")
+bprint(b"# error handling")
 
 def assertraises(f, exc=error.Abort):
     try:
         f()
     except exc as inst:
-        print('raised', inst.__class__.__name__)
+        bprint(b'raised', inst.__class__.__name__)
     else:
-        print('no exception?!')
+        bprint(b'no exception?!')
 
-print("# file doesn't exist")
-os.unlink('.hg/hgrc')
-assert not os.path.exists('.hg/hgrc')
+bprint(b"# file doesn't exist")
+os.unlink(b'.hg/hgrc')
+assert not os.path.exists(b'.hg/hgrc')
 testui(debug=True, silent=True)
-testui(user='abc', group='def', debug=True, silent=True)
+testui(user=b'abc', group=b'def', debug=True, silent=True)
 
 print()
-print("# parse error")
-f = open('.hg/hgrc', 'w')
-f.write('foo')
+bprint(b"# parse error")
+f = open(b'.hg/hgrc', 'wb')
+f.write(b'foo')
 f.close()
 
 try:
-    testui(user='abc', group='def', silent=True)
+    testui(user=b'abc', group=b'def', silent=True)
 except error.ParseError as inst:
-    print(inst)
+    bprint(inst)
 
 try:
     testui(debug=True, silent=True)
 except error.ParseError as inst:
-    print(inst)
+    bprint(inst)
 
 print()
-print('# access typed information')
-with open('.hg/hgrc', 'w') as f:
-    f.write('''\
+bprint(b'# access typed information')
+with open(b'.hg/hgrc', 'wb') as f:
+    f.write(b'''\
 [foo]
 sub=main
 sub:one=one
@@ -230,32 +246,33 @@
 bytes=81mb
 list=spam,ham,eggs
 ''')
-u = testui(user='abc', group='def', cuser='foo', silent=True)
+u = testui(user=b'abc', group=b'def', cuser=b'foo', silent=True)
 def configpath(section, name, default=None, untrusted=False):
     path = u.configpath(section, name, default, untrusted)
     if path is None:
         return None
     return util.pconvert(path)
 
-print('# suboptions, trusted and untrusted')
-trusted = u.configsuboptions('foo', 'sub')
-untrusted = u.configsuboptions('foo', 'sub', untrusted=True)
-print(
+bprint(b'# suboptions, trusted and untrusted')
+trusted = u.configsuboptions(b'foo', b'sub')
+untrusted = u.configsuboptions(b'foo', b'sub', untrusted=True)
+bprint(
     (trusted[0], sorted(trusted[1].items())),
     (untrusted[0], sorted(untrusted[1].items())))
-print('# path, trusted and untrusted')
-print(configpath('foo', 'path'), configpath('foo', 'path', untrusted=True))
-print('# bool, trusted and untrusted')
-print(u.configbool('foo', 'bool'), u.configbool('foo', 'bool', untrusted=True))
-print('# int, trusted and untrusted')
-print(
-    u.configint('foo', 'int', 0),
-    u.configint('foo', 'int', 0, untrusted=True))
-print('# bytes, trusted and untrusted')
-print(
-    u.configbytes('foo', 'bytes', 0),
-    u.configbytes('foo', 'bytes', 0, untrusted=True))
-print('# list, trusted and untrusted')
-print(
-    u.configlist('foo', 'list', []),
-    u.configlist('foo', 'list', [], untrusted=True))
+bprint(b'# path, trusted and untrusted')
+bprint(configpath(b'foo', b'path'), configpath(b'foo', b'path', untrusted=True))
+bprint(b'# bool, trusted and untrusted')
+bprint(u.configbool(b'foo', b'bool'),
+       u.configbool(b'foo', b'bool', untrusted=True))
+bprint(b'# int, trusted and untrusted')
+bprint(
+    u.configint(b'foo', b'int', 0),
+    u.configint(b'foo', b'int', 0, untrusted=True))
+bprint(b'# bytes, trusted and untrusted')
+bprint(
+    u.configbytes(b'foo', b'bytes', 0),
+    u.configbytes(b'foo', b'bytes', 0, untrusted=True))
+bprint(b'# list, trusted and untrusted')
+bprint(
+    u.configlist(b'foo', b'list', []),
+    u.configlist(b'foo', b'list', [], untrusted=True))
--- a/tests/test-trusted.py.out	Fri Jan 25 18:55:23 2019 +0900
+++ b/tests/test-trusted.py.out	Thu Jan 24 15:04:30 2019 -0500
@@ -174,9 +174,9 @@
 # parse error
 # different user, different group
 not trusting file .hg/hgrc from untrusted user abc, group def
-('foo', '.hg/hgrc:1')
+ParseError('foo', '.hg/hgrc:1')
 # same user, same group
-('foo', '.hg/hgrc:1')
+ParseError('foo', '.hg/hgrc:1')
 
 # access typed information
 # different user, different group