Skip to content
Open
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
120 changes: 120 additions & 0 deletions Lib/test/test_asyncio/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""Tests for the asyncio command-line interface (python -m asyncio)."""

import runpy
import sys
import unittest
import unittest.mock

from asyncio import tools

from test.support.script_helper import (
assert_python_failure,
assert_python_ok,
)


class TestAsyncioCLI(unittest.TestCase):

def run_dispatch(self, *args):
argv = ["python -m asyncio", *args]
with unittest.mock.patch.object(sys, "argv", argv):
with self.assertRaises(SystemExit) as cm:
runpy.run_module("asyncio.__main__", run_name="__main__")
self.assertEqual(cm.exception.code, 0)

def test_help(self):
_, stdout, _ = assert_python_ok("-m", "asyncio", "--help")
self.assertIn(b"ps", stdout)
self.assertIn(b"pstree", stdout)

def test_subcommand_help(self):
for command in ("ps", "pstree"):
with self.subTest(command=command):
_, stdout, _ = assert_python_ok(
"-m", "asyncio", command, "--help"
)
self.assertIn(b"Process ID to inspect", stdout)
self.assertIn(b"pid", stdout)

def test_missing_pid(self):
for command in ("ps", "pstree"):
with self.subTest(command=command):
_, _, stderr = assert_python_failure(
"-m", "asyncio", command
)
self.assertIn(b"required", stderr)

def test_invalid_pid(self):
for command in ("ps", "pstree"):
with self.subTest(command=command):
_, _, stderr = assert_python_failure(
"-m", "asyncio", command, "invalid"
)
self.assertIn(b"invalid", stderr)

def test_unknown_subcommand(self):
_, _, stderr = assert_python_failure("-m", "asyncio", "unknown")
self.assertIn(b"invalid choice", stderr)

def test_command_dispatch(self):
expected_kwargs = (
{"retries": 3}
if sys.version_info >= (3, 15)
else {}
)

for command, display_function in (
("ps", "display_awaited_by_tasks_table"),
("pstree", "display_awaited_by_tasks_tree"),
):
with self.subTest(command=command):
with unittest.mock.patch.object(
tools, display_function
) as display:
self.run_dispatch(command, "1234")
display.assert_called_once_with(1234, **expected_kwargs)

@unittest.skipIf(
sys.version_info < (3, 15),
"--retries was added in Python 3.15",
)
def test_custom_retries(self):
for command, display_function in (
("ps", "display_awaited_by_tasks_table"),
("pstree", "display_awaited_by_tasks_tree"),
):
with self.subTest(command=command):
with unittest.mock.patch.object(
tools, display_function
) as display:
self.run_dispatch(command, "1234", "--retries", "5")
display.assert_called_once_with(1234, retries=5)

@unittest.skipIf(
sys.version_info < (3, 15),
"--retries was added in Python 3.15",
)
def test_invalid_retries(self):
for command in ("ps", "pstree"):
with self.subTest(command=command):
_, _, stderr = assert_python_failure(
"-m", "asyncio", command, "1234",
"--retries", "invalid",
)
self.assertIn(b"invalid", stderr)

@unittest.skipIf(
sys.version_info < (3, 15),
"--retries was added in Python 3.15",
)
def test_subcommand_help_shows_retries(self):
for command in ("ps", "pstree"):
with self.subTest(command=command):
_, stdout, _ = assert_python_ok(
"-m", "asyncio", command, "--help"
)
self.assertIn(b"--retries", stdout)


if __name__ == "__main__":
unittest.main()
Loading