from typing import Union
from typing import TextIO
from pathlib import Path


def assert_same_contents(
    f1: Union[TextIO, str],
    f2: Union[TextIO, str],
    strip: bool = True,
) -> None:
    """
    Raise AssertionError if the contents of two files aren't identical.

    Parameters
    ----------
    f1
        File-like or file path to be compared.
    f2
        File-like or file path to be compared.
    """

    def get_contents(thing: Union[TextIO, str]) -> str:
        result: str
        if isinstance(thing, TextIO):
            result = thing.read()
        else:
            path = Path(thing)
            with open(path, "rt") as f:
                result = f.read()
        return result

    f1_contents = get_contents(f1)
    f2_contents = get_contents(f2)
    if strip:
        f1_contents = f1_contents.strip()
        f2_contents = f2_contents.strip()
    assert f1_contents == f2_contents