From 0d702f87c3b666e02ec3f69d6592f9836691f31c Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Mon, 17 Feb 2025 13:50:57 +0100 Subject: [PATCH] fix: decode answers file content explicitly as UTF-8 --- copier/subproject.py | 2 +- tests/test_updatediff.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/copier/subproject.py b/copier/subproject.py index b037baa97..63a949ec1 100644 --- a/copier/subproject.py +++ b/copier/subproject.py @@ -56,7 +56,7 @@ def _raw_answers(self) -> AnyByStrDict: """Get last answers, loaded raw as yaml.""" try: return yaml.safe_load( - (self.local_abspath / self.answers_relpath).read_text() + (self.local_abspath / self.answers_relpath).read_text("utf-8") ) except OSError: return {} diff --git a/tests/test_updatediff.py b/tests/test_updatediff.py index 4a6ea3f53..d85522104 100644 --- a/tests/test_updatediff.py +++ b/tests/test_updatediff.py @@ -1768,3 +1768,36 @@ def test_gitignore_file_unignored( # Otherwise, it should succeed. run_update(dst_path=dst, overwrite=True) assert "_commit: v3" in (dst / ".copier-answers.yml").read_text() + + +def test_update_with_answers_with_umlaut( + tmp_path_factory: pytest.TempPathFactory, +) -> None: + src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) + + with local.cwd(src): + build_file_tree( + { + "copier.yml": ( + """\ + umlaut: + type: str + """ + ), + "{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_nice_yaml }}", + } + ) + git_init("v1") + git("tag", "v1") + + run_copy(str(src), dst, data={"umlaut": "äöü"}, overwrite=True) + answers_file = dst / ".copier-answers.yml" + answers = yaml.safe_load(answers_file.read_text("utf-8")) + assert answers["umlaut"] == "äöü" + + with local.cwd(dst): + git_init("v1") + + run_update(dst, skip_answered=True, overwrite=True) + answers = yaml.safe_load(answers_file.read_text("utf-8")) + assert answers["umlaut"] == "äöü"