Skip to content

Commit

Permalink
fix: decode external data file content explicitly as UTF-8
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp committed Feb 18, 2025
1 parent 21b486c commit 3953603
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion copier/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def load_answersfile_data(
) -> AnyByStrDict:
"""Load answers data from a `$dst_path/$answers_file` file if it exists."""
try:
with open(Path(dst_path) / answers_file) as fd:
with Path(dst_path, answers_file).open(encoding="utf-8") as fd:
return yaml.safe_load(fd)
except (FileNotFoundError, IsADirectoryError):
warnings.warn(
Expand Down
28 changes: 28 additions & 0 deletions tests/test_answersfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,31 @@ def test_external_data(tmp_path_factory: pytest.TempPathFactory) -> None:
"parent2": "P2",
"child": "C1",
}


def test_external_data_with_umlaut(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))

build_file_tree(
{
(src / "copier.yml"): (
"""\
_external_data:
data: data.yml
ext_umlaut: "{{ _external_data.data.umlaut }}"
"""
),
(src / "{{ _copier_conf.answers_file }}.jinja"): (
"{{ _copier_answers|to_nice_yaml }}"
),
}
)
git_save(src, tag="v1")

(dst / "data.yml").write_text("umlaut: äöü", encoding="utf-8")

copier.run_copy(str(src), dst, defaults=True, overwrite=True)
answers = load_answersfile_data(dst, ".copier-answers.yml")
assert answers["ext_umlaut"] == "äöü"

0 comments on commit 3953603

Please sign in to comment.