|
| 1 | +import locale |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from textwrap import dedent |
| 5 | +from typing import Callable, Optional |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +pytest_plugins = ["pytester"] |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture( |
| 13 | + params=[ |
| 14 | + ("A", "a", "utf-8"), |
| 15 | + ("☆", "★", "utf-8"), |
| 16 | + ("b", "B", "cp1252"), |
| 17 | + ("☁", "☀", "utf-8"), |
| 18 | + ], |
| 19 | + ids=[ |
| 20 | + "Aa-utf8", |
| 21 | + "star-utf8", |
| 22 | + "bB-cp1252", |
| 23 | + "cloud-utf8", |
| 24 | + ], |
| 25 | +) |
| 26 | +def charset(request): |
| 27 | + return request.param |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture() |
| 31 | +def basic_file(tmp_path: Path) -> Callable[[str, str, str], tuple[str, str, str]]: |
| 32 | + |
| 33 | + def makebasicfile(a, b, encoding: str) -> tuple[str, str, str]: |
| 34 | + """alternative implementation without the use of `testdir.makepyfile`.""" |
| 35 | + |
| 36 | + content = """ |
| 37 | + def f(): |
| 38 | + ''' |
| 39 | + >>> print('{}') |
| 40 | + {} |
| 41 | + ''' |
| 42 | + pass |
| 43 | + """ |
| 44 | + |
| 45 | + original = dedent(content.format(a, b)) |
| 46 | + expected_result = dedent(content.format(a, a)) |
| 47 | + |
| 48 | + original_file = tmp_path.joinpath("test_basic.py") |
| 49 | + original_file.write_text(original, encoding=encoding) |
| 50 | + |
| 51 | + expected_diff = dedent( |
| 52 | + f""" |
| 53 | + >>> print('{a}') |
| 54 | + - {b} |
| 55 | + + {a} |
| 56 | + """ |
| 57 | + ).strip("\n") |
| 58 | + |
| 59 | + return str(original_file), expected_diff, expected_result |
| 60 | + |
| 61 | + return makebasicfile |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture() |
| 65 | +def ini_file(testdir) -> Callable[..., Path]: |
| 66 | + |
| 67 | + def makeini( |
| 68 | + encoding: Optional[str] = None, |
| 69 | + ) -> Path: |
| 70 | + """Create a pytest.ini file with the specified encoding.""" |
| 71 | + |
| 72 | + ini = ["[pytest]"] |
| 73 | + |
| 74 | + if encoding is not None: |
| 75 | + ini.append(f"doctest_encoding = {encoding}") |
| 76 | + |
| 77 | + ini.append("") |
| 78 | + |
| 79 | + p = testdir.makefile(".ini", pytest="\n".join(ini)) |
| 80 | + |
| 81 | + return Path(p) |
| 82 | + |
| 83 | + return makeini |
| 84 | + |
| 85 | + |
| 86 | +def test_basic_file_encoding_diff(testdir, capsys, basic_file, charset, ini_file): |
| 87 | + """ |
| 88 | + Test the diff from console output is as expected. |
| 89 | + """ |
| 90 | + a, b, encoding = charset |
| 91 | + |
| 92 | + # create python file to test |
| 93 | + file, diff, _ = basic_file(a, b, encoding) |
| 94 | + |
| 95 | + # create pytest.ini file |
| 96 | + ini = ini_file(encoding=encoding) |
| 97 | + assert ini.is_file(), "setup pytest.ini not created/found" |
| 98 | + |
| 99 | + testdir.inline_run( |
| 100 | + file, |
| 101 | + "--doctest-plus-generate-diff", |
| 102 | + "-c", |
| 103 | + str(ini), |
| 104 | + ) |
| 105 | + |
| 106 | + stdout, _ = capsys.readouterr() |
| 107 | + assert diff in stdout |
| 108 | + |
| 109 | + |
| 110 | +def test_basic_file_encoding_overwrite(testdir, basic_file, charset, ini_file): |
| 111 | + """ |
| 112 | + Test that the file is overwritten with the expected content. |
| 113 | + """ |
| 114 | + |
| 115 | + a, b, encoding = charset |
| 116 | + |
| 117 | + # create python file to test |
| 118 | + file, _, expected = basic_file(a, b, encoding) |
| 119 | + |
| 120 | + # create pytest.ini file |
| 121 | + ini = ini_file(encoding=encoding) |
| 122 | + assert ini.is_file(), "setup pytest.ini not created/found" |
| 123 | + |
| 124 | + testdir.inline_run( |
| 125 | + file, |
| 126 | + "--doctest-plus-generate-diff", |
| 127 | + "overwrite", |
| 128 | + "-c", |
| 129 | + str(ini), |
| 130 | + ) |
| 131 | + |
| 132 | + assert expected in Path(file).read_text(encoding) |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.skipif(os.getenv("CI", False), reason="skip on CI") |
| 136 | +def test_legacy_diff(testdir, capsys, basic_file, charset): |
| 137 | + """ |
| 138 | + Legacy test are supported to fail on Windows, when no encoding is provided. |
| 139 | +
|
| 140 | + On Windows this is cp1252, so "utf-8" are expected to fail while writing test files. |
| 141 | + """ |
| 142 | + a, b, _ = charset |
| 143 | + |
| 144 | + try: |
| 145 | + file, diff, _ = basic_file(a, b, None) |
| 146 | + except UnicodeEncodeError: |
| 147 | + encoding = locale.getpreferredencoding(False) |
| 148 | + reason = f"could not encode {repr(charset)} with {encoding=}" |
| 149 | + pytest.xfail(reason=reason) |
| 150 | + |
| 151 | + testdir.inline_run( |
| 152 | + file, |
| 153 | + "--doctest-plus-generate-diff", |
| 154 | + ) |
| 155 | + |
| 156 | + stdout, _ = capsys.readouterr() |
| 157 | + |
| 158 | + assert diff in stdout |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.skipif(os.getenv("CI", False), reason="skip on CI") |
| 162 | +def test_legacy_overwrite(testdir, basic_file, charset): |
| 163 | + """ |
| 164 | + Legacy test are supported to fail on Windows, when no encoding is provided. |
| 165 | +
|
| 166 | + On Windows this is cp1252, so "utf-8" are expected to fail while writing test files. |
| 167 | + """ |
| 168 | + |
| 169 | + a, b, _encoding = charset |
| 170 | + |
| 171 | + try: |
| 172 | + file, _, expected = basic_file(a, b, None) |
| 173 | + except UnicodeEncodeError: |
| 174 | + encoding = locale.getpreferredencoding(False) |
| 175 | + reason = f"could not encode {repr(charset)} with {encoding=}" |
| 176 | + pytest.xfail(reason=reason) |
| 177 | + |
| 178 | + testdir.inline_run( |
| 179 | + file, |
| 180 | + "--doctest-plus-generate-diff", |
| 181 | + "overwrite", |
| 182 | + ) |
| 183 | + |
| 184 | + assert expected in Path(file).read_text(_encoding) |
0 commit comments