pytest-doctestplus icon indicating copy to clipboard operation
pytest-doctestplus copied to clipboard

introduced option to specify file encoding

Open d-chris opened this issue 8 months ago • 15 comments

fixes #283

introduced option to specify file encoding when overwriting files.

default values

  • ini-file: None
  • cli-option: "utf-8"

ini-file always overwrite command line option

command line option

--text-file-encoding=TEXT_FILE_ENCODING
                        Specify encoding for files.

configuration file

  text_file_encoding (string):
                        Default encoding for text files.

d-chris avatar Mar 18 '25 20:03 d-chris

@pllim - bugfix or enhancement?

bsipocz avatar Mar 18 '25 21:03 bsipocz

bugfix or enhancement?

Hmm... It does introduce a new option, so more like enhancement?

pllim avatar Mar 18 '25 21:03 pllim

i tried to implement some tests

but seems with with testdir.inline_run(p, "--doctest-plus-generate-diff=diff", "--text-file-encoding", encoding) that the DebugRunnerPlus has an issue.

Traceback (most recent call last):
  File "C:\Python313\Lib\doctest.py", line 1395, in __run
    exec(compile(example.source, filename, "single",
         ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 compileflags, True), test.globs)
                 ^^^^^^^^^^^^^^^^^^^
  File "<doctest test_file_encoding_utf8.f[0]>", line 1
    print(☆)
          ^
SyntaxError: invalid character '☆' (U+2606)`

see commit f91f59704b974cc4b91d2542dd1b9798dd49c064

d-chris avatar Mar 19 '25 00:03 d-chris

changed PR to Draft

todos

  • fix issue with syntax error
  • refactore my pytest functions, split into seperate tests for diff and overwrite
  • write a small contribution.md / requirements-dev.txt, how i set up .venv to develop for windows

d-chris avatar Mar 19 '25 10:03 d-chris

@d-chris - the last todo point should definitely not be mixed into this PR

bsipocz avatar Mar 19 '25 13:03 bsipocz

for testing implemented alternative fixture to generate temporary python files, due issue #285

d-chris avatar Mar 23 '25 14:03 d-chris

somehow a can't trigger action within my fork, any tips?

i created a new action (outside of the PR scope) -> pass https://github.com/d-chris/pytest-doctestplus/actions/runs/14041267921

so the small fix from 09979a3 solved the issue

d-chris avatar Mar 24 '25 17:03 d-chris

Github Actions has to be enabled explicitly by maintainer if it detects a first time contributor. It is how they attempt to prevent bitcoin miners. Nothing you could have done on your side. I just enabled the CI. Thanks for your patience!

pllim avatar Mar 24 '25 17:03 pllim

Thanks for your patience—this is my first GH contribution ever!

Is there anything else I need to do to finish the PR?

d-chris avatar Mar 24 '25 17:03 d-chris

Since @bsipocz requested changes, would be nice if she could re-review. 🙏

pllim avatar Mar 24 '25 17:03 pllim

Since @bsipocz requested changes, would be nice if she could re-review

Yeap, I plan to come back to this and the other PR later this week.

bsipocz avatar Mar 24 '25 19:03 bsipocz

Hmm, failures look relevant for pytest < 7.4, but I haven't looked into the details.

Possible options:

  • find an easy workaround that makes this feature work; or only have it for 7.4+
  • we can consider dropping support for older versions. We don't really follow SPEC0 for these infrastructure tools but keep support until we run into a blockage, and I would be open to discuss if this is a blockage (7.3 will just about pass the 2 year limit in a couple of days)

bsipocz avatar Apr 01 '25 15:04 bsipocz

i use option --config-file to specify the ini file while running my tests.

whats confuses me i always run tox before a review, and i always pass all tests

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --config-file /tmp/pytest-of-runner/pytest-0/test_basic_file_encoding_overwrite3/pytest.ini

i guess there i just have to rewrite my tests to pass the configuration file in the correct way. dropping support should not be really necessary?

d-chris avatar Apr 01 '25 15:04 d-chris

ahh, indeeed. --config-file was new in 7.4: https://github.com/pytest-dev/pytest/releases/tag/7.4.0

bsipocz avatar Apr 01 '25 15:04 bsipocz

-c should do the trick

pytest --version & pytest --help | findstr "config"
This is pytest version 5.1.0, imported from D:\temp\pytest5\.venv\lib\site-packages\pytest.py
                        configuration file raise errors.
  -c file               load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
test session debugging and configuration:
  -h, --help            show help message and configuration info
  --trace-config        trace considerations of conftest.py files.

d-chris avatar Apr 01 '25 16:04 d-chris