2023-04-26 02:51:26 +02:00
|
|
|
import itertools
|
2020-08-19 12:40:10 +02:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import sys
|
2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Iterable, Iterator
|
2021-12-23 06:55:47 +01:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from io import SEEK_SET, TextIOWrapper
|
2020-08-19 12:40:10 +02:00
|
|
|
from types import TracebackType
|
2024-07-12 02:30:25 +02:00
|
|
|
from typing import IO, TYPE_CHECKING
|
2023-04-26 02:52:20 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
|
|
|
|
2023-04-26 02:52:20 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from _typeshed import ReadableBuffer
|
2020-08-19 12:40:10 +02:00
|
|
|
|
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
class ExtraConsoleOutputInTestError(Exception):
|
2020-08-19 12:40:10 +02:00
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-08-19 12:40:10 +02:00
|
|
|
class ExtraConsoleOutputFinder:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
valid_line_patterns = [
|
|
|
|
# Example: Running zerver.tests.test_attachments.AttachmentsTests.test_delete_unauthenticated
|
2024-04-16 23:29:07 +02:00
|
|
|
rb"^Running ",
|
|
|
|
rb"^----------------------------------------------------------------------",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: INFO: URL coverage report is in var/url_coverage.txt
|
2024-04-16 23:29:07 +02:00
|
|
|
rb"^INFO: URL coverage report is in",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: -- Running tests in parallel mode with 4 processes
|
2024-04-16 23:29:07 +02:00
|
|
|
rb"^-- Running tests in",
|
|
|
|
rb"^OK",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: Ran 2139 tests in 115.659s
|
2024-04-16 23:29:07 +02:00
|
|
|
rb"^Ran [0-9]+ tests in",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Destroying test database for alias 'default'...
|
2024-04-16 23:29:07 +02:00
|
|
|
rb"^Destroying test database for alias ",
|
|
|
|
rb"^Using existing clone",
|
|
|
|
rb"^\*\* Skipping ",
|
2020-08-19 12:40:10 +02:00
|
|
|
]
|
2024-04-16 23:29:07 +02:00
|
|
|
self.compiled_line_pattern = re.compile(rb"|".join(valid_line_patterns))
|
2021-12-23 06:55:47 +01:00
|
|
|
self.partial_line = b""
|
2021-12-28 22:32:36 +01:00
|
|
|
self.full_extra_output = bytearray()
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
def find_extra_output(self, data: bytes) -> None:
|
|
|
|
*lines, self.partial_line = (self.partial_line + data).split(b"\n")
|
2020-08-19 12:40:10 +02:00
|
|
|
for line in lines:
|
2021-12-23 06:55:47 +01:00
|
|
|
if not self.compiled_line_pattern.match(line):
|
|
|
|
self.full_extra_output += line + b"\n"
|
|
|
|
|
|
|
|
|
|
|
|
class WrappedIO(IO[bytes]):
|
|
|
|
def __init__(self, stream: IO[bytes], extra_output_finder: ExtraConsoleOutputFinder) -> None:
|
|
|
|
self.stream = stream
|
2020-08-19 12:40:10 +02:00
|
|
|
self.extra_output_finder = extra_output_finder
|
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
@property
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def mode(self) -> str:
|
|
|
|
return self.stream.mode
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
@property
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def name(self) -> str:
|
|
|
|
return self.stream.name
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def close(self) -> None:
|
|
|
|
pass
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
@property
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def closed(self) -> bool:
|
|
|
|
return self.stream.closed
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def fileno(self) -> int:
|
|
|
|
return self.stream.fileno()
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2020-08-19 12:40:10 +02:00
|
|
|
def flush(self) -> None:
|
2021-12-23 06:55:47 +01:00
|
|
|
self.stream.flush()
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def isatty(self) -> bool:
|
|
|
|
return self.stream.isatty()
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def read(self, n: int = -1) -> bytes:
|
|
|
|
return self.stream.read(n)
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def readable(self) -> bool:
|
|
|
|
return self.stream.readable()
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def readline(self, limit: int = -1) -> bytes:
|
|
|
|
return self.stream.readline(limit)
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-07-12 02:30:17 +02:00
|
|
|
def readlines(self, hint: int = -1) -> list[bytes]:
|
2021-12-23 06:55:47 +01:00
|
|
|
return self.stream.readlines(hint)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def seek(self, offset: int, whence: int = SEEK_SET) -> int:
|
|
|
|
return self.stream.seek(offset, whence)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def seekable(self) -> bool:
|
|
|
|
return self.stream.seekable()
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def tell(self) -> int:
|
|
|
|
return self.stream.tell()
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-07-12 02:30:23 +02:00
|
|
|
def truncate(self, size: int | None = None) -> int:
|
2021-12-23 06:55:47 +01:00
|
|
|
return self.truncate(size)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def writable(self) -> bool:
|
|
|
|
return self.stream.writable()
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-04-26 02:52:20 +02:00
|
|
|
def write(self, data: "ReadableBuffer") -> int:
|
2021-12-23 06:55:47 +01:00
|
|
|
num_chars = self.stream.write(data)
|
2023-04-26 02:52:20 +02:00
|
|
|
self.extra_output_finder.find_extra_output(bytes(data))
|
2021-12-23 06:55:47 +01:00
|
|
|
return num_chars
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-04-26 02:52:20 +02:00
|
|
|
def writelines(self, data: "Iterable[ReadableBuffer]") -> None:
|
2023-04-26 02:51:26 +02:00
|
|
|
data, data_copy = itertools.tee(data)
|
2021-12-23 06:55:47 +01:00
|
|
|
self.stream.writelines(data)
|
2023-04-26 02:51:26 +02:00
|
|
|
lines = b"".join(data_copy)
|
2020-08-19 12:40:10 +02:00
|
|
|
self.extra_output_finder.find_extra_output(lines)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def __next__(self) -> bytes:
|
|
|
|
return next(self.stream)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def __iter__(self) -> Iterator[bytes]:
|
|
|
|
return self
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def __enter__(self) -> IO[bytes]:
|
|
|
|
self.stream.__enter__()
|
|
|
|
return self
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-12-23 06:55:47 +01:00
|
|
|
def __exit__(
|
|
|
|
self,
|
2024-07-12 02:30:23 +02:00
|
|
|
exc_type: type[BaseException] | None,
|
|
|
|
exc_value: BaseException | None,
|
|
|
|
traceback: TracebackType | None,
|
2022-05-03 03:58:44 +02:00
|
|
|
) -> None:
|
|
|
|
self.stream.__exit__(exc_type, exc_value, traceback)
|
2021-12-23 06:55:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def tee_stderr_and_find_extra_console_output(
|
|
|
|
extra_output_finder: ExtraConsoleOutputFinder,
|
|
|
|
) -> Iterator[None]:
|
|
|
|
stderr = sys.stderr
|
|
|
|
|
|
|
|
# get shared console handler instance from any logger that have it
|
|
|
|
console_log_handler = logging.getLogger("django.server").handlers[0]
|
|
|
|
assert isinstance(console_log_handler, logging.StreamHandler)
|
|
|
|
assert console_log_handler.stream == stderr
|
|
|
|
|
|
|
|
sys.stderr = console_log_handler.stream = TextIOWrapper(
|
|
|
|
WrappedIO(stderr.buffer, extra_output_finder), line_buffering=True
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
sys.stderr.flush()
|
|
|
|
finally:
|
|
|
|
sys.stderr = console_log_handler.stream = stderr
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def tee_stdout_and_find_extra_console_output(
|
|
|
|
extra_output_finder: ExtraConsoleOutputFinder,
|
|
|
|
) -> Iterator[None]:
|
|
|
|
stdout = sys.stdout
|
|
|
|
sys.stdout = TextIOWrapper(
|
|
|
|
WrappedIO(sys.stdout.buffer, extra_output_finder), line_buffering=True
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
sys.stdout.flush()
|
|
|
|
finally:
|
|
|
|
sys.stdout = stdout
|