Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def init_empty_config_content(self) -> None:
with smart_open(
self.path, "a", encoding=self._settings["encoding"]
) as json_file:
yaml.dump({"commitizen": {}}, json_file, explicit_start=True)
yaml.dump(
{"commitizen": {}}, json_file, explicit_start=True, allow_unicode=True
)
Comment thread
bearomorphism marked this conversation as resolved.
Outdated

def contains_commitizen_section(self) -> bool:
with self.path.open("rb") as yaml_file:
Expand Down Expand Up @@ -63,6 +65,6 @@ def set_key(self, key: str, value: object) -> Self:
with smart_open(
self.path, "w", encoding=self._settings["encoding"]
) as yaml_file:
yaml.dump(config_doc, yaml_file, explicit_start=True)
yaml.dump(config_doc, yaml_file, explicit_start=True, allow_unicode=True)
Comment thread
bearomorphism marked this conversation as resolved.
Outdated

return self
36 changes: 36 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,39 @@ def test_init_with_invalid_content(self, tmp_path, config_file):
with pytest.raises(InvalidConfigurationError) as excinfo:
YAMLConfig(data=existing_content, path=path)
assert config_file in str(excinfo.value)

def test_set_key_preserves_unicode(self, tmp_path, config_file):
"""Regression test for #1164: emoji and other non-ASCII characters
must be preserved verbatim, not escaped to ``\\Uxxxx`` sequences."""
path = tmp_path / "commitizen" / config_file
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
"commitizen:\n"
' bump_message: "πŸš€ chore: bump $current_version to $new_version"\n',
encoding="utf-8",
)

yaml_config = YAMLConfig(data=path.read_text(encoding="utf-8"), path=path)
yaml_config.set_key("version", "0.1.1")

rewritten = path.read_text(encoding="utf-8")
assert "πŸš€" in rewritten
assert "\\U0001F680" not in rewritten
Comment thread
bearomorphism marked this conversation as resolved.
Outdated

def test_init_empty_config_content_passes_allow_unicode(
self, tmp_path, config_file, mocker
):
"""``init_empty_config_content`` must call ``yaml.dump`` with
``allow_unicode=True`` so that any non-ASCII default content (for
future maintainers) is written verbatim. The current default
(``{"commitizen": {}}``) is ASCII-only, so this asserts the
keyword is passed rather than its observable behaviour."""
path = tmp_path / "commitizen" / config_file
path.parent.mkdir(parents=True, exist_ok=True)
dump_spy = mocker.spy(yaml, "dump")

yaml_config = YAMLConfig(data="{}", path=path)
yaml_config.init_empty_config_content()

dump_spy.assert_called_once()
assert dump_spy.call_args.kwargs.get("allow_unicode") is True
Loading