Skip to content

Commit

Permalink
Modify logic and add more unittest to increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rickwu666666 committed Aug 7, 2024
1 parent c1df039 commit bc0f2d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 2 additions & 5 deletions providers/resource/bin/wifi_interface_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand All @@ -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]]]:
Expand Down Expand Up @@ -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)

Check warning on line 63 in providers/resource/bin/wifi_interface_mode.py

View check run for this annotation

Codecov / codecov/patch

providers/resource/bin/wifi_interface_mode.py#L61-L63

Added lines #L61 - L63 were not covered by tests

Expand Down
16 changes: 15 additions & 1 deletion providers/resource/tests/test_wifi_interface_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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
Expand Down

0 comments on commit bc0f2d2

Please sign in to comment.