Skip to content

Commit abcaf04

Browse files
author
benoit-cty
committed
test: improve coverage for Windows platform detection
- Add test for is_psutil_available() with small nice value - Add comprehensive error handling tests for count_physical_cpus() on Windows: - CalledProcessError - TimeoutExpired - ValueError for invalid output This brings test coverage to 100% for the Windows-specific code paths added in the PR.
1 parent 9e72978 commit abcaf04

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

tests/test_cpu.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def test_is_psutil_available_with_nice(self, mock_cpu_times):
2828
mock_cpu_times.return_value = mock_times
2929
self.assertTrue(is_psutil_available())
3030

31+
@mock.patch("psutil.cpu_times")
32+
def test_is_psutil_available_with_small_nice(self, mock_cpu_times):
33+
# Test when nice attribute is too small
34+
mock_times = mock.Mock()
35+
mock_times.nice = 0.00001
36+
mock_cpu_times.return_value = mock_times
37+
self.assertFalse(is_psutil_available())
38+
3139
@mock.patch("psutil.cpu_times")
3240
def test_is_psutil_available_without_nice(self, mock_cpu_times):
3341
# Create a mock without 'nice' attribute (like Windows)
@@ -341,6 +349,28 @@ def test_count_physical_cpus_windows(self):
341349
):
342350
assert count_physical_cpus() == 1
343351

352+
def test_count_physical_cpus_windows_with_error(self):
353+
with mock.patch("platform.system", return_value="Windows"):
354+
# Test CalledProcessError
355+
with mock.patch(
356+
"subprocess.run",
357+
side_effect=subprocess.CalledProcessError(1, "powershell"),
358+
):
359+
assert count_physical_cpus() == 1
360+
361+
# Test TimeoutExpired
362+
with mock.patch(
363+
"subprocess.run",
364+
side_effect=subprocess.TimeoutExpired("powershell", 10),
365+
):
366+
assert count_physical_cpus() == 1
367+
368+
# Test ValueError when converting invalid output
369+
with mock.patch(
370+
"subprocess.run", return_value=mock.Mock(returncode=0, stdout="invalid")
371+
):
372+
assert count_physical_cpus() == 1
373+
344374
def test_count_physical_cpus_linux(self):
345375
with mock.patch("platform.system", return_value="Linux"):
346376
lscpu_output = "Socket(s): 2\n"

0 commit comments

Comments
 (0)