def test_get_reference(self): branch_instance = Branch() head_instance = Head("references/branches/main") expected_result = branch.serialize_as_dict(branch_instance) result = {} with tempfile.TemporaryDirectory() as tmpdirname: tmppath = Path(tmpdirname) # store branch branch_path = repository.get_branch_path(tmppath, "main", False) makedirs(branch_path.parent, exist_ok=True) branch.store_as_file(branch_path, branch_instance) # store head head_path = repository.get_head_path(tmppath, False) makedirs(head_path.parent, exist_ok=True) head.store_as_file(head_path, head_instance) result = reference.serialize_as_dict( repository.get_reference(tmppath)) self.assertEqual(result, expected_result)
def test_load_from_file(self): file_path = get_named_tmpfile_path() head_instance = Head() head.store_as_file(file_path, head_instance) expected_result = head.serialize_as_dict(head_instance) result = head.serialize_as_dict(head.load_from_file(file_path)) self.assertEqual(result, expected_result)
def test_store_as_file(self): file_path = get_named_tmpfile_path() data = {"ref": ""} result = "" expected_result = transform.dict_as_json(data) head_instance = Head() head.store_as_file(file_path, head_instance) with open(file_path, "r") as f: result = f.read() self.assertEqual(result, expected_result)
def test_get_head(self): head_instance = Head() expected_result = head.serialize_as_dict(head_instance) result = {} with tempfile.TemporaryDirectory() as tmpdirname: tmppath = Path(tmpdirname) head_path = repository.get_head_path(tmppath, False) makedirs(head_path.parent, exist_ok=True) head.store_as_file(head_path, head_instance) result = head.serialize_as_dict(repository.get_head(tmppath)) self.assertEqual(result, expected_result)
def test_get_latest_commit(self): author_instance = Author("beesperester") commit_instance = Commit(author_instance, "initial commit") expected_result = commit.serialize_as_dict(commit_instance) result = {} with tempfile.TemporaryDirectory() as tmpdirname: tmppath = Path(tmpdirname) # create commit blob blobs_path = repository.get_blobs_path(tmppath, False) makedirs(blobs_path, exist_ok=True) hashid = commit.store_as_blob(blobs_path, commit_instance) # create branch with commit hashid branch_path = repository.get_branch_path(tmppath, "main", False) makedirs(branch_path.parent, exist_ok=True) branch_instance = Branch(hashid) branch.store_as_file(branch_path, branch_instance) # create head with branch as ref head_path = repository.get_head_path(tmppath, False) makedirs(head_path.parent, exist_ok=True) head_instance = Head("references/branches/main") head.store_as_file(head_path, head_instance) result = commit.serialize_as_dict( repository.get_latest_commit(tmppath)) self.assertEqual(result, expected_result)
def store_head(path: Path, head_instance: Head) -> None: head.store_as_file(get_head_path(path, False), head_instance)