from pathlib import Path


def testfile_checker(pytester, checker):
    """Return callable that compares pre existing file to testfile.

    This fixture is designed to run in a test case that injects the
    pytester fixture. pytester sets the current working directory
    to a temporary directory while it is active. pytester.copy_example()
    provides relative access to the project's directory.

    Caution: pytester.copy_example() writes a copy of file existing_filename
    at the pytester current working directory.
    """

    def check_it(existing_filename, testfile):
        """Compare pre existing file to testfile."""
        expected_file = pytester.copy_example(existing_filename)
        expected_contents = Path(expected_file).read_text(encoding="utf-8")
        checker(expected_contents, testfile)

    return check_it