tests/test-dispatch.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:
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     1
import os
37924
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
     2
import sys
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37924
diff changeset
     3
from mercurial import dispatch
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37924
diff changeset
     4
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
     5
37924
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
     6
def printb(data, end=b'\n'):
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
     7
    out = getattr(sys.stdout, 'buffer', sys.stdout)
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
     8
    out.write(data + end)
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
     9
    out.flush()
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
    10
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37924
diff changeset
    11
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 5095
diff changeset
    12
def testdispatch(cmd):
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 5095
diff changeset
    13
    """Simple wrapper around dispatch.dispatch()
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    14
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    15
    Prints command and result value, but does not handle quoting.
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    16
    """
37924
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
    17
    printb(b"running: %s" % (cmd,))
14438
08bfec2ef031 dispatch: wrap dispatch related information in a request class
Idan Kamara <idankk86@gmail.com>
parents: 9031
diff changeset
    18
    req = dispatch.request(cmd.split())
08bfec2ef031 dispatch: wrap dispatch related information in a request class
Idan Kamara <idankk86@gmail.com>
parents: 9031
diff changeset
    19
    result = dispatch.dispatch(req)
37924
32106c474086 tests: port test-dispatch.py to Python 3
Augie Fackler <augie@google.com>
parents: 36374
diff changeset
    20
    printb(b"result: %r" % (result,))
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37924
diff changeset
    22
36374
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    23
testdispatch(b"init test1")
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    24
os.chdir('test1')
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    25
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    26
# create file 'foo', add and commit
9031
3b76321aa0de compat: use open() instead of file() everywhere
Alejandro Santos <alejolp@alejolp.com>
parents: 5178
diff changeset
    27
f = open('foo', 'wb')
36374
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    28
f.write(b'foo\n')
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    29
f.close()
36374
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    30
testdispatch(b"add foo")
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    31
testdispatch(b"commit -m commit1 -d 2000-01-01 foo")
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    32
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    33
# append to file 'foo' and commit
9031
3b76321aa0de compat: use open() instead of file() everywhere
Alejandro Santos <alejolp@alejolp.com>
parents: 5178
diff changeset
    34
f = open('foo', 'ab')
36374
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    35
f.write(b'bar\n')
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    36
f.close()
36374
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    37
testdispatch(b"commit -m commit2 -d 2000-01-02 foo")
5095
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    38
f3f033def181 Added test for commands.dispatch (especially 88803a69b24)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
diff changeset
    39
# check 88803a69b24 (fancyopts modified command table)
36374
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    40
testdispatch(b"log -r 0")
f0c94af0d70d py3: add b'' prefixes in test-dispatch.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28405
diff changeset
    41
testdispatch(b"log -r tip")