-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added doctest encoding command line option #2101
Conversation
…es. Defaults to UTF-8.
…cify encoding for internal testdir._makefile() for the tests.
def test_encoding_latin1(self, testdir): | ||
"""Test support for --doctest-encoding option. | ||
""" | ||
testdir._makefile(".txt", [""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at first glance i wonder if this shouldn't use unicode marked strings like u"..."
i believe the actual encoding can be a parametrize parameter, and we might want to add an exotic one just to be on the paranoid side
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would break the code for Python 3.0 I guess because iirc it didn't have the unicode prefix anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wheerd python 3.3 added it back for more pleasant dual codebases
'üäö' | ||
testdir._makefile(".txt", [u""" | ||
>>> len(u'üäö') | ||
3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i propose making those a format string adding u in front of it for py2, and leaving it out for py3
that way the output can be tested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Python 2, they will be formatted as escape sequences: u'\x84\x94\x81'
. Probably I can just repr(value)
to the files.
@@ -43,6 +43,10 @@ def pytest_addoption(parser): | |||
action="store_true", default=False, | |||
help="ignore doctest ImportErrors", | |||
dest="doctest_ignore_import_errors") | |||
group.addoption("--doctest-encoding", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be better off being a ini
option instead:
- If one has to use this option, then usually all
doctest.txt
files will have the same encoding (if the developers want to keep their sanity that is) so it makes sense to configure that inpytest.ini
; - If really desired, one can use the
-o
option to overrideini
options in the command-line. - It is usually not a good idea to require users to pass certain command-line options to make the test suite pass, and in which case the users don't pass the correct option the test-suite will fail in mysterious ways;
@@ -129,6 +129,52 @@ def test_multiple_patterns(self, testdir): | |||
'*1 passed*', | |||
]) | |||
|
|||
def test_encoding_ascii(self, testdir): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could use @pytest.mark.parametrize
on the tests to avoid repeating yourself. Something like (already incorporating my request to change to an ini
option):
import pytest
@pytest.mark.parametrize('string, encoding', [
(u'foo', 'ascii'),
(u'üäö', 'latin1'),
(u'üäö', 'utf-8'),
])
def test_encoding_latin1(testdir, string, encoding):
"""Test support for doctest_encoding option.
"""
testdir.makeini("""
[pytest]
doctest_encoding={0}
""".format(encoding))
testdir._makefile(".txt", u"""
>>> len(u'{0}')
3
""".format(string), encoding=encoding)
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*1 passed*',
])
EDIT: fixed a comment left by accident
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than my comments, please add a CHANGELOG
entry under New Features and add yourself to the CHANGELOG. 😁
Parametrized the tests for it.
@@ -11,6 +11,13 @@ can change the pattern by issuing:: | |||
on the command line. Since version ``2.9``, ``--doctest-glob`` | |||
can be given multiple times in the command-line. | |||
|
|||
You can specify the encoding that will be used for those doctest files | |||
using the ``--doctest-encoding`` command line option:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the docs to mention the doctest_encoding
ini option instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, forgot about that.
doctest_encoding={0} | ||
""".format(encoding)) | ||
doctest = u""" | ||
>>> u"{}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of {}
is not supported by py26
and is breaking the CI, please change that to {0}
, {1}
, etc.
I think we're almost there, thanks again for the PR! |
@@ -11,6 +11,17 @@ can change the pattern by issuing:: | |||
on the command line. Since version ``2.9``, ``--doctest-glob`` | |||
can be given multiple times in the command-line. | |||
|
|||
You can specify the encoding that will be used for those doctest files | |||
using the ``doctest_encoding`` ini option: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing, please add this:
You can specify the encoding that will be used for those doctest files
using the ``doctest_encoding`` ini option:
.. versionadded:: 3.1
.. code-block:: ini
To convey to the users that this is new and was added in 3.1.
lovely - this is shaping up, good work |
The Thanks again @wheerd! |
I added a command line option to specify the encoding of doctest (.txt/.rst) files.
This should fix the error described in #540 which I am also having on my windows machine.