Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion natsort/compat/locale.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""

Check failure on line 1 in natsort/compat/locale.py

View workflow job for this annotation

GitHub Actions / Static Analysis

ruff (CPY001)

natsort/compat/locale.py:1:1: CPY001 Missing copyright notice at top of file
Compatibility interface for locale fuctionality.

Makes uniform API to either local or PyICU.
Expand Down Expand Up @@ -35,7 +35,7 @@
# This string should in theory be sorted after any other byte
# string because it contains the max byte char repeated many times.
# You would need some odd data to come after that.
null_string_locale_max = b"x7f" * 50
null_string_locale_max = b"\xff" * 50

def dumb_sort() -> bool:
"""Determine if the locale backend is not collating correctly."""
Expand Down
45 changes: 45 additions & 0 deletions tests/test_compat_locale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Test the locale compatibility layer."""

from __future__ import annotations

import importlib.util
import sys
import types
from typing import TYPE_CHECKING

import pytest

import natsort.compat.locale

if TYPE_CHECKING:
from collections.abc import Iterator


@pytest.fixture
def icu_locale_module() -> Iterator[types.ModuleType]:
"""Load natsort/compat/locale.py with a stub "icu" module in place."""
saved = sys.modules.get("icu")
sys.modules["icu"] = types.ModuleType("icu")
spec = importlib.util.spec_from_file_location(
"_icu_locale", natsort.compat.locale.__file__
)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
yield module
finally:
if saved is None:
del sys.modules["icu"]
else:
sys.modules["icu"] = saved


def test_icu_null_string_locale_max_sorts_after_any_bytes(
icu_locale_module: types.ModuleType,
) -> None:
# Prepended to numbers under ns.NUMAFTER | ns.LOCALE, so it must sort
# after every sort key PyICU can return.
assert icu_locale_module.null_string_locale_max > b"\xff" * 49
assert icu_locale_module.null_string_locale_max > b"\xfe" * 100
Loading