From bc0f2d20e484d37868504bceb67efc61bbe8dd69 Mon Sep 17 00:00:00 2001 From: rickwu4444 Date: Wed, 7 Aug 2024 15:22:27 +0800 Subject: [PATCH] Modify logic and add more unittest to increase code coverage --- providers/resource/bin/wifi_interface_mode.py | 7 ++----- .../resource/tests/test_wifi_interface_mode.py | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/providers/resource/bin/wifi_interface_mode.py b/providers/resource/bin/wifi_interface_mode.py index c54e0234f2..e073120083 100755 --- a/providers/resource/bin/wifi_interface_mode.py +++ b/providers/resource/bin/wifi_interface_mode.py @@ -11,7 +11,7 @@ def run_command(command: List[str]) -> str: ) return result.stdout.decode("utf-8") except Exception as e: - raise SystemError("An unexpected error occurred: {}", e) + raise SystemError("An unexpected error occurred: {}".format(e)) def get_interfaces() -> List[Tuple[str, str]]: @@ -20,7 +20,7 @@ def get_interfaces() -> List[Tuple[str, str]]: if output: matches = re.findall(interface_pattern, output, re.DOTALL) return [(phy, interface) for phy, interface in matches] - return [] + raise SystemExit(0) def get_wiphy_info() -> List[Tuple[str, List[str]]]: @@ -59,9 +59,6 @@ def print_supported_modes( def main(): interfaces = get_interfaces() - if not interfaces: - print("") - return wiphy_info = get_wiphy_info() print_supported_modes(interfaces, wiphy_info) diff --git a/providers/resource/tests/test_wifi_interface_mode.py b/providers/resource/tests/test_wifi_interface_mode.py index aa07292b25..4ff6f38dc5 100755 --- a/providers/resource/tests/test_wifi_interface_mode.py +++ b/providers/resource/tests/test_wifi_interface_mode.py @@ -22,9 +22,14 @@ def test_run_command_success(self, mock_run): @patch("subprocess.run") def test_run_command_failure(self, mock_run): mock_run.side_effect = Exception("Command failed") - with self.assertRaises(SystemError): + with self.assertRaises(SystemError) as context: run_command(["echo", "hello"]) + self.assertIn( + "An unexpected error occurred: Command failed", + str(context.exception), + ) + @patch("wifi_interface_mode.run_command") def test_get_interfaces(self, mock_run_command): mock_run_command.return_value = """ @@ -37,6 +42,15 @@ def test_get_interfaces(self, mock_run_command): result = get_interfaces() self.assertEqual(result, expected) + @patch("wifi_interface_mode.run_command") + def test_get_interfaces_no_wifi_interface(self, mock_run_command): + mock_run_command.return_value = "" + + with self.assertRaises(SystemExit) as cm: + get_interfaces() + + self.assertEqual(cm.exception.code, 0) + @patch("wifi_interface_mode.run_command") def test_get_wiphy_info_with_one_phy(self, mock_run_command): mock_run_command.return_value = """Wiphy phy0