Skip to content

Commit

Permalink
fixup DirectoryPathMustExistTest
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelLarkin committed Jun 20, 2024
1 parent 0ca7553 commit 5e54f7b
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions everyvoice/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,38 @@ class DirectoryPathMustExistTest(TestCase):
It should create a directory if it doesn't exist.
"""

def test_argument_is_Path(self):
"""directory_path_must_exist() expects a Path"""
with self.assertRaisesRegex(
ValueError,
r".*Assertion failed, \[type=assertion_error, input_value='invalid_not_a_Path', input_type=str\].*",
):
DirectoryPathMustExist(path="invalid_not_a_Path")

def test_using_a_directory_with_context(self):
"""
Verifies that directory_path_must_exist(), when using a context,
creates the directory if it doesn't exist.
"""
with tempfile.TemporaryDirectory() as tmpdir:
root_dir = Path(tmpdir)
directory = Path("test_using_a_directory_with_context")
self.assertFalse((root_dir / directory).exists())
with init_context({"writing_config": root_dir.resolve()}):
dir = DirectoryPathMustExist(path=directory)
# Note: dir.path shouldn't not change to an absolute value.
self.assertEqual(dir.path, directory)
self.assertTrue((root_dir / directory).exists())
# Note: since dir.path is NOT replaced with an absolute it
# shouldn't exist because it was created relative to the context's
# path.
self.assertFalse(dir.path.exists())

def test_path_already_exists(self):
path = Path(__file__).parent / "data"
with capture_logs() as output:
dir = DirectoryPathMustExist(path=path)
# TODO: Should check that there is no log produced saying a directory was created
# There should be no info logged.
self.assertListEqual(output, [])
self.assertTrue(dir.path.exists())

Expand All @@ -232,25 +259,6 @@ def test_using_a_directory(self):
self.assertTrue(path.exists())
self.assertTrue(dir.path.exists())

def test_using_a_directory_with_context(self):
"""
Verifies that directory_path_must_exist(), when using a context,
creates the directory if it doesn't exist.
"""
with tempfile.TemporaryDirectory() as tmpdir:
root_dir = Path(tmpdir)
directory = Path("test_using_a_directory_with_context")
self.assertFalse((root_dir / directory).exists())
with init_context({"writing_config": root_dir.resolve()}):
dir = DirectoryPathMustExist(path=directory)
# Note: dir.path shouldn't not change to an absolute value.
self.assertEqual(dir.path, directory)
self.assertTrue((root_dir / directory).exists())
# Note: since dir.path is NOT replaced with an absolute it
# shouldn't exist because it was created relative to the context's
# path.
self.assertFalse(dir.path.exists())


class GetDeviceFromAcceleratorTest(TestCase):
def test_auto(self):
Expand Down

0 comments on commit 5e54f7b

Please sign in to comment.