Skip to content

Fix asset names for test ids with escapes or non-ASCII characters - #1082

Open
NopeCharon wants to merge 1 commit into
pytest-dev:masterfrom
NopeCharon:fix-unicode-escape-asset-name
Open

NopeCharon wants to merge 1 commit into
pytest-dev:masterfrom
NopeCharon:fix-unicode-escape-asset-name

Conversation

@NopeCharon

Copy link
Copy Markdown

The asset filename for extras is built from the test id after test_id.encode("utf-8").decode("unicode_escape"). This has two problems:

  • If a test id contains a backslash sequence that isn't a valid escape, the whole run aborts with INTERNALERROR> UnicodeDecodeError: 'unicodeescape' codec can't decode bytes ...: malformed \N character escape. This happens with a Windows path parameter such as "C:\\New Folder" when disable_test_id_escaping_and_forfeit_all_rights_to_community_support is enabled, or with node ids produced by other plugins.
  • Non-ASCII characters that pytest doesn't escape, such as those in function names, are decoded byte by byte as Latin-1. This produces mojibake asset names, e.g. def test_é() gives test_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 with backslashreplace before decoding, so they survive the round-trip. It falls back to the raw test id when the id can't be decoded. _asset_filename sanitizes 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 a C:\New Folder parameter 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 master and pass with this change. The rest of the unit and integration suites pass locally, except for the two test_rerun integration tests, which fail the same way on master with pytest-rerunfailures 16.x.

Fixes #558

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Report fails to escape '\N' and '\U' characters in test parameter

1 participant