From cc0144a0a4161d2f988e8627c75bca366bc79b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Tue, 18 Mar 2025 20:57:03 +0100 Subject: [PATCH 01/10] introduced option to specify file encoding --- pytest_doctestplus/plugin.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pytest_doctestplus/plugin.py b/pytest_doctestplus/plugin.py index 6487da7..146079a 100644 --- a/pytest_doctestplus/plugin.py +++ b/pytest_doctestplus/plugin.py @@ -111,6 +111,10 @@ def pytest_addoption(parser): "Options accepted are 'txt', 'tex', and 'rst'. " "This is no longer recommended, use --doctest-glob instead." )) + + parser.addoption("--text-file-encoding", action="store", + help="Specify encoding for files.", + default="utf-8") # Defaults to `atol` parameter from `numpy.allclose`. parser.addoption("--doctest-plus-atol", action="store", @@ -142,6 +146,9 @@ def pytest_addoption(parser): parser.addini("text_file_format", "Default format for docs. " "This is no longer recommended, use --doctest-glob instead.") + + parser.addini("text_file_encoding", + "Default encoding for text files.", default=None) parser.addini("doctest_optionflags", "option flags for doctests", type="args", default=["ELLIPSIS", "NORMALIZE_WHITESPACE"],) @@ -912,13 +919,13 @@ def test_filter(test): return tests -def write_modified_file(fname, new_fname, changes): +def write_modified_file(fname, new_fname, changes, encoding=None): # Sort in reversed order to edit the lines: bad_tests = [] changes.sort(key=lambda x: (x["test_lineno"], x["example_lineno"]), reverse=True) - with open(fname) as f: + with open(fname, encoding=encoding) as f: text = f.readlines() for change in changes: @@ -939,7 +946,7 @@ def write_modified_file(fname, new_fname, changes): text[lineno:lineno+want.count("\n")] = [got] - with open(new_fname, "w") as f: + with open(new_fname, "w", encoding=encoding) as f: f.write("".join(text)) return bad_tests @@ -953,6 +960,9 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): all_bad_tests = [] if not diff_mode: return # we do not report or apply diffs + + # get encoding to open file default ini=None or option="utf-8" + encoding = config.getini("text_file_encoding") or config.getoption("text_file_encoding") if diff_mode != "overwrite": # In this mode, we write a corrected file to a temporary folder in @@ -974,7 +984,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): new_fname = fname.replace(common_path, tmpdirname) os.makedirs(os.path.split(new_fname)[0], exist_ok=True) - bad_tests = write_modified_file(fname, new_fname, changes) + bad_tests = write_modified_file(fname, new_fname, changes, encoding) all_bad_tests.extend(bad_tests) # git diff returns 1 to signal changes, so just ignore the @@ -1013,7 +1023,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): return terminalreporter.write_line("Applied fix to the following files:") for fname, changes in changesets.items(): - bad_tests = write_modified_file(fname, fname, changes) + bad_tests = write_modified_file(fname, fname, changes, encoding) all_bad_tests.extend(bad_tests) terminalreporter.write_line(f" {fname}") From a872339b0255f6bb548218db66b009128689d7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Tue, 18 Mar 2025 23:51:02 +0100 Subject: [PATCH 02/10] fix codestyle violations --- pytest_doctestplus/plugin.py | 8 ++++---- pytest_doctestplus/utils.py | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pytest_doctestplus/plugin.py b/pytest_doctestplus/plugin.py index 146079a..e091cc1 100644 --- a/pytest_doctestplus/plugin.py +++ b/pytest_doctestplus/plugin.py @@ -111,7 +111,7 @@ def pytest_addoption(parser): "Options accepted are 'txt', 'tex', and 'rst'. " "This is no longer recommended, use --doctest-glob instead." )) - + parser.addoption("--text-file-encoding", action="store", help="Specify encoding for files.", default="utf-8") @@ -146,7 +146,7 @@ def pytest_addoption(parser): parser.addini("text_file_format", "Default format for docs. " "This is no longer recommended, use --doctest-glob instead.") - + parser.addini("text_file_encoding", "Default encoding for text files.", default=None) @@ -451,7 +451,7 @@ def parse(self, s, name=None): continue if config.getoption('remote_data', 'none') != 'any': - if any(re.match(fr'{comment_char}\s+doctest-remote-data-all\s*::', x.strip()) + if any(re.match(fr'{comment_char}\s+doctest-remote-data-all\s*::', x.strip()) # noqa: E501 for x in lines): skip_all = True continue @@ -960,7 +960,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): all_bad_tests = [] if not diff_mode: return # we do not report or apply diffs - + # get encoding to open file default ini=None or option="utf-8" encoding = config.getini("text_file_encoding") or config.getoption("text_file_encoding") diff --git a/pytest_doctestplus/utils.py b/pytest_doctestplus/utils.py index 8a6df5c..c0c7064 100644 --- a/pytest_doctestplus/utils.py +++ b/pytest_doctestplus/utils.py @@ -3,6 +3,7 @@ from importlib.metadata import distribution from packaging.requirements import Requirement + class ModuleChecker: def find_module(self, module): From f91f59704b974cc4b91d2542dd1b9798dd49c064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Wed, 19 Mar 2025 01:30:05 +0100 Subject: [PATCH 03/10] nightly pytests utf8 failes due exception in DebugRunnerPlus --- tests/test_doctestplus.py | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/test_doctestplus.py b/tests/test_doctestplus.py index 63a5572..9ceb9fb 100644 --- a/tests/test_doctestplus.py +++ b/tests/test_doctestplus.py @@ -1538,3 +1538,75 @@ def f(): original_fixed = original.replace("1\n 2", "\n ".join(["0", "1", "2", "3"])) assert result == original_fixed + +@pytest.mark.parametrize('encoding', ['utf-8', 'cp1252']) +def test_file_encoding(testdir, capsys, encoding): + p = testdir.makepyfile(""" + def f(): + ''' + >>> print(2) + 4 + >>> print(3) + 5 + ''' + pass + """) + with open(p) as f: + original = f.read() + + testdir.inline_run(p, "--doctest-plus-generate-diff", "--text-file-encoding", encoding) + diff = dedent(""" + >>> print(2) + - 4 + + 2 + >>> print(3) + - 5 + + 3 + """) + captured = capsys.readouterr() + assert diff in captured.out + + testdir.inline_run(p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding) + captured = capsys.readouterr() + assert "Applied fix to the following files" in captured.out + + with open(p) as f: + result = f.read() + + assert result == original.replace("4", "2").replace("5", "3") + +@pytest.mark.parametrize('encoding', ['utf-8']) +def test_file_encoding_utf8(testdir, capsys, encoding): + p = testdir.makepyfile(""" + def f(): + ''' + >>> print(☆) + ★ + >>> print(☁) + ☀ + ''' + pass + """, encoding=encoding) + with open(p, encoding=encoding) as f: + original = f.read() + + testdir.inline_run(p, "--doctest-plus-generate-diff=diff", "--text-file-encoding", encoding) + diff = dedent(""" + >>> print(☆) + - ★ + + ☆ + >>> print(☁) + - ☀ + + ☁ + """) + captured = capsys.readouterr() + assert diff in captured.out + + testdir.inline_run(p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding) + captured = capsys.readouterr() + assert "Applied fix to the following files" in captured.out + + with open(p, encoding=encoding) as f: + result = f.read() + + assert result == original.replace("★", "☆").replace("☀", "☁") From a15cea02a667468e92e0251d1c4d49f9f6ce717c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Sun, 23 Mar 2025 09:02:08 +0100 Subject: [PATCH 04/10] moved encoding tests to new file --- tests/test_doctestplus.py | 72 ----------------------------- tests/test_encoding.py | 96 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 72 deletions(-) create mode 100644 tests/test_encoding.py diff --git a/tests/test_doctestplus.py b/tests/test_doctestplus.py index 9ceb9fb..63a5572 100644 --- a/tests/test_doctestplus.py +++ b/tests/test_doctestplus.py @@ -1538,75 +1538,3 @@ def f(): original_fixed = original.replace("1\n 2", "\n ".join(["0", "1", "2", "3"])) assert result == original_fixed - -@pytest.mark.parametrize('encoding', ['utf-8', 'cp1252']) -def test_file_encoding(testdir, capsys, encoding): - p = testdir.makepyfile(""" - def f(): - ''' - >>> print(2) - 4 - >>> print(3) - 5 - ''' - pass - """) - with open(p) as f: - original = f.read() - - testdir.inline_run(p, "--doctest-plus-generate-diff", "--text-file-encoding", encoding) - diff = dedent(""" - >>> print(2) - - 4 - + 2 - >>> print(3) - - 5 - + 3 - """) - captured = capsys.readouterr() - assert diff in captured.out - - testdir.inline_run(p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding) - captured = capsys.readouterr() - assert "Applied fix to the following files" in captured.out - - with open(p) as f: - result = f.read() - - assert result == original.replace("4", "2").replace("5", "3") - -@pytest.mark.parametrize('encoding', ['utf-8']) -def test_file_encoding_utf8(testdir, capsys, encoding): - p = testdir.makepyfile(""" - def f(): - ''' - >>> print(☆) - ★ - >>> print(☁) - ☀ - ''' - pass - """, encoding=encoding) - with open(p, encoding=encoding) as f: - original = f.read() - - testdir.inline_run(p, "--doctest-plus-generate-diff=diff", "--text-file-encoding", encoding) - diff = dedent(""" - >>> print(☆) - - ★ - + ☆ - >>> print(☁) - - ☀ - + ☁ - """) - captured = capsys.readouterr() - assert diff in captured.out - - testdir.inline_run(p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding) - captured = capsys.readouterr() - assert "Applied fix to the following files" in captured.out - - with open(p, encoding=encoding) as f: - result = f.read() - - assert result == original.replace("★", "☆").replace("☀", "☁") diff --git a/tests/test_encoding.py b/tests/test_encoding.py new file mode 100644 index 0000000..6eb13bc --- /dev/null +++ b/tests/test_encoding.py @@ -0,0 +1,96 @@ +from textwrap import dedent + +import pytest + +pytest_plugins = ["pytester"] + + +@pytest.mark.parametrize("encoding", ["utf-8", "cp1252"]) +def test_file_encoding(testdir, capsys, encoding): + p = testdir.makepyfile( + """ + def f(): + ''' + >>> print(2) + 4 + >>> print(3) + 5 + ''' + pass + """ + ) + with open(p) as f: + original = f.read() + + testdir.inline_run( + p, "--doctest-plus-generate-diff", "--text-file-encoding", encoding + ) + diff = dedent( + """ + >>> print(2) + - 4 + + 2 + >>> print(3) + - 5 + + 3 + """ + ) + captured = capsys.readouterr() + assert diff in captured.out + + testdir.inline_run( + p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding + ) + captured = capsys.readouterr() + assert "Applied fix to the following files" in captured.out + + with open(p) as f: + result = f.read() + + assert result == original.replace("4", "2").replace("5", "3") + + +@pytest.mark.parametrize("encoding", ["utf-8"]) +def test_file_encoding_utf8(testdir, capsys, encoding): + p = testdir.makepyfile( + """ + def f(): + ''' + >>> print(☆) + ★ + >>> print(☁) + ☀ + ''' + pass + """, + encoding=encoding, + ) + with open(p, encoding=encoding) as f: + original = f.read() + + testdir.inline_run( + p, "--doctest-plus-generate-diff=diff", "--text-file-encoding", encoding + ) + diff = dedent( + """ + >>> print(☆) + - ★ + + ☆ + >>> print(☁) + - ☀ + + ☁ + """ + ) + captured = capsys.readouterr() + assert diff in captured.out + + testdir.inline_run( + p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding + ) + captured = capsys.readouterr() + assert "Applied fix to the following files" in captured.out + + with open(p, encoding=encoding) as f: + result = f.read() + + assert result == original.replace("★", "☆").replace("☀", "☁") From 7940839ed4a3b9cdc56c91756aaed35721c25cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Sun, 23 Mar 2025 12:44:42 +0100 Subject: [PATCH 05/10] bugfix for diffing --- pytest_doctestplus/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_doctestplus/plugin.py b/pytest_doctestplus/plugin.py index e091cc1..450142e 100644 --- a/pytest_doctestplus/plugin.py +++ b/pytest_doctestplus/plugin.py @@ -991,7 +991,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): # exit status: with subprocess.Popen( ["git", "diff", "-p", "--no-index", fname, new_fname], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) as p: + stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding=encoding) as p: p.wait() # Diff should be fine, but write error if not: diff = p.stderr.read() From 88eebb59e4122330ff7de6b8b65bacd3c23263ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Sun, 23 Mar 2025 12:49:28 +0100 Subject: [PATCH 06/10] refactor encoding tests to use fixtures and improve readability --- tests/test_encoding.py | 198 ++++++++++++++++++++++++++--------------- 1 file changed, 124 insertions(+), 74 deletions(-) diff --git a/tests/test_encoding.py b/tests/test_encoding.py index 6eb13bc..07fe718 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -1,96 +1,146 @@ +import locale +from pathlib import Path from textwrap import dedent +from typing import Callable, Tuple import pytest pytest_plugins = ["pytester"] -@pytest.mark.parametrize("encoding", ["utf-8", "cp1252"]) -def test_file_encoding(testdir, capsys, encoding): - p = testdir.makepyfile( - """ - def f(): - ''' - >>> print(2) - 4 - >>> print(3) - 5 - ''' - pass - """ - ) - with open(p) as f: - original = f.read() +@pytest.fixture( + params=[ + ("A", "a", "utf-8"), + ("☆", "★", "utf-8"), + ("b", "B", "cp1252"), + ("☁", "☀", "utf-8"), + ], + ids=[ + "Aa-utf8", + "star-utf8", + "bB-cp1252", + "cloud-utf8", + ], +) +def charset(request): + return request.param + + +@pytest.fixture() +def basic_file(tmp_path: Path) -> Callable[[str, str, str], Tuple[Path, str, str]]: + + def makebasicfile(a, b, encoding: str) -> Tuple[Path, str, str]: + """alternative implementation without the use of `testdir.makepyfile`.""" + + content = """ + def f(): + ''' + >>> print('{}') + {} + ''' + pass + """ + + original = dedent(content.format(a, b)) + expected_result = dedent(content.format(a, a)) + + original_file = tmp_path.joinpath("test_basic.py") + original_file.write_text(original, encoding=encoding) + + expected_diff = dedent( + f""" + >>> print('{a}') + - {b} + + {a} + """ + ).strip("\n") + + return original_file, expected_diff, expected_result + + return makebasicfile + + +def test_basic_file_encoding_diff(testdir, capsys, basic_file, charset): + """ + Test the diff from console output is as expected. + """ + a, b, encoding = charset + + file, diff, _ = basic_file(a, b, encoding) testdir.inline_run( - p, "--doctest-plus-generate-diff", "--text-file-encoding", encoding + file, "--doctest-plus-generate-diff", "--text-file-encoding", encoding ) - diff = dedent( - """ - >>> print(2) - - 4 - + 2 - >>> print(3) - - 5 - + 3 + + stdout, _ = capsys.readouterr() + assert diff in stdout + + +def test_basic_file_encoding_overwrite(testdir, basic_file, charset): + """ + Test that the file is overwritten with the expected content. """ - ) - captured = capsys.readouterr() - assert diff in captured.out + + a, b, encoding = charset + + file, _, expected = basic_file(a, b, encoding) testdir.inline_run( - p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding - ) - captured = capsys.readouterr() - assert "Applied fix to the following files" in captured.out - - with open(p) as f: - result = f.read() - - assert result == original.replace("4", "2").replace("5", "3") - - -@pytest.mark.parametrize("encoding", ["utf-8"]) -def test_file_encoding_utf8(testdir, capsys, encoding): - p = testdir.makepyfile( - """ - def f(): - ''' - >>> print(☆) - ★ - >>> print(☁) - ☀ - ''' - pass - """, - encoding=encoding, + file, + "--doctest-plus-generate-diff", + "overwrite", + "--text-file-encoding", + encoding, ) - with open(p, encoding=encoding) as f: - original = f.read() + + assert expected in file.read_text(encoding) + + +def test_legacy_diff(testdir, capsys, basic_file, charset): + """ + Legacy test are supported to fail on Windows, when no encoding is provided. + + On Windows this is cp1252, so "utf-8" are expected to fail while writing test files. + """ + a, b, _ = charset + + try: + file, diff, _ = basic_file(a, b, None) + except UnicodeEncodeError: + encoding = locale.getpreferredencoding(False) + reason = f"could not encode {repr(charset)} with {encoding=}" + pytest.xfail(reason=reason) testdir.inline_run( - p, "--doctest-plus-generate-diff=diff", "--text-file-encoding", encoding + file, + "--doctest-plus-generate-diff", ) - diff = dedent( - """ - >>> print(☆) - - ★ - + ☆ - >>> print(☁) - - ☀ - + ☁ + + stdout, _ = capsys.readouterr() + + assert diff in stdout + + +def test_legacy_overwrite(testdir, basic_file, charset): """ - ) - captured = capsys.readouterr() - assert diff in captured.out + Legacy test are supported to fail on Windows, when no encoding is provided. + + On Windows this is cp1252, so "utf-8" are expected to fail while writing test files. + """ + + a, b, _encoding = charset + + try: + file, _, expected = basic_file(a, b, None) + except UnicodeEncodeError: + encoding = locale.getpreferredencoding(False) + reason = f"could not encode {repr(charset)} with {encoding=}" + pytest.xfail(reason=reason) testdir.inline_run( - p, "--doctest-plus-generate-diff=overwrite", "--text-file-encoding", encoding + file, + "--doctest-plus-generate-diff", + "overwrite", ) - captured = capsys.readouterr() - assert "Applied fix to the following files" in captured.out - - with open(p, encoding=encoding) as f: - result = f.read() - assert result == original.replace("★", "☆").replace("☀", "☁") + assert expected in file.read_text(_encoding) From 09979a3b41d6a3998bf1ca733875d48d266413ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Sun, 23 Mar 2025 23:09:49 +0100 Subject: [PATCH 07/10] changed fixture from path to str --- tests/test_encoding.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_encoding.py b/tests/test_encoding.py index 07fe718..7682946 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -29,7 +29,7 @@ def charset(request): @pytest.fixture() def basic_file(tmp_path: Path) -> Callable[[str, str, str], Tuple[Path, str, str]]: - def makebasicfile(a, b, encoding: str) -> Tuple[Path, str, str]: + def makebasicfile(a, b, encoding: str) -> Tuple[str, str, str]: """alternative implementation without the use of `testdir.makepyfile`.""" content = """ @@ -55,7 +55,7 @@ def f(): """ ).strip("\n") - return original_file, expected_diff, expected_result + return str(original_file), expected_diff, expected_result return makebasicfile @@ -93,7 +93,7 @@ def test_basic_file_encoding_overwrite(testdir, basic_file, charset): encoding, ) - assert expected in file.read_text(encoding) + assert expected in Path(file).read_text(encoding) def test_legacy_diff(testdir, capsys, basic_file, charset): @@ -143,4 +143,4 @@ def test_legacy_overwrite(testdir, basic_file, charset): "overwrite", ) - assert expected in file.read_text(_encoding) + assert expected in Path(file).read_text(_encoding) From 2b56f2c3fc2b16ef6ea750e39af95d7de484d88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Tue, 1 Apr 2025 15:58:18 +0200 Subject: [PATCH 08/10] refactor encoding tests to use doctest_encoding with ini-file --- tests/test_encoding.py | 54 +++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/tests/test_encoding.py b/tests/test_encoding.py index 7682946..4527e4a 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -1,7 +1,8 @@ import locale +import os from pathlib import Path from textwrap import dedent -from typing import Callable, Tuple +from typing import Callable, Optional import pytest @@ -27,9 +28,9 @@ def charset(request): @pytest.fixture() -def basic_file(tmp_path: Path) -> Callable[[str, str, str], Tuple[Path, str, str]]: +def basic_file(tmp_path: Path) -> Callable[[str, str, str], tuple[str, str, str]]: - def makebasicfile(a, b, encoding: str) -> Tuple[str, str, str]: + def makebasicfile(a, b, encoding: str) -> tuple[str, str, str]: """alternative implementation without the use of `testdir.makepyfile`.""" content = """ @@ -60,42 +61,78 @@ def f(): return makebasicfile -def test_basic_file_encoding_diff(testdir, capsys, basic_file, charset): +@pytest.fixture() +def ini_file(testdir) -> Callable[..., Path]: + + def makeini( + encoding: Optional[str] = None, + ) -> Path: + """Create a pytest.ini file with the specified encoding.""" + + ini = ["[pytest]"] + + if encoding is not None: + ini.append(f"doctest_encoding = {encoding}") + + ini.append("") + + p = testdir.makefile(".ini", pytest="\n".join(ini)) + + return Path(p) + + return makeini + + +def test_basic_file_encoding_diff(testdir, capsys, basic_file, charset, ini_file): """ Test the diff from console output is as expected. """ a, b, encoding = charset + # create python file to test file, diff, _ = basic_file(a, b, encoding) + # create pytest.ini file + ini = ini_file(encoding=encoding) + assert ini.is_file(), "setup pytest.ini not created/found" + testdir.inline_run( - file, "--doctest-plus-generate-diff", "--text-file-encoding", encoding + file, + "--doctest-plus-generate-diff", + "--config-file", + str(ini), ) stdout, _ = capsys.readouterr() assert diff in stdout -def test_basic_file_encoding_overwrite(testdir, basic_file, charset): +def test_basic_file_encoding_overwrite(testdir, basic_file, charset, ini_file): """ Test that the file is overwritten with the expected content. """ a, b, encoding = charset + # create python file to test file, _, expected = basic_file(a, b, encoding) + # create pytest.ini file + ini = ini_file(encoding=encoding) + assert ini.is_file(), "setup pytest.ini not created/found" + testdir.inline_run( file, "--doctest-plus-generate-diff", "overwrite", - "--text-file-encoding", - encoding, + "--config-file", + str(ini), ) assert expected in Path(file).read_text(encoding) +@pytest.mark.skipif(os.getenv("CI", False), reason="skip on CI") def test_legacy_diff(testdir, capsys, basic_file, charset): """ Legacy test are supported to fail on Windows, when no encoding is provided. @@ -121,6 +158,7 @@ def test_legacy_diff(testdir, capsys, basic_file, charset): assert diff in stdout +@pytest.mark.skipif(os.getenv("CI", False), reason="skip on CI") def test_legacy_overwrite(testdir, basic_file, charset): """ Legacy test are supported to fail on Windows, when no encoding is provided. From 4a639d7e3c84375a5ae8774b33d98be6a64dbcce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Tue, 1 Apr 2025 16:01:52 +0200 Subject: [PATCH 09/10] remove text file encoding options from pytest_addoption --- pytest_doctestplus/plugin.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/pytest_doctestplus/plugin.py b/pytest_doctestplus/plugin.py index 450142e..b94446b 100644 --- a/pytest_doctestplus/plugin.py +++ b/pytest_doctestplus/plugin.py @@ -112,10 +112,6 @@ def pytest_addoption(parser): "This is no longer recommended, use --doctest-glob instead." )) - parser.addoption("--text-file-encoding", action="store", - help="Specify encoding for files.", - default="utf-8") - # Defaults to `atol` parameter from `numpy.allclose`. parser.addoption("--doctest-plus-atol", action="store", help="set the absolute tolerance for float comparison", @@ -147,9 +143,6 @@ def pytest_addoption(parser): "Default format for docs. " "This is no longer recommended, use --doctest-glob instead.") - parser.addini("text_file_encoding", - "Default encoding for text files.", default=None) - parser.addini("doctest_optionflags", "option flags for doctests", type="args", default=["ELLIPSIS", "NORMALIZE_WHITESPACE"],) @@ -961,8 +954,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): if not diff_mode: return # we do not report or apply diffs - # get encoding to open file default ini=None or option="utf-8" - encoding = config.getini("text_file_encoding") or config.getoption("text_file_encoding") + encoding = config.getini("doctest_encoding") if diff_mode != "overwrite": # In this mode, we write a corrected file to a temporary folder in From f0ec39e5335e8c1a4457e2000ed87220a445a7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6rrer?= Date: Tue, 1 Apr 2025 17:55:55 +0200 Subject: [PATCH 10/10] changed option for pytest.ini to run on pytest < 7.4 --- tests/test_encoding.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_encoding.py b/tests/test_encoding.py index 4527e4a..0319e9f 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -99,7 +99,7 @@ def test_basic_file_encoding_diff(testdir, capsys, basic_file, charset, ini_file testdir.inline_run( file, "--doctest-plus-generate-diff", - "--config-file", + "-c", str(ini), ) @@ -125,7 +125,7 @@ def test_basic_file_encoding_overwrite(testdir, basic_file, charset, ini_file): file, "--doctest-plus-generate-diff", "overwrite", - "--config-file", + "-c", str(ini), )