Skip to content

Commit a294677

Browse files
test(extensions,presets): add UTF-8 manifest regression tests for github#2325
Positive: extension.yml/preset.yml with non-ASCII (Chinese + emoji) descriptions load correctly when written as UTF-8 bytes — fails on Windows without explicit encoding='utf-8'. Negative: files containing invalid UTF-8 bytes raise a clean error (ValidationError or UnicodeDecodeError), not a silent crash.
1 parent 378cf65 commit a294677

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎tests/test_extensions.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,35 @@ def test_non_mapping_yaml_raises_validation_error(self, temp_dir):
225225
with pytest.raises(ValidationError, match="YAML mapping"):
226226
ExtensionManifest(manifest_path)
227227

228+
def test_utf8_non_ascii_description_loads(self, temp_dir, valid_manifest_data):
229+
"""Regression for #2325: non-ASCII (UTF-8) description loads on any platform.
230+
231+
On Windows, Python's default text-mode encoding is the locale codepage
232+
(e.g. cp1252/GBK), which raises UnicodeDecodeError on UTF-8 bytes
233+
outside the ASCII range. The loader must open with encoding='utf-8'.
234+
"""
235+
import yaml
236+
237+
valid_manifest_data["extension"]["description"] = "中文测试 — émojis 🚀"
238+
manifest_path = temp_dir / "extension.yml"
239+
# Write UTF-8 bytes explicitly so the test exercises the read path,
240+
# not the (locale-dependent) write path.
241+
manifest_path.write_bytes(
242+
yaml.safe_dump(valid_manifest_data, allow_unicode=True).encode("utf-8")
243+
)
244+
245+
manifest = ExtensionManifest(manifest_path)
246+
assert manifest.description == "中文测试 — émojis 🚀"
247+
248+
def test_invalid_utf8_bytes_raises_validation_error(self, temp_dir):
249+
"""Negative case: file containing invalid UTF-8 bytes should not crash with raw UnicodeDecodeError."""
250+
manifest_path = temp_dir / "extension.yml"
251+
# 0xFF/0xFE are not valid UTF-8 lead bytes.
252+
manifest_path.write_bytes(b"\xff\xfe not valid utf-8 \xff\n")
253+
254+
with pytest.raises((ValidationError, UnicodeDecodeError)):
255+
ExtensionManifest(manifest_path)
256+
228257
def test_invalid_extension_id(self, temp_dir, valid_manifest_data):
229258
"""Test manifest with invalid extension ID format."""
230259
import yaml

‎tests/test_presets.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ def test_invalid_yaml(self, temp_dir):
160160
with pytest.raises(PresetValidationError, match="Invalid YAML"):
161161
PresetManifest(bad_file)
162162

163+
def test_utf8_non_ascii_description_loads(self, temp_dir, valid_pack_data):
164+
"""Regression for #2325: non-ASCII (UTF-8) description loads on any platform.
165+
166+
On Windows, Python's default text-mode encoding is the locale codepage
167+
(e.g. cp1252/GBK), which raises UnicodeDecodeError on UTF-8 bytes
168+
outside the ASCII range. The loader must open with encoding='utf-8'.
169+
"""
170+
valid_pack_data["preset"]["description"] = "中文测试 — émojis 🚀"
171+
manifest_path = temp_dir / "preset.yml"
172+
manifest_path.write_bytes(
173+
yaml.safe_dump(valid_pack_data, allow_unicode=True).encode("utf-8")
174+
)
175+
176+
manifest = PresetManifest(manifest_path)
177+
assert manifest.description == "中文测试 — émojis 🚀"
178+
179+
def test_invalid_utf8_bytes_raises_validation_error(self, temp_dir):
180+
"""Negative case: file containing invalid UTF-8 bytes should not crash silently."""
181+
manifest_path = temp_dir / "preset.yml"
182+
manifest_path.write_bytes(b"\xff\xfe not valid utf-8 \xff\n")
183+
184+
with pytest.raises((PresetValidationError, UnicodeDecodeError)):
185+
PresetManifest(manifest_path)
186+
163187
def test_missing_schema_version(self, temp_dir, valid_pack_data):
164188
"""Test missing schema_version field."""
165189
del valid_pack_data["schema_version"]

0 commit comments

Comments
 (0)