Show More
@@ -1,48 +1,49 | |||
|
1 | 1 | # Test the config layer generated by environment variables |
|
2 | 2 | |
|
3 | 3 | from __future__ import absolute_import, print_function |
|
4 | 4 | |
|
5 | 5 | import os |
|
6 | 6 | |
|
7 | 7 | from mercurial import ( |
|
8 | 8 | encoding, |
|
9 | 9 | rcutil, |
|
10 | 10 | ui as uimod, |
|
11 | util, | |
|
11 | 12 | ) |
|
12 | 13 | |
|
13 | 14 | testtmp = encoding.environ['TESTTMP'] |
|
14 | 15 | |
|
15 | 16 | # prepare hgrc files |
|
16 | 17 | def join(name): |
|
17 | 18 | return os.path.join(testtmp, name) |
|
18 | 19 | |
|
19 | 20 | with open(join('sysrc'), 'w') as f: |
|
20 | 21 | f.write('[ui]\neditor=e0\n[pager]\npager=p0\n') |
|
21 | 22 | |
|
22 | 23 | with open(join('userrc'), 'w') as f: |
|
23 | 24 | f.write('[ui]\neditor=e1') |
|
24 | 25 | |
|
25 | 26 | # replace rcpath functions so they point to the files above |
|
26 | 27 | def systemrcpath(): |
|
27 | 28 | return [join('sysrc')] |
|
28 | 29 | |
|
29 | 30 | def userrcpath(): |
|
30 | 31 | return [join('userrc')] |
|
31 | 32 | |
|
32 | 33 | rcutil.systemrcpath = systemrcpath |
|
33 | 34 | rcutil.userrcpath = userrcpath |
|
34 | 35 | os.path.isdir = lambda x: False # hack: do not load default.d/*.rc |
|
35 | 36 | |
|
36 | 37 | # utility to print configs |
|
37 | 38 | def printconfigs(env): |
|
38 | 39 | encoding.environ = env |
|
39 | 40 | rcutil._rccomponents = None # reset cache |
|
40 | 41 | ui = uimod.ui.load() |
|
41 | 42 | for section, name, value in ui.walkconfig(): |
|
42 | 43 | source = ui.configsource(section, name) |
|
43 | print('%s.%s=%s # %s' % (section, name, value, source)) | |
|
44 | print('%s.%s=%s # %s' % (section, name, value, util.pconvert(source))) | |
|
44 | 45 | print('') |
|
45 | 46 | |
|
46 | 47 | # environment variable overrides |
|
47 | 48 | printconfigs({}) |
|
48 | 49 | printconfigs({'EDITOR': 'e2', 'PAGER': 'p2'}) |
@@ -1,241 +1,247 | |||
|
1 | 1 | # Since it's not easy to write a test that portably deals |
|
2 | 2 | # with files from different users/groups, we cheat a bit by |
|
3 | 3 | # monkey-patching some functions in the util module |
|
4 | 4 | |
|
5 | 5 | from __future__ import absolute_import, print_function |
|
6 | 6 | |
|
7 | 7 | import os |
|
8 | 8 | from mercurial import ( |
|
9 | 9 | error, |
|
10 | 10 | ui as uimod, |
|
11 | 11 | util, |
|
12 | 12 | ) |
|
13 | 13 | |
|
14 | 14 | hgrc = os.environ['HGRCPATH'] |
|
15 | 15 | f = open(hgrc) |
|
16 | 16 | basehgrc = f.read() |
|
17 | 17 | f.close() |
|
18 | 18 | |
|
19 | 19 | def testui(user='foo', group='bar', tusers=(), tgroups=(), |
|
20 | 20 | cuser='foo', cgroup='bar', debug=False, silent=False, |
|
21 | 21 | report=True): |
|
22 | 22 | # user, group => owners of the file |
|
23 | 23 | # tusers, tgroups => trusted users/groups |
|
24 | 24 | # cuser, cgroup => user/group of the current process |
|
25 | 25 | |
|
26 | 26 | # write a global hgrc with the list of trusted users/groups and |
|
27 | 27 | # some setting so that we can be sure it was read |
|
28 | 28 | f = open(hgrc, 'w') |
|
29 | 29 | f.write(basehgrc) |
|
30 | 30 | f.write('\n[paths]\n') |
|
31 | 31 | f.write('global = /some/path\n\n') |
|
32 | 32 | |
|
33 | 33 | if tusers or tgroups: |
|
34 | 34 | f.write('[trusted]\n') |
|
35 | 35 | if tusers: |
|
36 | 36 | f.write('users = %s\n' % ', '.join(tusers)) |
|
37 | 37 | if tgroups: |
|
38 | 38 | f.write('groups = %s\n' % ', '.join(tgroups)) |
|
39 | 39 | f.close() |
|
40 | 40 | |
|
41 | 41 | # override the functions that give names to uids and gids |
|
42 | 42 | def username(uid=None): |
|
43 | 43 | if uid is None: |
|
44 | 44 | return cuser |
|
45 | 45 | return user |
|
46 | 46 | util.username = username |
|
47 | 47 | |
|
48 | 48 | def groupname(gid=None): |
|
49 | 49 | if gid is None: |
|
50 | 50 | return 'bar' |
|
51 | 51 | return group |
|
52 | 52 | util.groupname = groupname |
|
53 | 53 | |
|
54 | 54 | def isowner(st): |
|
55 | 55 | return user == cuser |
|
56 | 56 | util.isowner = isowner |
|
57 | 57 | |
|
58 | 58 | # try to read everything |
|
59 | 59 | #print '# File belongs to user %s, group %s' % (user, group) |
|
60 | 60 | #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups) |
|
61 | 61 | kind = ('different', 'same') |
|
62 | 62 | who = ('', 'user', 'group', 'user and the group') |
|
63 | 63 | trusted = who[(user in tusers) + 2*(group in tgroups)] |
|
64 | 64 | if trusted: |
|
65 | 65 | trusted = ', but we trust the ' + trusted |
|
66 | 66 | print('# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup], |
|
67 | 67 | trusted)) |
|
68 | 68 | |
|
69 | 69 | u = uimod.ui.load() |
|
70 | 70 | u.setconfig('ui', 'debug', str(bool(debug))) |
|
71 | 71 | u.setconfig('ui', 'report_untrusted', str(bool(report))) |
|
72 | 72 | u.readconfig('.hg/hgrc') |
|
73 | 73 | if silent: |
|
74 | 74 | return u |
|
75 | 75 | print('trusted') |
|
76 | 76 | for name, path in u.configitems('paths'): |
|
77 | print(' ', name, '=', path) | |
|
77 | print(' ', name, '=', util.pconvert(path)) | |
|
78 | 78 | print('untrusted') |
|
79 | 79 | for name, path in u.configitems('paths', untrusted=True): |
|
80 | 80 | print('.', end=' ') |
|
81 | 81 | u.config('paths', name) # warning with debug=True |
|
82 | 82 | print('.', end=' ') |
|
83 | 83 | u.config('paths', name, untrusted=True) # no warnings |
|
84 | print(name, '=', path) | |
|
84 | print(name, '=', util.pconvert(path)) | |
|
85 | 85 | print() |
|
86 | 86 | |
|
87 | 87 | return u |
|
88 | 88 | |
|
89 | 89 | os.mkdir('repo') |
|
90 | 90 | os.chdir('repo') |
|
91 | 91 | os.mkdir('.hg') |
|
92 | 92 | f = open('.hg/hgrc', 'w') |
|
93 | 93 | f.write('[paths]\n') |
|
94 | 94 | f.write('local = /another/path\n\n') |
|
95 | 95 | f.close() |
|
96 | 96 | |
|
97 | 97 | #print '# Everything is run by user foo, group bar\n' |
|
98 | 98 | |
|
99 | 99 | # same user, same group |
|
100 | 100 | testui() |
|
101 | 101 | # same user, different group |
|
102 | 102 | testui(group='def') |
|
103 | 103 | # different user, same group |
|
104 | 104 | testui(user='abc') |
|
105 | 105 | # ... but we trust the group |
|
106 | 106 | testui(user='abc', tgroups=['bar']) |
|
107 | 107 | # different user, different group |
|
108 | 108 | testui(user='abc', group='def') |
|
109 | 109 | # ... but we trust the user |
|
110 | 110 | testui(user='abc', group='def', tusers=['abc']) |
|
111 | 111 | # ... but we trust the group |
|
112 | 112 | testui(user='abc', group='def', tgroups=['def']) |
|
113 | 113 | # ... but we trust the user and the group |
|
114 | 114 | testui(user='abc', group='def', tusers=['abc'], tgroups=['def']) |
|
115 | 115 | # ... but we trust all users |
|
116 | 116 | print('# we trust all users') |
|
117 | 117 | testui(user='abc', group='def', tusers=['*']) |
|
118 | 118 | # ... but we trust all groups |
|
119 | 119 | print('# we trust all groups') |
|
120 | 120 | testui(user='abc', group='def', tgroups=['*']) |
|
121 | 121 | # ... but we trust the whole universe |
|
122 | 122 | print('# we trust all users and groups') |
|
123 | 123 | testui(user='abc', group='def', tusers=['*'], tgroups=['*']) |
|
124 | 124 | # ... check that users and groups are in different namespaces |
|
125 | 125 | print("# we don't get confused by users and groups with the same name") |
|
126 | 126 | testui(user='abc', group='def', tusers=['def'], tgroups=['abc']) |
|
127 | 127 | # ... lists of user names work |
|
128 | 128 | print("# list of user names") |
|
129 | 129 | testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'], |
|
130 | 130 | tgroups=['bar', 'baz', 'qux']) |
|
131 | 131 | # ... lists of group names work |
|
132 | 132 | print("# list of group names") |
|
133 | 133 | testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'], |
|
134 | 134 | tgroups=['bar', 'def', 'baz', 'qux']) |
|
135 | 135 | |
|
136 | 136 | print("# Can't figure out the name of the user running this process") |
|
137 | 137 | testui(user='abc', group='def', cuser=None) |
|
138 | 138 | |
|
139 | 139 | print("# prints debug warnings") |
|
140 | 140 | u = testui(user='abc', group='def', cuser='foo', debug=True) |
|
141 | 141 | |
|
142 | 142 | print("# report_untrusted enabled without debug hides warnings") |
|
143 | 143 | u = testui(user='abc', group='def', cuser='foo', report=False) |
|
144 | 144 | |
|
145 | 145 | print("# report_untrusted enabled with debug shows warnings") |
|
146 | 146 | u = testui(user='abc', group='def', cuser='foo', debug=True, report=False) |
|
147 | 147 | |
|
148 | 148 | print("# ui.readconfig sections") |
|
149 | 149 | filename = 'foobar' |
|
150 | 150 | f = open(filename, 'w') |
|
151 | 151 | f.write('[foobar]\n') |
|
152 | 152 | f.write('baz = quux\n') |
|
153 | 153 | f.close() |
|
154 | 154 | u.readconfig(filename, sections=['foobar']) |
|
155 | 155 | print(u.config('foobar', 'baz')) |
|
156 | 156 | |
|
157 | 157 | print() |
|
158 | 158 | print("# read trusted, untrusted, new ui, trusted") |
|
159 | 159 | u = uimod.ui.load() |
|
160 | 160 | u.setconfig('ui', 'debug', 'on') |
|
161 | 161 | u.readconfig(filename) |
|
162 | 162 | u2 = u.copy() |
|
163 | 163 | def username(uid=None): |
|
164 | 164 | return 'foo' |
|
165 | 165 | util.username = username |
|
166 | 166 | u2.readconfig('.hg/hgrc') |
|
167 | 167 | print('trusted:') |
|
168 | 168 | print(u2.config('foobar', 'baz')) |
|
169 | 169 | print('untrusted:') |
|
170 | 170 | print(u2.config('foobar', 'baz', untrusted=True)) |
|
171 | 171 | |
|
172 | 172 | print() |
|
173 | 173 | print("# error handling") |
|
174 | 174 | |
|
175 | 175 | def assertraises(f, exc=error.Abort): |
|
176 | 176 | try: |
|
177 | 177 | f() |
|
178 | 178 | except exc as inst: |
|
179 | 179 | print('raised', inst.__class__.__name__) |
|
180 | 180 | else: |
|
181 | 181 | print('no exception?!') |
|
182 | 182 | |
|
183 | 183 | print("# file doesn't exist") |
|
184 | 184 | os.unlink('.hg/hgrc') |
|
185 | 185 | assert not os.path.exists('.hg/hgrc') |
|
186 | 186 | testui(debug=True, silent=True) |
|
187 | 187 | testui(user='abc', group='def', debug=True, silent=True) |
|
188 | 188 | |
|
189 | 189 | print() |
|
190 | 190 | print("# parse error") |
|
191 | 191 | f = open('.hg/hgrc', 'w') |
|
192 | 192 | f.write('foo') |
|
193 | 193 | f.close() |
|
194 | 194 | |
|
195 | 195 | try: |
|
196 | 196 | testui(user='abc', group='def', silent=True) |
|
197 | 197 | except error.ParseError as inst: |
|
198 | 198 | print(inst) |
|
199 | 199 | |
|
200 | 200 | try: |
|
201 | 201 | testui(debug=True, silent=True) |
|
202 | 202 | except error.ParseError as inst: |
|
203 | 203 | print(inst) |
|
204 | 204 | |
|
205 | 205 | print() |
|
206 | 206 | print('# access typed information') |
|
207 | 207 | with open('.hg/hgrc', 'w') as f: |
|
208 | 208 | f.write('''\ |
|
209 | 209 | [foo] |
|
210 | 210 | sub=main |
|
211 | 211 | sub:one=one |
|
212 | 212 | sub:two=two |
|
213 | 213 | path=monty/python |
|
214 | 214 | bool=true |
|
215 | 215 | int=42 |
|
216 | 216 | bytes=81mb |
|
217 | 217 | list=spam,ham,eggs |
|
218 | 218 | ''') |
|
219 | 219 | u = testui(user='abc', group='def', cuser='foo', silent=True) |
|
220 | def configpath(section, name, default=None, untrusted=False): | |
|
221 | path = u.configpath(section, name, default, untrusted) | |
|
222 | if path is None: | |
|
223 | return None | |
|
224 | return util.pconvert(path) | |
|
225 | ||
|
220 | 226 | print('# suboptions, trusted and untrusted') |
|
221 | 227 | trusted = u.configsuboptions('foo', 'sub') |
|
222 | 228 | untrusted = u.configsuboptions('foo', 'sub', untrusted=True) |
|
223 | 229 | print( |
|
224 | 230 | (trusted[0], sorted(trusted[1].items())), |
|
225 | 231 | (untrusted[0], sorted(untrusted[1].items()))) |
|
226 | 232 | print('# path, trusted and untrusted') |
|
227 |
print( |
|
|
233 | print(configpath('foo', 'path'), configpath('foo', 'path', untrusted=True)) | |
|
228 | 234 | print('# bool, trusted and untrusted') |
|
229 | 235 | print(u.configbool('foo', 'bool'), u.configbool('foo', 'bool', untrusted=True)) |
|
230 | 236 | print('# int, trusted and untrusted') |
|
231 | 237 | print( |
|
232 | 238 | u.configint('foo', 'int', 0), |
|
233 | 239 | u.configint('foo', 'int', 0, untrusted=True)) |
|
234 | 240 | print('# bytes, trusted and untrusted') |
|
235 | 241 | print( |
|
236 | 242 | u.configbytes('foo', 'bytes', 0), |
|
237 | 243 | u.configbytes('foo', 'bytes', 0, untrusted=True)) |
|
238 | 244 | print('# list, trusted and untrusted') |
|
239 | 245 | print( |
|
240 | 246 | u.configlist('foo', 'list', []), |
|
241 | 247 | u.configlist('foo', 'list', [], untrusted=True)) |
General Comments 0
You need to be logged in to leave comments.
Login now