##// END OF EJS Templates
py3: almost fix test-trusted.py...
Augie Fackler -
r41388:73ccba60 default
parent child Browse files
Show More
@@ -5,19 +5,34 b''
5 5 from __future__ import absolute_import, print_function
6 6
7 7 import os
8 import sys
9
8 10 from mercurial import (
9 11 error,
12 pycompat,
10 13 ui as uimod,
11 14 util,
12 15 )
16 from mercurial.utils import stringutil
13 17
14 18 hgrc = os.environ['HGRCPATH']
15 f = open(hgrc)
19 f = open(hgrc, 'rb')
16 20 basehgrc = f.read()
17 21 f.close()
18 22
19 def testui(user='foo', group='bar', tusers=(), tgroups=(),
20 cuser='foo', cgroup='bar', debug=False, silent=False,
23 def _maybesysstr(v):
24 if isinstance(v, bytes):
25 return pycompat.sysstr(v)
26 return pycompat.sysstr(stringutil.pprint(v))
27
28 def bprint(*args, **kwargs):
29 print(*[_maybesysstr(a) for a in args],
30 **{k: _maybesysstr(v) for k, v in kwargs.items()})
31 # avoid awkward interleaving with ui object's output
32 sys.stdout.flush()
33
34 def testui(user=b'foo', group=b'bar', tusers=(), tgroups=(),
35 cuser=b'foo', cgroup=b'bar', debug=False, silent=False,
21 36 report=True):
22 37 # user, group => owners of the file
23 38 # tusers, tgroups => trusted users/groups
@@ -25,17 +40,17 b" def testui(user='foo', group='bar', tuse"
25 40
26 41 # write a global hgrc with the list of trusted users/groups and
27 42 # some setting so that we can be sure it was read
28 f = open(hgrc, 'w')
43 f = open(hgrc, 'wb')
29 44 f.write(basehgrc)
30 f.write('\n[paths]\n')
31 f.write('global = /some/path\n\n')
45 f.write(b'\n[paths]\n')
46 f.write(b'global = /some/path\n\n')
32 47
33 48 if tusers or tgroups:
34 f.write('[trusted]\n')
49 f.write(b'[trusted]\n')
35 50 if tusers:
36 f.write('users = %s\n' % ', '.join(tusers))
51 f.write(b'users = %s\n' % b', '.join(tusers))
37 52 if tgroups:
38 f.write('groups = %s\n' % ', '.join(tgroups))
53 f.write(b'groups = %s\n' % b', '.join(tgroups))
39 54 f.close()
40 55
41 56 # override the functions that give names to uids and gids
@@ -47,7 +62,7 b" def testui(user='foo', group='bar', tuse"
47 62
48 63 def groupname(gid=None):
49 64 if gid is None:
50 return 'bar'
65 return b'bar'
51 66 return group
52 67 util.groupname = groupname
53 68
@@ -58,13 +73,14 b" def testui(user='foo', group='bar', tuse"
58 73 # try to read everything
59 74 #print '# File belongs to user %s, group %s' % (user, group)
60 75 #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)
61 kind = ('different', 'same')
62 who = ('', 'user', 'group', 'user and the group')
76 kind = (b'different', b'same')
77 who = (b'', b'user', b'group', b'user and the group')
63 78 trusted = who[(user in tusers) + 2*(group in tgroups)]
64 79 if trusted:
65 trusted = ', but we trust the ' + trusted
66 print('# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
67 trusted))
80 trusted = b', but we trust the ' + trusted
81 bprint(b'# %s user, %s group%s' % (kind[user == cuser],
82 kind[group == cgroup],
83 trusted))
68 84
69 85 u = uimod.ui.load()
70 86 # disable the configuration registration warning
@@ -72,33 +88,33 b" def testui(user='foo', group='bar', tuse"
72 88 # the purpose of this test is to check the old behavior, not to validate the
73 89 # behavior from registered item. so we silent warning related to unregisted
74 90 # config.
75 u.setconfig('devel', 'warn-config-unknown', False, 'test')
76 u.setconfig('devel', 'all-warnings', False, 'test')
77 u.setconfig('ui', 'debug', str(bool(debug)))
78 u.setconfig('ui', 'report_untrusted', str(bool(report)))
79 u.readconfig('.hg/hgrc')
91 u.setconfig(b'devel', b'warn-config-unknown', False, b'test')
92 u.setconfig(b'devel', b'all-warnings', False, b'test')
93 u.setconfig(b'ui', b'debug', pycompat.bytestr(bool(debug)))
94 u.setconfig(b'ui', b'report_untrusted', pycompat.bytestr(bool(report)))
95 u.readconfig(b'.hg/hgrc')
80 96 if silent:
81 97 return u
82 print('trusted')
83 for name, path in u.configitems('paths'):
84 print(' ', name, '=', util.pconvert(path))
85 print('untrusted')
86 for name, path in u.configitems('paths', untrusted=True):
87 print('.', end=' ')
88 u.config('paths', name) # warning with debug=True
89 print('.', end=' ')
90 u.config('paths', name, untrusted=True) # no warnings
91 print(name, '=', util.pconvert(path))
98 bprint(b'trusted')
99 for name, path in u.configitems(b'paths'):
100 bprint(b' ', name, b'=', util.pconvert(path))
101 bprint(b'untrusted')
102 for name, path in u.configitems(b'paths', untrusted=True):
103 bprint(b'.', end=b' ')
104 u.config(b'paths', name) # warning with debug=True
105 bprint(b'.', end=b' ')
106 u.config(b'paths', name, untrusted=True) # no warnings
107 bprint(name, b'=', util.pconvert(path))
92 108 print()
93 109
94 110 return u
95 111
96 os.mkdir('repo')
97 os.chdir('repo')
98 os.mkdir('.hg')
99 f = open('.hg/hgrc', 'w')
100 f.write('[paths]\n')
101 f.write('local = /another/path\n\n')
112 os.mkdir(b'repo')
113 os.chdir(b'repo')
114 os.mkdir(b'.hg')
115 f = open(b'.hg/hgrc', 'wb')
116 f.write(b'[paths]\n')
117 f.write(b'local = /another/path\n\n')
102 118 f.close()
103 119
104 120 #print '# Everything is run by user foo, group bar\n'
@@ -106,120 +122,120 b' f.close()'
106 122 # same user, same group
107 123 testui()
108 124 # same user, different group
109 testui(group='def')
125 testui(group=b'def')
110 126 # different user, same group
111 testui(user='abc')
127 testui(user=b'abc')
112 128 # ... but we trust the group
113 testui(user='abc', tgroups=['bar'])
129 testui(user=b'abc', tgroups=[b'bar'])
114 130 # different user, different group
115 testui(user='abc', group='def')
131 testui(user=b'abc', group=b'def')
116 132 # ... but we trust the user
117 testui(user='abc', group='def', tusers=['abc'])
133 testui(user=b'abc', group=b'def', tusers=[b'abc'])
118 134 # ... but we trust the group
119 testui(user='abc', group='def', tgroups=['def'])
135 testui(user=b'abc', group=b'def', tgroups=[b'def'])
120 136 # ... but we trust the user and the group
121 testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
137 testui(user=b'abc', group=b'def', tusers=[b'abc'], tgroups=[b'def'])
122 138 # ... but we trust all users
123 print('# we trust all users')
124 testui(user='abc', group='def', tusers=['*'])
139 bprint(b'# we trust all users')
140 testui(user=b'abc', group=b'def', tusers=[b'*'])
125 141 # ... but we trust all groups
126 print('# we trust all groups')
127 testui(user='abc', group='def', tgroups=['*'])
142 bprint(b'# we trust all groups')
143 testui(user=b'abc', group=b'def', tgroups=[b'*'])
128 144 # ... but we trust the whole universe
129 print('# we trust all users and groups')
130 testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
145 bprint(b'# we trust all users and groups')
146 testui(user=b'abc', group=b'def', tusers=[b'*'], tgroups=[b'*'])
131 147 # ... check that users and groups are in different namespaces
132 print("# we don't get confused by users and groups with the same name")
133 testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
148 bprint(b"# we don't get confused by users and groups with the same name")
149 testui(user=b'abc', group=b'def', tusers=[b'def'], tgroups=[b'abc'])
134 150 # ... lists of user names work
135 print("# list of user names")
136 testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
137 tgroups=['bar', 'baz', 'qux'])
151 bprint(b"# list of user names")
152 testui(user=b'abc', group=b'def', tusers=[b'foo', b'xyz', b'abc', b'bleh'],
153 tgroups=[b'bar', b'baz', b'qux'])
138 154 # ... lists of group names work
139 print("# list of group names")
140 testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
141 tgroups=['bar', 'def', 'baz', 'qux'])
155 bprint(b"# list of group names")
156 testui(user=b'abc', group=b'def', tusers=[b'foo', b'xyz', b'bleh'],
157 tgroups=[b'bar', b'def', b'baz', b'qux'])
142 158
143 print("# Can't figure out the name of the user running this process")
144 testui(user='abc', group='def', cuser=None)
159 bprint(b"# Can't figure out the name of the user running this process")
160 testui(user=b'abc', group=b'def', cuser=None)
145 161
146 print("# prints debug warnings")
147 u = testui(user='abc', group='def', cuser='foo', debug=True)
162 bprint(b"# prints debug warnings")
163 u = testui(user=b'abc', group=b'def', cuser=b'foo', debug=True)
148 164
149 print("# report_untrusted enabled without debug hides warnings")
150 u = testui(user='abc', group='def', cuser='foo', report=False)
165 bprint(b"# report_untrusted enabled without debug hides warnings")
166 u = testui(user=b'abc', group=b'def', cuser=b'foo', report=False)
151 167
152 print("# report_untrusted enabled with debug shows warnings")
153 u = testui(user='abc', group='def', cuser='foo', debug=True, report=False)
168 bprint(b"# report_untrusted enabled with debug shows warnings")
169 u = testui(user=b'abc', group=b'def', cuser=b'foo', debug=True, report=False)
154 170
155 print("# ui.readconfig sections")
156 filename = 'foobar'
157 f = open(filename, 'w')
158 f.write('[foobar]\n')
159 f.write('baz = quux\n')
171 bprint(b"# ui.readconfig sections")
172 filename = b'foobar'
173 f = open(filename, 'wb')
174 f.write(b'[foobar]\n')
175 f.write(b'baz = quux\n')
160 176 f.close()
161 u.readconfig(filename, sections=['foobar'])
162 print(u.config('foobar', 'baz'))
177 u.readconfig(filename, sections=[b'foobar'])
178 bprint(u.config(b'foobar', b'baz'))
163 179
164 180 print()
165 print("# read trusted, untrusted, new ui, trusted")
181 bprint(b"# read trusted, untrusted, new ui, trusted")
166 182 u = uimod.ui.load()
167 183 # disable the configuration registration warning
168 184 #
169 185 # the purpose of this test is to check the old behavior, not to validate the
170 186 # behavior from registered item. so we silent warning related to unregisted
171 187 # config.
172 u.setconfig('devel', 'warn-config-unknown', False, 'test')
173 u.setconfig('devel', 'all-warnings', False, 'test')
174 u.setconfig('ui', 'debug', 'on')
188 u.setconfig(b'devel', b'warn-config-unknown', False, b'test')
189 u.setconfig(b'devel', b'all-warnings', False, b'test')
190 u.setconfig(b'ui', b'debug', b'on')
175 191 u.readconfig(filename)
176 192 u2 = u.copy()
177 193 def username(uid=None):
178 return 'foo'
194 return b'foo'
179 195 util.username = username
180 u2.readconfig('.hg/hgrc')
181 print('trusted:')
182 print(u2.config('foobar', 'baz'))
183 print('untrusted:')
184 print(u2.config('foobar', 'baz', untrusted=True))
196 u2.readconfig(b'.hg/hgrc')
197 bprint(b'trusted:')
198 bprint(u2.config(b'foobar', b'baz'))
199 bprint(b'untrusted:')
200 bprint(u2.config(b'foobar', b'baz', untrusted=True))
185 201
186 202 print()
187 print("# error handling")
203 bprint(b"# error handling")
188 204
189 205 def assertraises(f, exc=error.Abort):
190 206 try:
191 207 f()
192 208 except exc as inst:
193 print('raised', inst.__class__.__name__)
209 bprint(b'raised', inst.__class__.__name__)
194 210 else:
195 print('no exception?!')
211 bprint(b'no exception?!')
196 212
197 print("# file doesn't exist")
198 os.unlink('.hg/hgrc')
199 assert not os.path.exists('.hg/hgrc')
213 bprint(b"# file doesn't exist")
214 os.unlink(b'.hg/hgrc')
215 assert not os.path.exists(b'.hg/hgrc')
200 216 testui(debug=True, silent=True)
201 testui(user='abc', group='def', debug=True, silent=True)
217 testui(user=b'abc', group=b'def', debug=True, silent=True)
202 218
203 219 print()
204 print("# parse error")
205 f = open('.hg/hgrc', 'w')
206 f.write('foo')
220 bprint(b"# parse error")
221 f = open(b'.hg/hgrc', 'wb')
222 f.write(b'foo')
207 223 f.close()
208 224
209 225 try:
210 testui(user='abc', group='def', silent=True)
226 testui(user=b'abc', group=b'def', silent=True)
211 227 except error.ParseError as inst:
212 print(inst)
228 bprint(inst)
213 229
214 230 try:
215 231 testui(debug=True, silent=True)
216 232 except error.ParseError as inst:
217 print(inst)
233 bprint(inst)
218 234
219 235 print()
220 print('# access typed information')
221 with open('.hg/hgrc', 'w') as f:
222 f.write('''\
236 bprint(b'# access typed information')
237 with open(b'.hg/hgrc', 'wb') as f:
238 f.write(b'''\
223 239 [foo]
224 240 sub=main
225 241 sub:one=one
@@ -230,32 +246,33 b' int=42'
230 246 bytes=81mb
231 247 list=spam,ham,eggs
232 248 ''')
233 u = testui(user='abc', group='def', cuser='foo', silent=True)
249 u = testui(user=b'abc', group=b'def', cuser=b'foo', silent=True)
234 250 def configpath(section, name, default=None, untrusted=False):
235 251 path = u.configpath(section, name, default, untrusted)
236 252 if path is None:
237 253 return None
238 254 return util.pconvert(path)
239 255
240 print('# suboptions, trusted and untrusted')
241 trusted = u.configsuboptions('foo', 'sub')
242 untrusted = u.configsuboptions('foo', 'sub', untrusted=True)
243 print(
256 bprint(b'# suboptions, trusted and untrusted')
257 trusted = u.configsuboptions(b'foo', b'sub')
258 untrusted = u.configsuboptions(b'foo', b'sub', untrusted=True)
259 bprint(
244 260 (trusted[0], sorted(trusted[1].items())),
245 261 (untrusted[0], sorted(untrusted[1].items())))
246 print('# path, trusted and untrusted')
247 print(configpath('foo', 'path'), configpath('foo', 'path', untrusted=True))
248 print('# bool, trusted and untrusted')
249 print(u.configbool('foo', 'bool'), u.configbool('foo', 'bool', untrusted=True))
250 print('# int, trusted and untrusted')
251 print(
252 u.configint('foo', 'int', 0),
253 u.configint('foo', 'int', 0, untrusted=True))
254 print('# bytes, trusted and untrusted')
255 print(
256 u.configbytes('foo', 'bytes', 0),
257 u.configbytes('foo', 'bytes', 0, untrusted=True))
258 print('# list, trusted and untrusted')
259 print(
260 u.configlist('foo', 'list', []),
261 u.configlist('foo', 'list', [], untrusted=True))
262 bprint(b'# path, trusted and untrusted')
263 bprint(configpath(b'foo', b'path'), configpath(b'foo', b'path', untrusted=True))
264 bprint(b'# bool, trusted and untrusted')
265 bprint(u.configbool(b'foo', b'bool'),
266 u.configbool(b'foo', b'bool', untrusted=True))
267 bprint(b'# int, trusted and untrusted')
268 bprint(
269 u.configint(b'foo', b'int', 0),
270 u.configint(b'foo', b'int', 0, untrusted=True))
271 bprint(b'# bytes, trusted and untrusted')
272 bprint(
273 u.configbytes(b'foo', b'bytes', 0),
274 u.configbytes(b'foo', b'bytes', 0, untrusted=True))
275 bprint(b'# list, trusted and untrusted')
276 bprint(
277 u.configlist(b'foo', b'list', []),
278 u.configlist(b'foo', b'list', [], untrusted=True))
@@ -174,9 +174,9 b' quux'
174 174 # parse error
175 175 # different user, different group
176 176 not trusting file .hg/hgrc from untrusted user abc, group def
177 ('foo', '.hg/hgrc:1')
177 ParseError('foo', '.hg/hgrc:1')
178 178 # same user, same group
179 ('foo', '.hg/hgrc:1')
179 ParseError('foo', '.hg/hgrc:1')
180 180
181 181 # access typed information
182 182 # different user, different group
General Comments 0
You need to be logged in to leave comments. Login now