From bbf7039d4fb5c0aa3a72d1e807af451be96f6546 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 14 Apr 2024 17:41:55 +0100 Subject: [PATCH 1/3] Improve the output of `tests/stubtest_third_party.py` when it fails --- scripts/stubsabot.py | 1 + tests/stubtest_third_party.py | 29 +++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/scripts/stubsabot.py b/scripts/stubsabot.py index 5b88e26ec757..19fe1fcd66f0 100644 --- a/scripts/stubsabot.py +++ b/scripts/stubsabot.py @@ -498,6 +498,7 @@ async def determine_action(stub_path: Path, session: aiohttp.ClientSession) -> U "Release": f"{pypi_info.pypi_root}/{relevant_version}", "Homepage": project_urls.get("Homepage"), "Repository": stub_info.upstream_repository, + "Typeshed stubs": f"https://github.com/{TYPESHED_OWNER}/typeshed/tree/main/{stub_info.distribution}", "Changelog": project_urls.get("Changelog") or project_urls.get("Changes") or project_urls.get("Change Log"), } links = {k: v for k, v in maybe_links.items() if v is not None} diff --git a/tests/stubtest_third_party.py b/tests/stubtest_third_party.py index fc8d51b75abf..e803aa992fd3 100755 --- a/tests/stubtest_third_party.py +++ b/tests/stubtest_third_party.py @@ -24,7 +24,7 @@ def run_stubtest( metadata = read_metadata(dist_name) except NoSuchStubError as e: parser.error(str(e)) - print(f"{dist_name}... ", end="") + print(f"{dist_name}... ", end="", flush=True) stubtest_settings = metadata.stubtest_settings if stubtest_settings.skipped: @@ -131,28 +131,45 @@ def run_stubtest( try: subprocess.run(stubtest_cmd, env=stubtest_env, check=True, capture_output=True) except subprocess.CalledProcessError as e: - print_error("fail") + print_error("fail\n") + divider = "*" * 70 + + print(divider) + print("Commands run:") print_commands(dist, pip_cmd, stubtest_cmd, mypypath) + + print(divider) + print("Command output:\n") print_command_output(e) - print("Python version: ", file=sys.stderr) + 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) ret = subprocess.run([sys.executable, "-VV"], capture_output=True) print_command_output(ret) - print("Ran with the following environment:", file=sys.stderr) ret = subprocess.run([pip_exe, "freeze", "--all"], capture_output=True) print_command_output(ret) + 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}', file=sys.stderr + f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_path_relative}', + file=sys.stderr ) print(file=sys.stderr) else: - print(f"Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path}:", file=sys.stderr) + print( + f"Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path_relative}:", + file=sys.stderr + ) ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True) print_command_output(ret) + print(divider) + return False else: print_success_msg() From 3debcd7d5b225b1df6c118f4025e95aedb4e7758 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 14 Apr 2024 16:57:42 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks --- tests/stubtest_third_party.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/stubtest_third_party.py b/tests/stubtest_third_party.py index e803aa992fd3..ea1e9e16fb88 100755 --- a/tests/stubtest_third_party.py +++ b/tests/stubtest_third_party.py @@ -157,13 +157,13 @@ def run_stubtest( if allowlist_path.exists(): print( f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_path_relative}', - file=sys.stderr + file=sys.stderr, ) print(file=sys.stderr) else: print( f"Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path_relative}:", - file=sys.stderr + file=sys.stderr, ) ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True) print_command_output(ret) From 6e0f246cd415cfa0e6c41a9eae021cbc96fbde31 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 14 Apr 2024 23:19:40 +0100 Subject: [PATCH 3/3] Add a dedicated helper function --- tests/stubtest_third_party.py | 11 +++++------ tests/utils.py | 8 ++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/stubtest_third_party.py b/tests/stubtest_third_party.py index ea1e9e16fb88..e2e27fcafa81 100755 --- a/tests/stubtest_third_party.py +++ b/tests/stubtest_third_party.py @@ -13,7 +13,7 @@ from typing import NoReturn from parse_metadata import NoSuchStubError, get_recursive_requirements, read_metadata -from utils import PYTHON_VERSION, colored, get_mypy_req, print_error, print_success_msg +from utils import PYTHON_VERSION, colored, get_mypy_req, print_divider, print_error, print_success_msg def run_stubtest( @@ -132,17 +132,16 @@ def run_stubtest( subprocess.run(stubtest_cmd, env=stubtest_env, check=True, capture_output=True) except subprocess.CalledProcessError as e: print_error("fail\n") - divider = "*" * 70 - print(divider) + print_divider() print("Commands run:") print_commands(dist, pip_cmd, stubtest_cmd, mypypath) - print(divider) + print_divider() print("Command output:\n") print_command_output(e) - print(divider) + print_divider() print(f"Upstream repository: {metadata.upstream_repository}") print(f"Typeshed source code: https://github.com/python/typeshed/tree/main/stubs/{dist.name}") @@ -168,7 +167,7 @@ def run_stubtest( ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True) print_command_output(ret) - print(divider) + print_divider() return False else: diff --git a/tests/utils.py b/tests/utils.py index cf608b9b4c28..2bd58d632cd3 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -56,6 +56,14 @@ def print_success_msg() -> None: 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("*" * 70) + + # ==================================================================== # Dynamic venv creation # ====================================================================