Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions tests/stubtest_third_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,42 +131,38 @@ def run_stubtest(
try:
subprocess.run(stubtest_cmd, env=stubtest_env, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
print_error("fail\n")
print_error("fail")

print_divider()
print("Commands run:", file=sys.stderr)
print("Commands run:")
print_commands(dist, pip_cmd, stubtest_cmd, mypypath)

print_divider()
print("Command output:\n", file=sys.stderr)
print("Command output:\n")
print_command_output(e)

print_divider()
print(f"Upstream repository: {metadata.upstream_repository}")
print(f"Typeshed source code: https://github.com/python/typeshed/tree/main/stubs/{dist.name}")

print("Python version: ", file=sys.stderr, end="", flush=True)
print("Python version: ", end="", flush=True)
ret = subprocess.run([sys.executable, "-VV"], capture_output=True)
print_command_output(ret)
print("Ran with the following environment:", file=sys.stderr)
print("\nRan with the following environment:")
ret = subprocess.run([pip_exe, "freeze", "--all"], capture_output=True)
print_command_output(ret)

print_divider()
allowlist_path_relative = allowlist_path.relative_to(Path.cwd())
if allowlist_path.exists():
print(
f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_path_relative}',
file=sys.stderr,
)
print(file=sys.stderr)
print(f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_path_relative}')
print()
else:
print(
f"Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path_relative}:",
file=sys.stderr,
)
print(f"Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path_relative}:")
ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True)
print_command_output(ret)

print_divider()
print(f"Upstream repository: {metadata.upstream_repository}")
print(f"Typeshed source code: https://github.com/python/typeshed/tree/main/stubs/{dist.name}")

print_divider()

return False
Expand Down Expand Up @@ -338,23 +334,21 @@ def setup_uwsgi_stubtest_command(dist: Path, venv_dir: Path, stubtest_cmd: list[


def print_commands(dist: Path, pip_cmd: list[str], stubtest_cmd: list[str], mypypath: str) -> None:
print(file=sys.stderr)
print(" ".join(pip_cmd), file=sys.stderr)
print(f"MYPYPATH={mypypath}", " ".join(stubtest_cmd), file=sys.stderr)
print(file=sys.stderr)
print()
print(" ".join(pip_cmd))
print(f"MYPYPATH={mypypath}", " ".join(stubtest_cmd))


def print_command_failure(message: str, e: subprocess.CalledProcessError) -> None:
print_error("fail")
print(file=sys.stderr)
print(message, file=sys.stderr)
print()
print(message)
print_command_output(e)


def print_command_output(e: subprocess.CalledProcessError | subprocess.CompletedProcess[bytes]) -> None:
print(e.stdout.decode(), end="", file=sys.stderr)
print(e.stderr.decode(), end="", file=sys.stderr)
print(file=sys.stderr)
print(e.stdout.decode(), end="")
print(e.stderr.decode(), end="")


def main() -> NoReturn:
Expand Down
14 changes: 7 additions & 7 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,29 @@ def strip_comments(text: str) -> str:
def print_command(cmd: str | Iterable[str]) -> None:
if not isinstance(cmd, str):
cmd = " ".join(cmd)
print(colored(f"Running: {cmd}", "blue"), file=sys.stderr)
print(colored(f"Running: {cmd}", "blue"))


def print_error(error: str, end: str = "\n", fix_path: tuple[str, str] = ("", "")) -> None:
error_split = error.split("\n")
old, new = fix_path
for line in error_split[:-1]:
print(colored(line.replace(old, new), "red"), file=sys.stderr)
print(colored(error_split[-1], "red"), end=end, file=sys.stderr)
print(colored(line.replace(old, new), "red"))
print(colored(error_split[-1], "red"), end=end)


def print_success_msg() -> None:
print(colored("success", "green"), file=sys.stderr)
print(colored("success", "green"))


def print_divider() -> None:
"""Print a row of * symbols across the screen.

This can be useful to divide terminal output into separate sections.
"""
print(file=sys.stderr)
print("*" * 70, file=sys.stderr)
print(file=sys.stderr)
print()
print("*" * 70)
print()


# ====================================================================
Expand Down