2020-08-19 12:40:10 +02:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import sys
|
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
|
2021-12-23 06:55:47 +01:00
|
|
|
from typing import IO, Iterable, Iterator, List, Optional, Type
|
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
|
2021-12-23 06:55:47 +01:00
|
|
|
b"^Running ",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: ** Test is TOO slow: analytics.tests.test_counts.TestRealmActiveHumans.test_end_to_end (0.581 s)
|
2021-12-23 06:55:47 +01:00
|
|
|
b"^\\*\\* Test is TOO slow: ",
|
|
|
|
b"^----------------------------------------------------------------------",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: INFO: URL coverage report is in var/url_coverage.txt
|
2021-12-23 06:55:47 +01:00
|
|
|
b"^INFO: URL coverage report is in",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: INFO: Try running: ./tools/create-test-api-docs
|
2021-12-23 06:55:47 +01:00
|
|
|
b"^INFO: Try running:",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: -- Running tests in parallel mode with 4 processes
|
2021-12-23 06:55:47 +01:00
|
|
|
b"^-- Running tests in",
|
|
|
|
b"^OK",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Example: Ran 2139 tests in 115.659s
|
2021-12-23 06:55:47 +01:00
|
|
|
b"^Ran [0-9]+ tests in",
|
2020-08-19 12:40:10 +02:00
|
|
|
# Destroying test database for alias 'default'...
|
2021-12-23 06:55:47 +01:00
|
|
|
b"^Destroying test database for alias ",
|
|
|
|
b"^Using existing clone",
|
|
|
|
b"^\\*\\* Skipping ",
|
2020-08-19 12:40:10 +02:00
|
|
|
]
|
2021-12-23 06:55:47 +01:00
|
|
|
self.compiled_line_pattern = re.compile(b"|".join(valid_line_patterns))
|
|
|
|
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
|
|
|
|
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
|
|
|
|
def name(self) -> str:
|
|
|
|
return self.stream.name
|
2020-08-19 12:40:10 +02:00
|
|
|
|
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
|
|
|
|
def closed(self) -> bool:
|
|
|
|
return self.stream.closed
|
|
|
|
|
|
|
|
def fileno(self) -> int:
|
|
|
|
return self.stream.fileno()
|
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
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
def isatty(self) -> bool:
|
|
|
|
return self.stream.isatty()
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
def readable(self) -> bool:
|
|
|
|
return self.stream.readable()
|
2020-08-19 12:40:10 +02:00
|
|
|
|
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
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
def readlines(self, hint: int = -1) -> List[bytes]:
|
|
|
|
return self.stream.readlines(hint)
|
|
|
|
|
|
|
|
def seek(self, offset: int, whence: int = SEEK_SET) -> int:
|
|
|
|
return self.stream.seek(offset, whence)
|
|
|
|
|
|
|
|
def seekable(self) -> bool:
|
|
|
|
return self.stream.seekable()
|
|
|
|
|
|
|
|
def tell(self) -> int:
|
|
|
|
return self.stream.tell()
|
|
|
|
|
|
|
|
def truncate(self, size: Optional[int] = None) -> int:
|
|
|
|
return self.truncate(size)
|
|
|
|
|
|
|
|
def writable(self) -> bool:
|
|
|
|
return self.stream.writable()
|
|
|
|
|
|
|
|
def write(self, data: bytes) -> int:
|
|
|
|
num_chars = self.stream.write(data)
|
2020-08-19 12:40:10 +02:00
|
|
|
self.extra_output_finder.find_extra_output(data)
|
2021-12-23 06:55:47 +01:00
|
|
|
return num_chars
|
2020-08-19 12:40:10 +02:00
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
def writelines(self, data: Iterable[bytes]) -> None:
|
|
|
|
self.stream.writelines(data)
|
|
|
|
lines = b"".join(data)
|
2020-08-19 12:40:10 +02:00
|
|
|
self.extra_output_finder.find_extra_output(lines)
|
|
|
|
|
2021-12-23 06:55:47 +01:00
|
|
|
def __next__(self) -> bytes:
|
|
|
|
return next(self.stream)
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator[bytes]:
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __enter__(self) -> IO[bytes]:
|
|
|
|
self.stream.__enter__()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(
|
|
|
|
self,
|
|
|
|
exc_type: Optional[Type[BaseException]],
|
|
|
|
exc_value: Optional[BaseException],
|
|
|
|
traceback: Optional[TracebackType],
|
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
|