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