From d87f9672fa63819b167200db0429acd2595f88c2 Mon Sep 17 00:00:00 2001 From: FemtosecondLaser <38204088+FemtosecondLaser@users.noreply.github.com> Date: Sat, 23 Oct 2021 13:12:49 +0100 Subject: [PATCH] Improved the readability of the tests. --- tests/unit/test_cli.py | 45 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 55c12ea8c..cf6d91002 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -207,32 +207,33 @@ class DaemonDocsTests(TestCase): class EnsureDirectoryExistsTests(TestCase): + def setUp(self): + self.temp_dir = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.temp_dir) + def test_when_parent_dir_does_not_exist_then_dir_is_created_with_parent(self): - with tempfile.TemporaryDirectory() as temp_dir: - dir_path = os.path.join(temp_dir, "parent_dir", "dir") - ensure_directory_exists(dir_path) - self.assertTrue(os.path.exists(dir_path)) + dir_path = os.path.join(self.temp_dir, "parent_dir", "dir") + ensure_directory_exists(dir_path) + self.assertTrue(os.path.exists(dir_path)) def test_when_non_writable_dir_exists_then_raise(self): - with tempfile.TemporaryDirectory() as temp_dir: - dir_path = os.path.join(temp_dir, "dir") - pathlib.Path(dir_path).mkdir(mode=0o555) # creates a non-writable, readable and executable dir - with self.assertRaises(PermissionError): - ensure_directory_exists(dir_path) + dir_path = os.path.join(self.temp_dir, "dir") + pathlib.Path(dir_path).mkdir(mode=0o555) # creates a non-writable, readable and executable dir + with self.assertRaises(PermissionError): + ensure_directory_exists(dir_path) def test_when_dir_exists_and_writable_then_no_raise(self): - with tempfile.TemporaryDirectory() as temp_dir: - dir_path = os.path.join(temp_dir, "dir") - pathlib.Path(dir_path).mkdir(mode=0o777) # creates a writable, readable and executable dir - try: - ensure_directory_exists(dir_path) - except (FileExistsError, PermissionError) as err: - self.fail(f"{type(err).__name__} was raised") + dir_path = os.path.join(self.temp_dir, "dir") + pathlib.Path(dir_path).mkdir(mode=0o777) # creates a writable, readable and executable dir + try: + ensure_directory_exists(dir_path) + except (FileExistsError, PermissionError) as err: + self.fail(f"{type(err).__name__} was raised") def test_when_non_dir_file_exists_at_path_then_raise(self): - with tempfile.TemporaryDirectory() as temp_dir: - file_path = os.path.join(temp_dir, "file.extension") - with open(file_path, 'x'): - pass - with self.assertRaises(FileExistsError): - ensure_directory_exists(file_path) + file_path = os.path.join(self.temp_dir, "file.extension") + pathlib.Path(file_path).touch() + with self.assertRaises(FileExistsError): + ensure_directory_exists(file_path)