From 2a8c2a3f521c03dee1c673e227b6d6dd0674cda2 Mon Sep 17 00:00:00 2001 From: davrens101 <166565828+davrens101@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:50:57 -0800 Subject: [PATCH] Use UTF-8 when sorting simple YAML files --- README.md | 2 +- pre_commit_hooks/sort_simple_yaml.py | 2 +- tests/sort_simple_yaml_test.py | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8432455f..d6db8e25 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ the following commandline options: Sorts entries in requirements.txt and constraints.txt and removes incorrect entry for `pkg-resources==0.0.0` #### `sort-simple-yaml` -Sorts simple YAML files which consist only of top-level +Sorts simple UTF-8 YAML files which consist only of top-level keys, preserving comments and blocks. Note that `sort-simple-yaml` by default matches no `files` as it enforces a diff --git a/pre_commit_hooks/sort_simple_yaml.py b/pre_commit_hooks/sort_simple_yaml.py index 65e6b7a6..ffd76678 100644 --- a/pre_commit_hooks/sort_simple_yaml.py +++ b/pre_commit_hooks/sort_simple_yaml.py @@ -107,7 +107,7 @@ def main(argv: Sequence[str] | None = None) -> int: retval = 0 for filename in args.filenames: - with open(filename, 'r+') as f: + with open(filename, 'r+', encoding='utf-8') as f: lines = [line.rstrip() for line in f.readlines()] new_lines = sort(lines) diff --git a/tests/sort_simple_yaml_test.py b/tests/sort_simple_yaml_test.py index 6cbda857..abb745ac 100644 --- a/tests/sort_simple_yaml_test.py +++ b/tests/sort_simple_yaml_test.py @@ -4,6 +4,7 @@ import pytest +from pre_commit_hooks import sort_simple_yaml from pre_commit_hooks.sort_simple_yaml import first_key from pre_commit_hooks.sort_simple_yaml import main from pre_commit_hooks.sort_simple_yaml import parse_block @@ -52,6 +53,29 @@ def test_integration_good_bad_lines(tmpdir, bad_lines, good_lines, retval): assert [line.rstrip() for line in f.readlines()] == good_lines +@pytest.mark.parametrize( + 'source,expected_status', + [ + ('z: last\n\na: \u041d\n', RETVAL_BAD), + ('a: \u041d\n\nz: last\n', RETVAL_GOOD), + ], +) +def test_utf8_yaml_with_cp1252_locale( + tmp_path, monkeypatch, source, expected_status, +): + path = tmp_path / 'config.yaml' + path.write_text(source, encoding='utf-8') + original_open = open + + def cp1252_open(filename, mode, **kwargs): + kwargs.setdefault('encoding', 'cp1252') + return original_open(filename, mode, **kwargs) + + monkeypatch.setattr(sort_simple_yaml, 'open', cp1252_open, raising=False) + assert main([str(path)]) == expected_status + assert path.read_text(encoding='utf-8') == 'a: \u041d\n\nz: last\n' + + def test_parse_header(): lines = ['# some header', '# is here', '', 'this is not a header'] assert parse_block(lines, header=True) == ['# some header', '# is here']