From f4082a27e641826e5685298c738d7ca89d9ed04f Mon Sep 17 00:00:00 2001 From: Dan Bungert Date: Fri, 8 Sep 2023 19:48:51 +0000 Subject: [PATCH 1/3] tests: improve tests for shutil.which() == None --- probert/tests/test_filesystem.py | 18 +++++++++--------- probert/tests/test_os.py | 6 ++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/probert/tests/test_filesystem.py b/probert/tests/test_filesystem.py index 6334af3..3a2a7f0 100644 --- a/probert/tests/test_filesystem.py +++ b/probert/tests/test_filesystem.py @@ -196,17 +196,17 @@ async def test_get_device_filesystem_sizing_ext4_no_min(self): actual = await get_device_filesystem(self.device, True) self.assertEqual(expected, actual) - @patch('probert.filesystem.shutil.which') - async def test_ntfsresize_not_found(self, which): - which.return_value = None + +@patch('probert.filesystem.shutil.which', new=Mock(return_value=None)) +class TestFilesystemToolNotFound(IsolatedAsyncioTestCase): + def setUp(self): + self.device = Mock() + + async def test_ntfsresize_not_found(self): self.assertEqual(None, await get_ntfs_sizing(self.device)) - @patch('probert.filesystem.shutil.which') - async def test_dumpe2fs_not_found(self, which): - which.return_value = None + async def test_dumpe2fs_not_found(self): self.assertEqual(None, await get_dumpe2fs_info(self.device)) - @patch('probert.filesystem.shutil.which') - async def test_resize2fs_not_found(self, which): - which.return_value = None + async def test_resize2fs_not_found(self): self.assertEqual(None, await get_resize2fs_info(self.device)) diff --git a/probert/tests/test_os.py b/probert/tests/test_os.py index a7b5f19..2d66d5b 100644 --- a/probert/tests/test_os.py +++ b/probert/tests/test_os.py @@ -154,3 +154,9 @@ async def test_run_once(self, run): self.assertEqual({}, await probe()) self.assertEqual({}, await probe()) run.assert_called_once() + + +@patch('probert.filesystem.shutil.which', new=Mock(return_value=None)) +class TestOsProberNotFound(IsolatedAsyncioTestCase): + async def test_os_prober_not_found(self): + self.assertEqual(None, _run_os_prober()) From f997d6629eb5ae71cef4fc472fc4acc70bec7b51 Mon Sep 17 00:00:00 2001 From: Dan Bungert Date: Fri, 8 Sep 2023 19:57:22 +0000 Subject: [PATCH 2/3] tests: fix other tests for shutil.which() These tests would only pass if the relevant tools were actually installed, but we're not running them. --- probert/tests/test_filesystem.py | 1 + probert/tests/test_os.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/probert/tests/test_filesystem.py b/probert/tests/test_filesystem.py index 3a2a7f0..56f4200 100644 --- a/probert/tests/test_filesystem.py +++ b/probert/tests/test_filesystem.py @@ -60,6 +60,7 @@ async def test_expected_output_vg(self): assert expected == await get_swap_sizing(device) +@patch('probert.filesystem.shutil.which', new=Mock(return_value='/bin/false')) class TestFilesystem(IsolatedAsyncioTestCase): def setUp(self): self.device = Mock() diff --git a/probert/tests/test_os.py b/probert/tests/test_os.py index 2d66d5b..b08e45c 100644 --- a/probert/tests/test_os.py +++ b/probert/tests/test_os.py @@ -15,11 +15,12 @@ import subprocess from unittest import IsolatedAsyncioTestCase -from unittest.mock import patch +from unittest.mock import Mock, patch from probert.os import probe, _parse_osprober, _run_os_prober +@patch('probert.filesystem.shutil.which', new=Mock(return_value='/bin/false')) class TestOsProber(IsolatedAsyncioTestCase): def tearDown(self): _run_os_prober.cache_clear() From 6a00322a247ed9020660092c6809de935d715a24 Mon Sep 17 00:00:00 2001 From: Dan Bungert Date: Mon, 11 Sep 2023 14:25:12 -0600 Subject: [PATCH 3/3] in common class --- probert/tests/__init__.py | 11 +++++++++++ probert/tests/test_filesystem.py | 10 ++++++---- probert/tests/test_os.py | 14 ++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/probert/tests/__init__.py b/probert/tests/__init__.py index e69de29..9c1555c 100644 --- a/probert/tests/__init__.py +++ b/probert/tests/__init__.py @@ -0,0 +1,11 @@ +import shutil # noqa: F401 +from unittest import IsolatedAsyncioTestCase +from unittest.mock import patch + + +class ProbertTestCase(IsolatedAsyncioTestCase): + def setUp(self): + which = patch("shutil.which") + self.m_which = which.start() + self.m_which.return_value = '/bin/false' + self.addCleanup(which.stop) diff --git a/probert/tests/test_filesystem.py b/probert/tests/test_filesystem.py index 56f4200..580322c 100644 --- a/probert/tests/test_filesystem.py +++ b/probert/tests/test_filesystem.py @@ -27,6 +27,7 @@ get_swap_sizing, get_device_filesystem, ) +from probert.tests import ProbertTestCase def read_file(filename): @@ -60,9 +61,9 @@ async def test_expected_output_vg(self): assert expected == await get_swap_sizing(device) -@patch('probert.filesystem.shutil.which', new=Mock(return_value='/bin/false')) -class TestFilesystem(IsolatedAsyncioTestCase): +class TestFilesystem(ProbertTestCase): def setUp(self): + super().setUp() self.device = Mock() self.device.device_node = random_string() @@ -198,10 +199,11 @@ async def test_get_device_filesystem_sizing_ext4_no_min(self): self.assertEqual(expected, actual) -@patch('probert.filesystem.shutil.which', new=Mock(return_value=None)) -class TestFilesystemToolNotFound(IsolatedAsyncioTestCase): +class TestFilesystemToolNotFound(ProbertTestCase): def setUp(self): + super().setUp() self.device = Mock() + self.m_which.return_value = None async def test_ntfsresize_not_found(self): self.assertEqual(None, await get_ntfs_sizing(self.device)) diff --git a/probert/tests/test_os.py b/probert/tests/test_os.py index b08e45c..823d6e6 100644 --- a/probert/tests/test_os.py +++ b/probert/tests/test_os.py @@ -14,14 +14,13 @@ # along with this program. If not, see . import subprocess -from unittest import IsolatedAsyncioTestCase -from unittest.mock import Mock, patch +from unittest.mock import patch from probert.os import probe, _parse_osprober, _run_os_prober +from probert.tests import ProbertTestCase -@patch('probert.filesystem.shutil.which', new=Mock(return_value='/bin/false')) -class TestOsProber(IsolatedAsyncioTestCase): +class TestOsProber(ProbertTestCase): def tearDown(self): _run_os_prober.cache_clear() @@ -157,7 +156,10 @@ async def test_run_once(self, run): run.assert_called_once() -@patch('probert.filesystem.shutil.which', new=Mock(return_value=None)) -class TestOsProberNotFound(IsolatedAsyncioTestCase): +class TestOsProberNotFound(ProbertTestCase): + def setUp(self): + super().setUp() + self.m_which.return_value = None + async def test_os_prober_not_found(self): self.assertEqual(None, _run_os_prober())