1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-17 23:53:49 +01:00

Pass encoding to AtomicWriter in write_utf8_file_atomic (#164015)

This commit is contained in:
Ye Zhiling
2026-02-27 18:40:58 +08:00
committed by GitHub
parent 1944a8bd3a
commit 856a9e695a
2 changed files with 17 additions and 1 deletions

View File

@@ -83,6 +83,19 @@ def test_write_utf8_file_fails_at_rename_and_remove(
assert "File replacement cleanup failed" in caplog.text
@pytest.mark.parametrize("func", [write_utf8_file, write_utf8_file_atomic])
def test_write_utf8_file_with_non_ascii_content(tmp_path: Path, func) -> None:
"""Test files with non-ASCII content can be written even when locale is ASCII."""
test_file = tmp_path / "test.json"
non_ascii_data = '{"name":"自动化","emoji":"🏠"}'
with patch("locale.getpreferredencoding", return_value="ascii"):
func(test_file, non_ascii_data, False)
file_text = test_file.read_text(encoding="utf-8")
assert file_text == non_ascii_data
def test_write_utf8_file_atomic_fails(tmpdir: py.path.local) -> None:
"""Test OSError from write_utf8_file_atomic is rethrown as WriteError."""
test_dir = tmpdir.mkdir("files")