tests/test-config-env.py
author Gregory Szorc <gregory.szorc@gmail.com>
Fri, 16 Mar 2018 09:41:21 -0700
changeset 37000 44467a4d472f
parent 36753 a22915edc279
child 37122 d4a2e0d5d042
permissions -rw-r--r--
hgweb: refactor multirequest to be a dict of lists ... instead of a list of 2-tuples. This makes key lookups faster. The only downside is we lose total ordering of all entries. But we weren't relying on that before, so it's no loss. Differential Revision: https://phab.mercurial-scm.org/D2881

# Test the config layer generated by environment variables

from __future__ import absolute_import, print_function

import os

from mercurial import (
    encoding,
    rcutil,
    ui as uimod,
    util,
)

testtmp = encoding.environ[b'TESTTMP']

# prepare hgrc files
def join(name):
    return os.path.join(testtmp, name)

with open(join(b'sysrc'), 'wb') as f:
    f.write(b'[ui]\neditor=e0\n[pager]\npager=p0\n')

with open(join(b'userrc'), 'wb') as f:
    f.write(b'[ui]\neditor=e1')

# replace rcpath functions so they point to the files above
def systemrcpath():
    return [join(b'sysrc')]

def userrcpath():
    return [join(b'userrc')]

rcutil.systemrcpath = systemrcpath
rcutil.userrcpath = userrcpath
os.path.isdir = lambda x: False # hack: do not load default.d/*.rc

# utility to print configs
def printconfigs(env):
    encoding.environ = env
    rcutil._rccomponents = None # reset cache
    ui = uimod.ui.load()
    for section, name, value in ui.walkconfig():
        source = ui.configsource(section, name)
        util.stdout.write(b'%s.%s=%s # %s\n'
                          % (section, name, value, util.pconvert(source)))
    util.stdout.write(b'\n')

# environment variable overrides
printconfigs({})
printconfigs({b'EDITOR': b'e2', b'PAGER': b'p2'})