tests/test-status-inprocess.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48875 6000f5b25c9b
permissions -rwxr-xr-x
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:
47500
23f5ed6dbcb1 run-tests: stop writing a `python3` symlink pointing to python2
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45830
diff changeset
     1
#!/usr/bin/env python
28843
2c7e6f363138 tests: stop direct symbol import of mercurial modules in test-status-inprocess
Yuya Nishihara <yuya@tcha.org>
parents: 28824
diff changeset
     2
37901
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
     3
import sys
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
     4
28843
2c7e6f363138 tests: stop direct symbol import of mercurial modules in test-status-inprocess
Yuya Nishihara <yuya@tcha.org>
parents: 28824
diff changeset
     5
from mercurial import (
2c7e6f363138 tests: stop direct symbol import of mercurial modules in test-status-inprocess
Yuya Nishihara <yuya@tcha.org>
parents: 28824
diff changeset
     6
    commands,
2c7e6f363138 tests: stop direct symbol import of mercurial modules in test-status-inprocess
Yuya Nishihara <yuya@tcha.org>
parents: 28824
diff changeset
     7
    localrepo,
2c7e6f363138 tests: stop direct symbol import of mercurial modules in test-status-inprocess
Yuya Nishihara <yuya@tcha.org>
parents: 28824
diff changeset
     8
    ui as uimod,
28765
7779f9dfd938 py3: use absolute_import in test-status-inprocess.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 10905
diff changeset
     9
)
10838
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    10
37901
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    11
print_ = print
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39548
diff changeset
    12
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39548
diff changeset
    13
37901
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    14
def print(*args, **kwargs):
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    15
    """print() wrapper that flushes stdout buffers to avoid py3 buffer issues
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    16
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    17
    We could also just write directly to sys.stdout.buffer the way the
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    18
    ui object will, but this was easier for porting the test.
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    19
    """
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    20
    print_(*args, **kwargs)
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    21
    sys.stdout.flush()
bbff7170f665 tests: fix test-status-inprocess.py on Python 3
Augie Fackler <augie@google.com>
parents: 37660
diff changeset
    22
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39548
diff changeset
    23
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 28843
diff changeset
    24
u = uimod.ui.load()
10838
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    25
28766
7f7cd44cd6d5 py3: use print_function in test-status-inprocess.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28765
diff changeset
    26
print('% creating repo')
39548
7ce9dea3a14a localrepo: move repo creation logic out of localrepository.__init__ (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37901
diff changeset
    27
repo = localrepo.instance(u, b'.', create=True)
10838
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    28
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    29
f = open('test.py', 'w')
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    30
try:
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    31
    f.write('foo\n')
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    32
finally:
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    33
    f.close
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    34
28766
7f7cd44cd6d5 py3: use print_function in test-status-inprocess.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28765
diff changeset
    35
print('% add and commit')
37660
9dfa4e9ed45d py3: add b'' prefixes to tests/test-status-inprocess.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30559
diff changeset
    36
commands.add(u, repo, b'test.py')
9dfa4e9ed45d py3: add b'' prefixes to tests/test-status-inprocess.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30559
diff changeset
    37
commands.commit(u, repo, message=b'*')
28843
2c7e6f363138 tests: stop direct symbol import of mercurial modules in test-status-inprocess
Yuya Nishihara <yuya@tcha.org>
parents: 28824
diff changeset
    38
commands.status(u, repo, clean=True)
10838
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    39
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    40
28766
7f7cd44cd6d5 py3: use print_function in test-status-inprocess.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28765
diff changeset
    41
print('% change')
10838
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    42
f = open('test.py', 'w')
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    43
try:
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    44
    f.write('bar\n')
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    45
finally:
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    46
    f.close()
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    47
07dbafd3a0e2 add a test for the inprocess status dirstate race
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
diff changeset
    48
# this would return clean instead of changed before the fix
28843
2c7e6f363138 tests: stop direct symbol import of mercurial modules in test-status-inprocess
Yuya Nishihara <yuya@tcha.org>
parents: 28824
diff changeset
    49
commands.status(u, repo, clean=True, modified=True)