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 handed, LOCK, starting class Handed(unittest.TestCase): def test_hands_a_window_the_setups_own_variables_and_the_two_that_move_its_folders(self) -> None: env = {"BUILD_RELEASE_JUDGE_MODEL": "judge", "XDG_CONFIG_HOME": "/configs", "XDG_STATE_HOME": "/state", "XDG_DATA_HOME": "/data", "PATH": "/bin"} self.assertEqual(handed(env), {"BUILD_RELEASE_JUDGE_MODEL": "judge", "XDG_CONFIG_HOME": "/configs", "XDG_STATE_HOME": "/state"}) def test_hands_no_variable_the_starter_lacks(self) -> None: self.assertEqual(handed({"PATH": "/bin"}), {}) 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()