Skip to content
Closed
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pre_commit_hooks/sort_simple_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions tests/sort_simple_yaml_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand Down
Loading