Repository navigation
Fix asset names for test ids with escapes or non-ASCII characters - #1082
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
The asset filename for extras was derived from the test id with
test_id.encode("utf-8").decode("unicode_escape"). This had two
problems:
* Test ids containing a backslash sequence that isn't a valid escape,
such as "\N" in a Windows path parameter when test id escaping is
disabled (or in node ids produced by other plugins), raised
UnicodeDecodeError and aborted the whole run with an INTERNALERROR.
* Non-ASCII characters that pytest doesn't escape, such as those in
function names, were decoded byte by byte as Latin-1 and ended up as
mojibake in the asset filename (e.g. "test_Ã__0_0.txt").
Escape non-ASCII characters with backslashreplace before decoding so
they survive the round-trip, and fall back to the raw test id when it
can't be decoded. The filename is sanitized afterwards anyway, so the
fallback still yields a valid asset name.
Fixes pytest-dev#558
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The asset filename for extras is built from the test id after
test_id.encode("utf-8").decode("unicode_escape"). This has two problems:INTERNALERROR> UnicodeDecodeError: 'unicodeescape' codec can't decode bytes ...: malformed \N character escape. This happens with a Windows path parameter such as"C:\\New Folder"whendisable_test_id_escaping_and_forfeit_all_rights_to_community_supportis enabled, or with node ids produced by other plugins.def test_é()givestest_a.py__test_Ã__0_0.txt(the same root cause as Multi byte character method name is garbled #501).Changes
A new
_unescape_test_id()helper escapes non-ASCII characters withbackslashreplacebefore decoding, so they survive the round-trip. It falls back to the raw test id when the id can't be decoded._asset_filenamesanitizes the name afterwards anyway, so the fallback still gives a valid filename. Escaped parametrize ids such as[\u00e4]still decode back to readable characters, as before.Testing
New tests in
testing/test_unit.py:test_extra_asset_name: checks the asset filename for an ASCII test, a non-ASCII function name and a non-ASCII parametrize id.test_extra_asset_name_invalid_escape_sequence: checks that aC:\New Folderparameter with id escaping disabled no longer crashes the run and produces..._test_pass_C__New_Folder__0_0.txt.The non-ASCII function name case and the invalid escape case fail on
masterand pass with this change. The rest of the unit and integration suites pass locally, except for the twotest_rerunintegration tests, which fail the same way onmasterwith pytest-rerunfailures 16.x.Fixes #558