Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions tools/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def parse_test_metadata(path: Path, display_path: str | None = None) -> TestMeta
marker = "// wave-test:"

try:
lines = path.read_text().splitlines()
except OSError as error:
lines = path.read_text(encoding="utf-8").splitlines()
except (OSError, UnicodeDecodeError) as error:
raise ValueError(f"failed to read wave-test metadata from {display}: {error}") from error

for line in lines:
Expand Down
56 changes: 56 additions & 0 deletions tools/test_test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
# SPDX-License-Identifier: MPL-2.0
# AI TRAINING NOTICE: Prohibited without prior written permission. No use for machine learning or generative AI training, fine-tuning, distillation, embedding, or dataset creation.

import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest

Expand Down Expand Up @@ -99,6 +102,59 @@ def test_artifact_expectations_require_matching_emit_and_architecture(self):
with self.assertRaises(ValueError):
self.parse(directory, body)

def test_utf8_metadata_read_with_non_ascii_comments(self):
with tempfile.TemporaryDirectory() as directory:
source = Path(directory) / "case.wave"
source.write_bytes(
"// wave-test: mode=check\n// non-ascii comment: 測試 é\nfun main() {}\n".encode(
"utf-8"
)
)
metadata = parse_test_metadata(source, "cases/shared/test1.wave")
self.assertEqual(metadata.mode, "check")

def test_utf8_metadata_read_independent_of_ascii_host_locale(self):
with tempfile.TemporaryDirectory() as directory:
source = Path(directory) / "case.wave"
source.write_bytes(
"// wave-test: mode=check\n// non-ascii comment: 測試 é\nfun main() {}\n".encode(
"utf-8"
)
)
command = [
sys.executable,
"-c",
"import sys; from pathlib import Path; "
"from tools.test_contracts import parse_test_metadata; "
"meta = parse_test_metadata(Path(sys.argv[1]), 'case.wave'); "
"sys.stdout.write(meta.mode)",
str(source),
]
env = {**os.environ, "LC_ALL": "C", "PYTHONUTF8": "0"}
process = subprocess.run(
command,
capture_output=True,
text=True,
env=env,
)
self.assertEqual(process.returncode, 0, process.stderr)
self.assertEqual(process.stdout, "check")

def test_invalid_utf8_bytes_reports_display_path_in_value_error(self):
with tempfile.TemporaryDirectory() as directory:
source = Path(directory) / "case.wave"
source.write_bytes(b"// wave-test: mode=check\n// \xff\n")
with self.assertRaises(ValueError) as context:
parse_test_metadata(source, "shared/case.wave")
self.assertIn("failed to read wave-test metadata from shared/case.wave:", str(context.exception))
self.assertIsInstance(context.exception.__cause__, UnicodeDecodeError)

# Also verify fallback to str(source) when display_path is omitted
with self.assertRaises(ValueError) as context_default:
parse_test_metadata(source)
self.assertIn(f"failed to read wave-test metadata from {source}:", str(context_default.exception))
self.assertIsInstance(context_default.exception.__cause__, UnicodeDecodeError)


class ArtifactContractTests(unittest.TestCase):
def setUp(self):
Expand Down
Loading