tests/test-config-env.py
author Arseniy Alekseyev <aalekseyev@janestreet.com>
Fri, 26 Apr 2024 19:10:35 +0100
changeset 51626 865efc020c33
parent 48875 6000f5b25c9b
permissions -rw-r--r--
dirstate: remove the python-side whitelist of allowed matchers This whitelist is too permissive because it allows matchers that contain disallowed ones deep inside, for example through `intersectionmatcher`. It is also too restrictive because it doesn't pass through some of the matchers we support, such as `patternmatcher`. It's also unnecessary because unsupported matchers raise `FallbackError` and we fall back anyway. Making this change makes more of the tests use rust code path, and therefore subtly change behavior. For example, rust status in largefiles repos seems to have strange behavior.

# Test the config layer generated by environment variables


import os

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

from mercurial.utils import procutil

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')]


extensions.wrapfunction(rcutil, 'default_rc_resources', lambda orig: [])

rcutil.systemrcpath = systemrcpath
rcutil.userrcpath = userrcpath

# 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)
        procutil.stdout.write(
            b'%s.%s=%s # %s\n' % (section, name, value, util.pconvert(source))
        )
    procutil.stdout.write(b'\n')


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