11import locale
2+ import os
23from pathlib import Path
34from textwrap import dedent
4- from typing import Callable , Tuple
5+ from typing import Callable , Optional
56
67import pytest
78
@@ -27,9 +28,9 @@ def charset(request):
2728
2829
2930@pytest .fixture ()
30- def basic_file (tmp_path : Path ) -> Callable [[str , str , str ], Tuple [ Path , str , str ]]:
31+ def basic_file (tmp_path : Path ) -> Callable [[str , str , str ], tuple [ str , str , str ]]:
3132
32- def makebasicfile (a , b , encoding : str ) -> Tuple [str , str , str ]:
33+ def makebasicfile (a , b , encoding : str ) -> tuple [str , str , str ]:
3334 """alternative implementation without the use of `testdir.makepyfile`."""
3435
3536 content = """
@@ -60,42 +61,78 @@ def f():
6061 return makebasicfile
6162
6263
63- def test_basic_file_encoding_diff (testdir , capsys , basic_file , charset ):
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 ):
6487 """
6588 Test the diff from console output is as expected.
6689 """
6790 a , b , encoding = charset
6891
92+ # create python file to test
6993 file , diff , _ = basic_file (a , b , encoding )
7094
95+ # create pytest.ini file
96+ ini = ini_file (encoding = encoding )
97+ assert ini .is_file (), "setup pytest.ini not created/found"
98+
7199 testdir .inline_run (
72- file , "--doctest-plus-generate-diff" , "--text-file-encoding" , encoding
100+ file ,
101+ "--doctest-plus-generate-diff" ,
102+ "--config-file" ,
103+ str (ini ),
73104 )
74105
75106 stdout , _ = capsys .readouterr ()
76107 assert diff in stdout
77108
78109
79- def test_basic_file_encoding_overwrite (testdir , basic_file , charset ):
110+ def test_basic_file_encoding_overwrite (testdir , basic_file , charset , ini_file ):
80111 """
81112 Test that the file is overwritten with the expected content.
82113 """
83114
84115 a , b , encoding = charset
85116
117+ # create python file to test
86118 file , _ , expected = basic_file (a , b , encoding )
87119
120+ # create pytest.ini file
121+ ini = ini_file (encoding = encoding )
122+ assert ini .is_file (), "setup pytest.ini not created/found"
123+
88124 testdir .inline_run (
89125 file ,
90126 "--doctest-plus-generate-diff" ,
91127 "overwrite" ,
92- "--text -file-encoding " ,
93- encoding ,
128+ "--config -file" ,
129+ str ( ini ) ,
94130 )
95131
96132 assert expected in Path (file ).read_text (encoding )
97133
98134
135+ @pytest .mark .skipif (os .getenv ("CI" , False ), reason = "skip on CI" )
99136def test_legacy_diff (testdir , capsys , basic_file , charset ):
100137 """
101138 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):
121158 assert diff in stdout
122159
123160
161+ @pytest .mark .skipif (os .getenv ("CI" , False ), reason = "skip on CI" )
124162def test_legacy_overwrite (testdir , basic_file , charset ):
125163 """
126164 Legacy test are supported to fail on Windows, when no encoding is provided.
0 commit comments