1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
import os
import subprocess
from pathlib import Path
from typing import List, Tuple
_BROKEN_STDOUT_RETURN_CODE = 120
def setup_broken_stdout_test(
args: List[str], deprecated_python: bool
) -> Tuple[str, int]:
proc = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Call close() on stdout to cause a broken pipe.
assert proc.stdout is not None
proc.stdout.close()
returncode = proc.wait()
assert proc.stderr is not None
stderr = proc.stderr.read().decode("utf-8")
expected_msg = "ERROR: Pipe to stdout was broken"
if deprecated_python:
assert expected_msg in stderr
else:
assert stderr.startswith(expected_msg)
return stderr, returncode
def test_broken_stdout_pipe(deprecated_python: bool) -> None:
"""
Test a broken pipe to stdout.
"""
stderr, returncode = setup_broken_stdout_test(
["pip", "list"],
deprecated_python=deprecated_python,
)
# Check that no traceback occurs.
assert "raise BrokenStdoutLoggingError()" not in stderr
assert stderr.count("Traceback") == 0
assert returncode == _BROKEN_STDOUT_RETURN_CODE
def test_broken_stdout_pipe__log_option(deprecated_python: bool, tmpdir: Path) -> None:
"""
Test a broken pipe to stdout when --log is passed.
"""
log_path = os.path.join(str(tmpdir), "log.txt")
stderr, returncode = setup_broken_stdout_test(
["pip", "--log", log_path, "list"],
deprecated_python=deprecated_python,
)
# Check that no traceback occurs.
assert "raise BrokenStdoutLoggingError()" not in stderr
assert stderr.count("Traceback") == 0
assert returncode == _BROKEN_STDOUT_RETURN_CODE
def test_broken_stdout_pipe__verbose(deprecated_python: bool) -> None:
"""
Test a broken pipe to stdout with verbose logging enabled.
"""
stderr, returncode = setup_broken_stdout_test(
["pip", "-vv", "list"],
deprecated_python=deprecated_python,
)
# Check that a traceback occurs and that it occurs at most once.
# We permit up to two because the exception can be chained.
assert "raise BrokenStdoutLoggingError()" in stderr
assert 1 <= stderr.count("Traceback") <= 2
assert returncode == _BROKEN_STDOUT_RETURN_CODE
|