From e47a6206050689d79276a9e7b151a9af99a83b25 Mon Sep 17 00:00:00 2001 From: Bhuvansh Kataria Date: Thu, 3 Dec 2026 22:29:27 +0000 Subject: [PATCH] gh-131178: add asyncio CLI tests --- Lib/test/test_asyncio/test_cli.py | 120 ++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 Lib/test/test_asyncio/test_cli.py diff --git a/Lib/test/test_asyncio/test_cli.py b/Lib/test/test_asyncio/test_cli.py new file mode 100644 index 00000000000000..31bb8b4dbf96d0 --- /dev/null +++ b/Lib/test/test_asyncio/test_cli.py @@ -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()