Skip to content
Snippets Groups Projects
test_util.py 714 B
Newer Older
Christoph Alt's avatar
Christoph Alt committed
from cbutil.util import read_file_line_wise
from contextlib import contextmanager
from pathlib import Path
import os


@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