| 1 |
import os |
| 2 |
from functools import partial |
| 3 |
from pathlib import Path |
| 4 |
from typing import Callable |
| 5 |
from typing import Optional |
| 6 |
from typing import Union |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from .common import check_text_files |
| 11 |
from .common import perform_regression_check |
| 12 |
|
| 13 |
|
| 14 |
class FileRegressionFixture: |
| 15 |
""" |
| 16 |
Implementation of `file_regression` fixture. |
| 17 |
""" |
| 18 |
|
| 19 |
def __init__( |
| 20 |
self, datadir: Path, original_datadir: Path, request: pytest.FixtureRequest |
| 21 |
) -> None: |
| 22 |
self.request = request |
| 23 |
self.datadir = datadir |
| 24 |
self.original_datadir = original_datadir |
| 25 |
self.force_regen = False |
| 26 |
self.with_test_class_names = False |
| 27 |
|
| 28 |
def check( |
| 29 |
self, |
| 30 |
contents: Union[str, bytes], |
| 31 |
encoding: Optional[str] = None, |
| 32 |
extension: str = ".txt", |
| 33 |
newline: Optional[str] = None, |
| 34 |
basename: Optional[str] = None, |
| 35 |
fullpath: Optional["os.PathLike[str]"] = None, |
| 36 |
binary: bool = False, |
| 37 |
obtained_filename: Optional["os.PathLike[str]"] = None, |
| 38 |
check_fn: Optional[Callable[[Path, Path], None]] = None, |
| 39 |
) -> None: |
| 40 |
""" |
| 41 |
Checks the contents against a previously recorded version, or generate a new file. |
| 42 |
|
| 43 |
:param contents: content to be verified. |
| 44 |
:param encoding: Encoding used to write file, if any. |
| 45 |
:param extension: Extension of file. |
| 46 |
:param newline: See `io.open` docs. |
| 47 |
:param binary: If the file is binary or text. |
| 48 |
:param obtained_filename: ..see:: FileRegressionCheck |
| 49 |
:param check_fn: a function with signature ``(obtained_filename, expected_filename)`` that should raise |
| 50 |
AssertionError if both files differ. |
| 51 |
If not given, use internal function which compares text using :py:mod:`difflib`. |
| 52 |
""" |
| 53 |
__tracebackhide__ = True |
| 54 |
|
| 55 |
if binary and encoding: |
| 56 |
raise ValueError( |
| 57 |
"Only binary ({!r}) or encoding ({!r}) parameters must be passed at the same time.".format( |
| 58 |
binary, encoding |
| 59 |
) |
| 60 |
) |
| 61 |
|
| 62 |
if binary: |
| 63 |
assert isinstance( |
| 64 |
contents, bytes |
| 65 |
), "Expected bytes contents but received type {}".format( |
| 66 |
type(contents).__name__ |
| 67 |
) |
| 68 |
else: |
| 69 |
assert isinstance( |
| 70 |
contents, str |
| 71 |
), "Expected text/unicode contents but received type {}".format( |
| 72 |
type(contents).__name__ |
| 73 |
) |
| 74 |
|
| 75 |
if check_fn is None: |
| 76 |
if binary: |
| 77 |
|
| 78 |
def check_fn(obtained_filename: Path, expected_filename: Path) -> None: |
| 79 |
if obtained_filename.read_bytes() != expected_filename.read_bytes(): |
| 80 |
raise AssertionError( |
| 81 |
"Binary files {} and {} differ.".format( |
| 82 |
obtained_filename, expected_filename |
| 83 |
) |
| 84 |
) |
| 85 |
|
| 86 |
else: |
| 87 |
check_fn = partial(check_text_files, encoding=encoding) |
| 88 |
|
| 89 |
def dump_fn(filename: Path) -> None: |
| 90 |
mode = "wb" if binary else "w" |
| 91 |
with open(str(filename), mode, encoding=encoding, newline=newline) as f: |
| 92 |
f.write(contents) |
| 93 |
|
| 94 |
assert check_fn is not None |
| 95 |
perform_regression_check( |
| 96 |
datadir=self.datadir, |
| 97 |
original_datadir=self.original_datadir, |
| 98 |
request=self.request, |
| 99 |
check_fn=check_fn, |
| 100 |
dump_fn=dump_fn, |
| 101 |
extension=extension, |
| 102 |
basename=basename, |
| 103 |
fullpath=fullpath, |
| 104 |
force_regen=self.force_regen, |
| 105 |
with_test_class_names=self.with_test_class_names, |
| 106 |
obtained_filename=obtained_filename, |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
Check = check |