from cbutil.util import read_file_line_wise, get_git_infos from contextlib import contextmanager from pathlib import Path import os import git @contextmanager def tmp_file(path: Path): """Contextmanager that creates a temporary file and removes it afterwards.""" try: with open(path, "w") as tmp_test_file: yield tmp_test_file finally: os.remove(path) def test_read_file(): test_lines = ["test1", "test2"] test_path = Path("/tmp/tmp_test_file") with tmp_file(test_path) as tmp_test_file: for line in test_lines: tmp_test_file.write(f"{line}\n") for expected, actual in zip(test_lines, read_file_line_wise(test_path)): assert expected == actual def test_git_infos(): infos = get_git_infos(".", commit_key="commit", commit_msg_key="commit_message") assert infos["commit"] == git.Repo(".").head.commit.hexsha