##// 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 for t, f in rcutil.rccomponents():
1807 for t, f in rcutil.rccomponents():
1808 if t == 'path':
1808 if t == 'path':
1809 ui.debug('read config from: %s\n' % f)
1809 ui.debug('read config from: %s\n' % f)
1810 elif t == 'items':
1811 pass
1810 else:
1812 else:
1811 raise error.ProgrammingError('unknown rctype: %s' % t)
1813 raise error.ProgrammingError('unknown rctype: %s' % t)
1812 untrusted = bool(opts.get('untrusted'))
1814 untrusted = bool(opts.get('untrusted'))
@@ -76,15 +76,22 b' def rccomponents():'
76 and is the config file path. if type is 'items', obj is a list of (section,
76 and is the config file path. if type is 'items', obj is a list of (section,
77 name, value, source) that should fill the config directly.
77 name, value, source) that should fill the config directly.
78 '''
78 '''
79 envrc = ('items', envrcitems())
80
79 global _rccomponents
81 global _rccomponents
80 if _rccomponents is None:
82 if _rccomponents is None:
81 if 'HGRCPATH' in encoding.environ:
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 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
87 for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
84 if not p:
88 if not p:
85 continue
89 continue
86 _rccomponents.extend(('path', p) for p in _expandrcpath(p))
90 _rccomponents.extend(('path', p) for p in _expandrcpath(p))
87 else:
91 else:
88 paths = defaultrcpath() + systemrcpath() + userrcpath()
92 paths = defaultrcpath() + systemrcpath()
89 _rccomponents = [('path', os.path.normpath(p)) for p in paths]
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 return _rccomponents
97 return _rccomponents
@@ -211,10 +211,20 b' class ui(object):'
211 def load(cls):
211 def load(cls):
212 """Create a ui and load global and user configs"""
212 """Create a ui and load global and user configs"""
213 u = cls()
213 u = cls()
214 # we always trust global config files
214 # we always trust global config files and environment variables
215 for t, f in rcutil.rccomponents():
215 for t, f in rcutil.rccomponents():
216 if t == 'path':
216 if t == 'path':
217 u.readconfig(f, trust=True)
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 else:
228 else:
219 raise error.ProgrammingError('unknown rctype: %s' % t)
229 raise error.ProgrammingError('unknown rctype: %s' % t)
220 return u
230 return u
@@ -164,3 +164,17 b' edit failure'
164 $ HGEDITOR=false hg config --edit
164 $ HGEDITOR=false hg config --edit
165 abort: edit failed: false exited with status 1
165 abort: edit failed: false exited with status 1
166 [255]
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