|
1 | 1 | from functools import partial |
2 | | -from subprocess import run |
3 | | -from typing import Any |
4 | | - |
| 2 | +import subprocess |
| 3 | +from typing import TYPE_CHECKING, Any |
| 4 | +import sys |
5 | 5 | import pytest |
6 | 6 |
|
7 | | -# TODO Would like help with this test, how do I make it runnable? |
| 7 | +#XXX Would like help with this test, how do I make it runnable? |
| 8 | + |
| 9 | +# Haven't run this test yet |
| 10 | + |
| 11 | +PTY_SIZE = (80, 40) |
8 | 12 |
|
| 13 | +if sys.platform == "win32": |
| 14 | + if TYPE_CHECKING: |
| 15 | + from winpty.winpty import PTY |
| 16 | + else: |
| 17 | + from winpty import PTY |
9 | 18 |
|
10 | | -def test(expect_color: bool, *args: Any, **kwargs: Any) -> None: |
11 | | - res = run(*args, capture_output=True, **kwargs) |
12 | | - if "Found" not in res.stdout: # ?? |
| 19 | + def run_pty(cmd: str, env: dict[str, str] = {}) -> tuple[str, str]: |
| 20 | + pty = PTY(*PTY_SIZE) |
| 21 | + # For the purposes of this test, str.split() is enough |
| 22 | + appname, cmdline = cmd.split(maxsplit=1) |
| 23 | + pty.spawn(appname, cmdline, "\0".join(map(lambda kv: f"{kv[0]}={kv[1]}", env.items()))) |
| 24 | + while pty.isalive(): |
| 25 | + pass |
| 26 | + return pty.read(), pty.read_stderr() |
| 27 | +elif sys.platform == "unix": |
| 28 | + from pty import openpty |
| 29 | + |
| 30 | + def run_pty(cmd: str, env: dict[str, str] = {}) -> tuple[str, str]: |
| 31 | + # TODO Would like help checking quality of this function, |
| 32 | + # it's partially written by Copilot because I'm not familiar with Unix openpty |
| 33 | + master_fd, slave_fd = openpty() |
| 34 | + try: |
| 35 | + p = subprocess.run(cmd, stdout=slave_fd, stderr=subprocess.PIPE, env=env, text=True) |
| 36 | + os.close(slave_fd) |
| 37 | + return os.read(slave_fd, 10000).decode(), p.stderr |
| 38 | + finally: |
| 39 | + os.close(master_fd) |
| 40 | +def test(expect_color: bool, pty: bool, cmd: str, env: dict[str, str] = {}) -> None: |
| 41 | + if pty: |
| 42 | + stdout, stderr = run_pty(cmd, env=env) |
| 43 | + else: |
| 44 | + proc = subprocess.run(cmd, capture_output=True, env=env, text=True) |
| 45 | + stdout = proc.stdout |
| 46 | + stderr = proc.stderr |
| 47 | + if "Found" not in stdout: # ?? |
13 | 48 | pytest.fail("Command failed to complete or did not detect type error") |
14 | 49 | if expect_color: # Expect color control chars |
15 | | - assert "<string>:1: error:" not in res.stdout |
16 | | - assert "\nFound" not in res.stdout |
| 50 | + assert "<string>:1: error:" not in stdout |
| 51 | + assert "\nFound" not in stdout |
17 | 52 | else: # Expect no color control chars |
18 | | - assert "<string>:1: error:" in res.stdout |
19 | | - assert "\nFound" in res.stdout |
| 53 | + assert "<string>:1: error:" in stdout |
| 54 | + assert "\nFound" in stdout |
| 55 | + |
20 | 56 |
|
| 57 | +def test_pty(expect_color: bool, cmd: str, env: dict[str, str] = {}) -> None: |
| 58 | + test(expect_color, True, cmd, env) |
21 | 59 |
|
22 | | -colored = partial(test, True) |
23 | | -not_colored = partial(test, False) |
| 60 | +def test_not_pty(expect_color: bool, cmd: str, env: dict[str, str] = {}) -> None: |
| 61 | + test(expect_color, False, cmd, env) |
24 | 62 |
|
25 | 63 |
|
26 | 64 | @pytest.mark.parametrize("command", ["mypy", "dmypy run --"]) |
27 | | -def test_color_output(command: str) -> None: |
| 65 | +def test_it(command: str) -> None: |
28 | 66 | # Note: Though we don't check stderr, capturing it is useful |
29 | 67 | # because it provides traceback if mypy crashes due to exception |
30 | 68 | # and pytest reveals it upon failure (?) |
31 | | - not_colored(f"{command} -c \"1+'a'\"") |
32 | | - colored(f"{command} -c \"1+'a'\"", env={"MYPY_FORCE_COLOR": "1"}) |
33 | | - colored(f"{command} -c \"1+'a'\" --color-output") |
34 | | - not_colored(f"{command} -c \"1+'a'\" --no-color-output") |
35 | | - colored(f"{command} -c \"1+'a'\" --no-color-output", env={"MYPY_FORCE_COLOR": "1"}) # TODO |
| 69 | + test_pty(True, "mypy -c \"1+'a'\" --color-output=force") |
| 70 | + test_pty(False, "mypy -c \"1+'a'\" --no-color-output") |
| 71 | + test_not_pty(False, "mypy -c \"1+'a'\" --color-output") |
| 72 | + test_not_pty(True, "mypy -c \"1+'a'\" --color-output=force") |
| 73 | + test_not_pty(False, "mypy -c \"1+'a'\" --color-output", {"MYPY_FORCE_COLOR": "1"}) |
| 74 | + test_not_pty(True, "mypy -c \"1+'a'\" --color-output=force", {"MYPY_FORCE_COLOR": "1"}) |
| 75 | + test_not_pty(False, "mypy -c \"1+'a'\" --no-color-output", {"MYPY_FORCE_COLOR": "1"}) |
| 76 | + test_not_pty(False, "mypy -c \"1+'a'\" --no-color-output", {"FORCE_COLOR": "1"}) |
| 77 | + test_not_pty(False, "mypy -c \"1+'a'\" --color-output", {"MYPY_FORCE_COLOR": "0"}) |
36 | 78 |
|
37 | 79 |
|
38 | 80 | # TODO: Tests in the terminal (require manual testing?) |
|
0 commit comments