##// END OF EJS Templates
rcutil: let environ override system configs (BC)...
Jun Wu -
r31685:d83e5165 default
parent child Browse files
Show More
@@ -0,0 +1,48 b''
1 # Test the config layer generated by environment variables
2
3 from __future__ import absolute_import, print_function
4
5 import os
6
7 from mercurial import (
8 encoding,
9 rcutil,
10 ui as uimod,
11 )
12
13 testtmp = encoding.environ['TESTTMP']
14
15 # prepare hgrc files
16 def join(name):
17 return os.path.join(testtmp, name)
18
19 with open(join('sysrc'), 'w') as f:
20 f.write('[ui]\neditor=e0\n[pager]\npager=p0\n')
21
22 with open(join('userrc'), 'w') as f:
23 f.write('[ui]\neditor=e1')
24
25 # replace rcpath functions so they point to the files above
26 def systemrcpath():
27 return [join('sysrc')]
28
29 def userrcpath():
30 return [join('userrc')]
31
32 rcutil.systemrcpath = systemrcpath
33 rcutil.userrcpath = userrcpath
34 os.path.isdir = lambda x: False # hack: do not load default.d/*.rc
35
36 # utility to print configs
37 def printconfigs(env):
38 encoding.environ = env
39 rcutil._rccomponents = None # reset cache
40 ui = uimod.ui.load()
41 for section, name, value in ui.walkconfig():
42 source = ui.configsource(section, name)
43 print('%s.%s=%s # %s' % (section, name, value, source))
44 print('')
45
46 # environment variable overrides
47 printconfigs({})
48 printconfigs({'EDITOR': 'e2', 'PAGER': 'p2'})
@@ -0,0 +1,6 b''
1 pager.pager=p0 # $TESTTMP/sysrc:4
2 ui.editor=e1 # $TESTTMP/userrc:2
3
4 pager.pager=p2 # $PAGER
5 ui.editor=e1 # $TESTTMP/userrc:2
6
@@ -1807,6 +1807,8 b' def config(ui, repo, *values, **opts):'
1807 1807 for t, f in rcutil.rccomponents():
1808 1808 if t == 'path':
1809 1809 ui.debug('read config from: %s\n' % f)
1810 elif t == 'items':
1811 pass
1810 1812 else:
1811 1813 raise error.ProgrammingError('unknown rctype: %s' % t)
1812 1814 untrusted = bool(opts.get('untrusted'))
@@ -76,15 +76,22 b' def rccomponents():'
76 76 and is the config file path. if type is 'items', obj is a list of (section,
77 77 name, value, source) that should fill the config directly.
78 78 '''
79 envrc = ('items', envrcitems())
80
79 81 global _rccomponents
80 82 if _rccomponents is None:
81 83 if 'HGRCPATH' in encoding.environ:
82 _rccomponents = []
84 # assume HGRCPATH is all about user configs so environments can be
85 # overridden.
86 _rccomponents = [envrc]
83 87 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
84 88 if not p:
85 89 continue
86 90 _rccomponents.extend(('path', p) for p in _expandrcpath(p))
87 91 else:
88 paths = defaultrcpath() + systemrcpath() + userrcpath()
92 paths = defaultrcpath() + systemrcpath()
89 93 _rccomponents = [('path', os.path.normpath(p)) for p in paths]
94 _rccomponents.append(envrc)
95 paths = userrcpath()
96 _rccomponents.extend(('path', os.path.normpath(p)) for p in paths)
90 97 return _rccomponents
@@ -211,10 +211,20 b' class ui(object):'
211 211 def load(cls):
212 212 """Create a ui and load global and user configs"""
213 213 u = cls()
214 # we always trust global config files
214 # we always trust global config files and environment variables
215 215 for t, f in rcutil.rccomponents():
216 216 if t == 'path':
217 217 u.readconfig(f, trust=True)
218 elif t == 'items':
219 sections = set()
220 for section, name, value, source in f:
221 # do not set u._ocfg
222 # XXX clean this up once immutable config object is a thing
223 u._tcfg.set(section, name, value, source)
224 u._ucfg.set(section, name, value, source)
225 sections.add(section)
226 for section in sections:
227 u.fixconfig(section=section)
218 228 else:
219 229 raise error.ProgrammingError('unknown rctype: %s' % t)
220 230 return u
@@ -164,3 +164,17 b' edit failure'
164 164 $ HGEDITOR=false hg config --edit
165 165 abort: edit failed: false exited with status 1
166 166 [255]
167
168 config affected by environment variables
169
170 $ EDITOR=e1 VISUAL=e2 hg config --debug | grep 'ui\.editor'
171 $VISUAL: ui.editor=e2
172
173 $ VISUAL=e2 hg config --debug --config ui.editor=e3 | grep 'ui\.editor'
174 --config: ui.editor=e3
175
176 $ PAGER=p1 hg config --debug | grep 'pager\.pager'
177 $PAGER: pager.pager=p1
178
179 $ PAGER=p1 hg config --debug --config pager.pager=p2 | grep 'pager\.pager'
180 --config: pager.pager=p2
General Comments 0
You need to be logged in to leave comments. Login now