diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1b4bbc6dd..bbf4ad9e2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -60,7 +60,7 @@ jobs: steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - - run: pip install $(grep tomli== requirements-tests.txt) $(grep mypy== requirements-tests.txt) termcolor + - run: pip install $(grep tomli== requirements-tests.txt) $(grep mypy== requirements-tests.txt) - run: ./tests/mypy_test.py --platform=${{ matrix.platform }} --python-version=${{ matrix.python-version }} pyright: diff --git a/tests/colors.py b/tests/colors.py deleted file mode 100644 index 9f81be7cf..000000000 --- a/tests/colors.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Helper module so we don't have to install types-termcolor in CI. - -This is imported by `mypy_test.py` and `stubtest_third_party.py`. -""" - -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - - def colored(__str: str, __style: str) -> str: - ... - -else: - try: - from termcolor import colored - except ImportError: - - def colored(s: str, _: str) -> str: - return s - - -def print_error(error: str, end: str = "\n") -> None: - error_split = error.split("\n") - for line in error_split[:-1]: - print(colored(line, "red")) - print(colored(error_split[-1], "red"), end=end) - - -def print_success_msg() -> None: - print(colored("success", "green")) diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 5571940ff..cb54189bd 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -19,8 +19,6 @@ import subprocess import sys import tempfile from collections.abc import Iterable -from contextlib import redirect_stdout -from io import StringIO from pathlib import Path from typing import TYPE_CHECKING, NamedTuple @@ -30,7 +28,6 @@ if TYPE_CHECKING: from typing_extensions import TypeAlias import tomli -from colors import colored, print_error, print_success_msg parser = argparse.ArgumentParser(description="Test runner for typeshed. Patterns are unanchored regexps on the full path.") parser.add_argument("-v", "--verbose", action="count", default=0, help="More output") @@ -168,7 +165,7 @@ def run_mypy( try: from mypy.api import run as mypy_run except ImportError: - print_error("Cannot import mypy. Did you install it?") + print("Cannot import mypy. Did you install it?") sys.exit(1) with tempfile.NamedTemporaryFile("w+") as temp: @@ -186,14 +183,9 @@ def run_mypy( if args.dry_run: exit_code = 0 else: - stdout_file = StringIO() - with redirect_stdout(stdout_file): - stdout, stderr, exit_code = mypy_run(mypy_args) - if exit_code: - if stderr: - print_error(stderr) - if stdout: - print_error(stdout_file.getvalue(), end="") + stdout, stderr, exit_code = mypy_run(mypy_args) + print(stdout, end="") + print(stderr, end="") return exit_code @@ -202,11 +194,8 @@ ReturnCode: TypeAlias = int def run_mypy_as_subprocess(directory: StrPath, flags: Iterable[str]) -> ReturnCode: result = subprocess.run([sys.executable, "-m", "mypy", directory, *flags], capture_output=True) - stdout, stderr = result.stdout, result.stderr - if stderr: - print_error(stderr.decode()) - if stdout: - print_error(stdout.decode()) + print(result.stdout.decode()) + print(result.stderr.decode()) return result.returncode @@ -245,7 +234,6 @@ def get_mypy_flags( if strict: flags.append("--strict") if test_suite_run: - flags.append("--namespace-packages") if sys.platform == "win32" or args.platform == "win32": flags.extend(["--exclude", "tests/pytype_test.py"]) else: @@ -303,17 +291,13 @@ def test_third_party_distribution(distribution: str, major: int, minor: int, arg seen_dists: set[str] = set() add_third_party_files(distribution, major, files, args, configurations, seen_dists) - print(f"testing {distribution} ({len(files)} files)... ", end="") + print(f"testing {distribution} ({len(files)} files)...") if not files: - print_error("no files found") + print("--- no files found ---") sys.exit(1) code = run_mypy(args, configurations, major, minor, files) - - if not code: - print_success_msg() - return code, len(files) @@ -354,9 +338,6 @@ def test_stdlib(code: int, major: int, minor: int, args: argparse.Namespace) -> this_code = run_mypy(args, [], major, minor, files, custom_typeshed=True) code = max(code, this_code) - if not code: - print_success_msg() - return TestResults(code, len(files)) @@ -394,8 +375,6 @@ def test_the_test_scripts(code: int, major: int, minor: int, args: argparse.Name else: this_code = run_mypy_as_subprocess("tests", flags) code = max(code, this_code) - if not code: - print_success_msg() return TestResults(code, num_test_files_to_test) @@ -414,8 +393,6 @@ def test_the_test_cases(code: int, major: int, minor: int, args: argparse.Namesp shutil.copytree(Path("test_cases"), Path(td) / "test_cases") this_code = run_mypy_as_subprocess(td, flags) code = max(code, this_code) - if not code: - print_success_msg() return TestResults(code, num_test_case_files) @@ -456,7 +433,7 @@ def main() -> None: if args.python_version: versions = [v for v in versions if any(("%d.%d" % v).startswith(av) for av in args.python_version)] if not versions: - print_error("--- no versions selected ---") + print("--- no versions selected ---") sys.exit(1) code = 0 @@ -465,17 +442,13 @@ def main() -> None: code, files_checked_this_version = test_typeshed(code, major, minor, args) total_files_checked += files_checked_this_version if code: - print_error(f"--- exit status {code}, {total_files_checked} files checked ---") + print(f"--- exit status {code}, {total_files_checked} files checked ---") sys.exit(code) if not total_files_checked: - print_error("--- nothing to do; exit 1 ---") + print("--- nothing to do; exit 1 ---") sys.exit(1) - print(colored(f"--- success, {total_files_checked} files checked ---", "green")) + print(f"--- success, {total_files_checked} files checked ---") if __name__ == "__main__": - try: - main() - except KeyboardInterrupt: - print_error("\n\n!!!\nTest aborted due to KeyboardInterrupt\n!!!") - sys.exit(1) + main() diff --git a/tests/stubtest_third_party.py b/tests/stubtest_third_party.py index de68552e1..b3d350d2b 100755 --- a/tests/stubtest_third_party.py +++ b/tests/stubtest_third_party.py @@ -10,10 +10,22 @@ import sys import tempfile import venv from pathlib import Path -from typing import NoReturn +from typing import TYPE_CHECKING, NoReturn import tomli -from colors import colored, print_error, print_success_msg + +if TYPE_CHECKING: + + def colored(__str: str, __style: str) -> str: + ... + +else: + try: + from termcolor import colored + except ImportError: + + def colored(s: str, _: str) -> str: + return s @functools.lru_cache() @@ -26,10 +38,10 @@ def run_stubtest(dist: Path, *, verbose: bool = False) -> bool: with open(dist / "METADATA.toml") as f: metadata = dict(tomli.loads(f.read())) - print(f"{dist.name}... ", end="") + print(f"{dist.name}...", end="") if not metadata.get("stubtest", True): - print(colored("skipping", "yellow")) + print(colored(" skipping", "yellow")) return True with tempfile.TemporaryDirectory() as tmp: @@ -88,7 +100,7 @@ def run_stubtest(dist: Path, *, verbose: bool = False) -> bool: try: subprocess.run(stubtest_cmd, env={"MYPYPATH": str(dist), "MYPY_FORCE_COLOR": "1"}, check=True, capture_output=True) except subprocess.CalledProcessError as e: - print_error(" fail") + print(colored(" fail", "red")) print_commands(dist, pip_cmd, stubtest_cmd) print_command_output(e) @@ -108,7 +120,7 @@ def run_stubtest(dist: Path, *, verbose: bool = False) -> bool: return False else: - print_success_msg() + print(colored(" success", "green")) if verbose: print_commands(dist, pip_cmd, stubtest_cmd) @@ -124,7 +136,7 @@ def print_commands(dist: Path, pip_cmd: list[str], stubtest_cmd: list[str]) -> N def print_command_failure(message: str, e: subprocess.CalledProcessError) -> None: - print_error("fail") + print(colored(" fail", "red")) print(file=sys.stderr) print(message, file=sys.stderr) print_command_output(e)