| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| import asyncio.subprocess | |
| import contextlib | |
| import inspect | |
| import io | |
| import sys | |
| import unittest | |
| from collections.abc import AsyncIterator | |
| from unittest import mock | |
| import cli.cli | |
| import main | |
| @contextlib.asynccontextmanager | |
| async def _noop_package_server() -> AsyncIterator[None]: | |
| yield | |
| class TestMain(unittest.TestCase): | |
| def test_main_is_synchronous_function(self) -> None: | |
| self.assertFalse(inspect.iscoroutinefunction(main.main)) | |
| @mock.patch.object(cli.cli, "main", autospec=True, spec_set=True) | |
| def test_main_cli_propagates_exit_code( | |
| self, mock_cli_main: mock.AsyncMock | |
| ) -> None: | |
| for expected_code in (0, 1, 2): | |
| with self.subTest(expected_code=expected_code): | |
| mock_cli_main.reset_mock() | |
| mock_cli_main.return_value = expected_code | |
| exit_code = main.main(("cli", "status")) | |
| self.assertEqual(exit_code, expected_code) | |
| mock_cli_main.assert_awaited_once_with(["status"]) | |
| @mock.patch.object(cli.cli, "main", autospec=True, spec_set=True) | |
| @mock.patch.object(sys, "argv", ("fx-debug", "cli", "status")) | |
| def test_main_defaults_to_sys_argv( | |
| self, mock_cli_main: mock.AsyncMock | |
| ) -> None: | |
| mock_cli_main.return_value = 3 | |
| exit_code = main.main() | |
| self.assertEqual(exit_code, 3) | |
| mock_cli_main.assert_awaited_once_with(["status"]) | |
| @mock.patch.object( | |
| asyncio.subprocess, | |
| "create_subprocess_exec", | |
| autospec=True, | |
| spec_set=True, | |
| ) | |
| @mock.patch.object(cli.cli, "main", autospec=True, spec_set=True) | |
| @mock.patch.object(main, "importlib", autospec=True, spec_set=True) | |
| def test_main_help_returns_zero( | |
| self, | |
| mock_importlib: mock.Mock, | |
| mock_cli_main: mock.AsyncMock, | |
| mock_create_subprocess: mock.AsyncMock, | |
| ) -> None: | |
| for flag in ("--help", "-h"): | |
| with self.subTest(flag=flag): | |
| stdout = io.StringIO() | |
| with mock.patch.object(sys, "stdout", stdout): | |
| exit_code = main.main((flag,)) | |
| self.assertEqual(exit_code, 0) | |
| mock_importlib.import_module.assert_not_called() | |
| mock_cli_main.assert_not_called() | |
| mock_create_subprocess.assert_not_called() | |
| @mock.patch.object( | |
| asyncio.subprocess, | |
| "create_subprocess_exec", | |
| autospec=True, | |
| spec_set=True, | |
| ) | |
| @mock.patch.object(main, "package_server", autospec=True, spec_set=True) | |
| def test_main_interactive_fallback_propagates_exit_code( | |
| self, | |
| mock_package_server: mock.Mock, | |
| mock_create_subprocess: mock.AsyncMock, | |
| ) -> None: | |
| mock_package_server.run_and_stop_on_exit.side_effect = ( | |
| _noop_package_server | |
| ) | |
| for expected_code in (0, 42): | |
| with self.subTest(expected_code=expected_code): | |
| mock_package_server.run_and_stop_on_exit.reset_mock() | |
| mock_create_subprocess.reset_mock() | |
| mock_proc = mock.create_autospec( | |
| asyncio.subprocess.Process, instance=True, spec_set=True | |
| ) | |
| mock_proc.wait.return_value = expected_code | |
| mock_create_subprocess.return_value = mock_proc | |
| exit_code = main.main(("--target", "emulator")) | |
| self.assertEqual(exit_code, expected_code) | |
| mock_package_server.run_and_stop_on_exit.assert_called_once_with() | |
| mock_create_subprocess.assert_awaited_once_with( | |
| "ffx", | |
| "debug", | |
| "connect", | |
| "--target", | |
| "emulator", | |
| stdin=sys.stdin, | |
| ) | |
| mock_proc.wait.assert_awaited_once_with() | |
| if __name__ == "__main__": | |
| unittest.main() |