Skip to content

Commit 56bca18

Browse files
fix(yaml-config): address PR #1966 reviewer feedback
* rename json_file -> yaml_file context-manager var * force UTF-8 for YAML writes (YAML 1.2 spec mandates UTF-8/16/32) to avoid UnicodeEncodeError when self._settings.encoding is non-UTF-8 * tighten regression test to reject any \Uxxxxxxxx escape (case-insensitive) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b404143 commit 56bca18

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

commitizen/config/yaml_config.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ def __init__(self, *, data: bytes | str, path: Path) -> None:
2727
self._parse_setting(data)
2828

2929
def init_empty_config_content(self) -> None:
30-
with smart_open(
31-
self.path, "a", encoding=self._settings["encoding"]
32-
) as json_file:
30+
# Write YAML as UTF-8; YAML 1.2 requires UTF-8/16/32.
31+
with smart_open(self.path, "a", encoding="utf-8") as yaml_file:
3332
yaml.dump(
34-
{"commitizen": {}}, json_file, explicit_start=True, allow_unicode=True
33+
{"commitizen": {}}, yaml_file, explicit_start=True, allow_unicode=True
3534
)
3635

3736
def contains_commitizen_section(self) -> bool:
@@ -62,9 +61,8 @@ def set_key(self, key: str, value: object) -> Self:
6261
config_doc = yaml.load(yaml_file, Loader=yaml.FullLoader)
6362

6463
config_doc["commitizen"][key] = value
65-
with smart_open(
66-
self.path, "w", encoding=self._settings["encoding"]
67-
) as yaml_file:
64+
# Write YAML as UTF-8; YAML 1.2 requires UTF-8/16/32.
65+
with smart_open(self.path, "w", encoding="utf-8") as yaml_file:
6866
yaml.dump(config_doc, yaml_file, explicit_start=True, allow_unicode=True)
6967

7068
return self

tests/test_conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import os
5+
import re
56
from pathlib import Path
67
from typing import Any
78

@@ -514,7 +515,7 @@ def test_set_key_preserves_unicode(self, tmp_path, config_file):
514515

515516
rewritten = path.read_text(encoding="utf-8")
516517
assert "🚀" in rewritten
517-
assert "\\U0001F680" not in rewritten
518+
assert not re.search(r"\\U[0-9a-fA-F]{8}", rewritten)
518519

519520
def test_init_empty_config_content_passes_allow_unicode(
520521
self, tmp_path, config_file, mocker

0 commit comments

Comments
 (0)