diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a340a2e..2758042 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,10 @@ Unreleased Thanks to Abhishek Verma in `PR #755 `__. +* Fix crash when test node IDs contain surrogate characters from undecodable filenames. + + Thanks to Huanyi Xie in `PR #751 `__. + 5.0.0 (2026-09-01) ------------------ diff --git a/src/pytest_randomly/__init__.py b/src/pytest_randomly/__init__.py index 85e9eee..8878f84 100644 --- a/src/pytest_randomly/__init__.py +++ b/src/pytest_randomly/__init__.py @@ -288,7 +288,7 @@ def reduce_list_of_lists(lists: list[list[T]]) -> list[T]: @lru_cache def _crc32(string: str) -> int: - return crc32(string.encode()) + return crc32(string.encode(errors="surrogatepass")) if have_faker: # pragma: no branch diff --git a/tests/test_pytest_randomly.py b/tests/test_pytest_randomly.py index a71d68c..c3214b4 100644 --- a/tests/test_pytest_randomly.py +++ b/tests/test_pytest_randomly.py @@ -2,6 +2,7 @@ import shutil from unittest import mock +from zlib import crc32 import pytest @@ -91,6 +92,23 @@ def test_a(): out.assert_outcomes(passed=1, failed=0) +def test_undecodable_filename(ourtester): + path = ourtester.path / "test_\udcff.py" + try: + path.write_text("def test_one():\n assert True\n", encoding="utf-8") + except OSError: # pragma: no cover + pytest.skip("Filesystem does not allow undecodable filenames") + + result = ourtester.runpytest( + "--randomly-seed=1", + # Upstream issue: the cache provider cannot persist surrogate-containing node IDs + "-p", + "no:cacheprovider", + ) + + result.assert_outcomes(passed=1) + + def test_using_last_seed(ourtester): ourtester.makepyfile( test_one=""" @@ -913,3 +931,11 @@ def test_a(pytestconfig): seed = header[0].removeprefix("Using --randomly-seed=") worker_seeds = {p.read_text() for p in ourtester.path.glob("seed-*.txt")} assert worker_seeds == {seed} + + +def test_crc32_surrogates(): + """ + Lone surrogates, as found in node IDs from undecodable filenames, hash as + their UTF-8 surrogatepass bytes instead of raising UnicodeEncodeError. + """ + assert pytest_randomly._crc32("a\udcffb") == crc32(b"a\xed\xb3\xbfb")