import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from failure import Failure from chunk_driver.claims import LOCK, starting class Starting(unittest.TestCase): def setUp(self) -> None: temp = tempfile.TemporaryDirectory() self.addCleanup(temp.cleanup) self.folder = pathlib.Path(temp.name) def test_yields_none_while_it_holds_the_lock_in_a_folder_it_makes(self) -> None: with starting(self.folder / "state" / "proj") as failure: self.assertIsNone(failure) self.assertTrue((self.folder / "state" / "proj" / LOCK).is_file()) def test_yields_a_failure_naming_the_path_it_could_not_make(self) -> None: (self.folder / "state").write_text("", encoding="utf-8") with starting(self.folder / "state") as failure: assert isinstance(failure, Failure) self.assertRegex(failure.reason, f"^cannot lock {self.folder}/state: \\w") def test_yields_a_failure_naming_the_lock_it_could_not_open(self) -> None: (self.folder / LOCK).mkdir() with starting(self.folder) as failure: assert isinstance(failure, Failure) self.assertRegex(failure.reason, f"^cannot lock {self.folder}/{LOCK}: \\w") def test_an_error_raised_while_the_lock_is_held_is_the_callers(self) -> None: with self.assertRaises(OSError), starting(self.folder): raise OSError("not the lock's") if __name__ == "__main__": unittest.main()