tests/test-config-env.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48875 6000f5b25c9b
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     1
# Test the config layer generated by environment variables
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     2
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     3
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     4
import os
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     5
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     6
from mercurial import (
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     7
    encoding,
43872
527eba3013ea tests: make test-config-env.py a little less hacky
Martin von Zweigbergk <martinvonz@google.com>
parents: 43076
diff changeset
     8
    extensions,
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
     9
    rcutil,
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    10
    ui as uimod,
31857
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31685
diff changeset
    11
    util,
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    12
)
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    13
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    14
from mercurial.utils import procutil
37119
d4a2e0d5d042 procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36730
diff changeset
    15
36730
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    16
testtmp = encoding.environ[b'TESTTMP']
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    17
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    18
# prepare hgrc files
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    19
def join(name):
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    20
    return os.path.join(testtmp, name)
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    22
36730
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    23
with open(join(b'sysrc'), 'wb') as f:
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    24
    f.write(b'[ui]\neditor=e0\n[pager]\npager=p0\n')
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    25
36730
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    26
with open(join(b'userrc'), 'wb') as f:
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    27
    f.write(b'[ui]\neditor=e1')
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    28
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    29
# replace rcpath functions so they point to the files above
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    30
def systemrcpath():
36730
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    31
    return [join(b'sysrc')]
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    32
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    33
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    34
def userrcpath():
36730
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    35
    return [join(b'userrc')]
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    36
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    37
44031
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43872
diff changeset
    38
extensions.wrapfunction(rcutil, 'default_rc_resources', lambda orig: [])
43872
527eba3013ea tests: make test-config-env.py a little less hacky
Martin von Zweigbergk <martinvonz@google.com>
parents: 43076
diff changeset
    39
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    40
rcutil.systemrcpath = systemrcpath
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    41
rcutil.userrcpath = userrcpath
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    42
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    43
# utility to print configs
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    44
def printconfigs(env):
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    45
    encoding.environ = env
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    46
    rcutil._rccomponents = None  # reset cache
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    47
    ui = uimod.ui.load()
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    48
    for section, name, value in ui.walkconfig():
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    49
        source = ui.configsource(section, name)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    50
        procutil.stdout.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    51
            b'%s.%s=%s # %s\n' % (section, name, value, util.pconvert(source))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    52
        )
37119
d4a2e0d5d042 procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36730
diff changeset
    53
    procutil.stdout.write(b'\n')
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    54
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37119
diff changeset
    55
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    56
# environment variable overrides
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
diff changeset
    57
printconfigs({})
36730
a22915edc279 py3: byte-stringify test-config.t and test-config-env.py
Yuya Nishihara <yuya@tcha.org>
parents: 31857
diff changeset
    58
printconfigs({b'EDITOR': b'e2', b'PAGER': b'p2'})