From a4f2aa2edf3a35a19241c1bb71c88c33eb65defb Mon Sep 17 00:00:00 2001 From: whning Date: Sat, 31 Oct 2026 20:13:40 +0800 Subject: [PATCH] Report invalid manifest encoding as a validation error --- artifactManifest/artifact_manifest.py | 2 +- artifactManifest/tests/test_artifact_manifest.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/artifactManifest/artifact_manifest.py b/artifactManifest/artifact_manifest.py index 0600d82..d9c5946 100644 --- a/artifactManifest/artifact_manifest.py +++ b/artifactManifest/artifact_manifest.py @@ -213,7 +213,7 @@ def _validate_relative_path(value: object) -> str: def _load_expected(manifest_path: Path) -> dict[str, ManifestFile]: try: payload = json.loads(manifest_path.read_text(encoding="utf-8")) - except (OSError, json.JSONDecodeError) as exc: + except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc: raise ManifestError(f"cannot read manifest {manifest_path}: {exc}") from exc if not isinstance(payload, dict): raise ManifestError("manifest must contain a JSON object") diff --git a/artifactManifest/tests/test_artifact_manifest.py b/artifactManifest/tests/test_artifact_manifest.py index fa7a21c..ba28a53 100644 --- a/artifactManifest/tests/test_artifact_manifest.py +++ b/artifactManifest/tests/test_artifact_manifest.py @@ -197,3 +197,17 @@ def test_named_pipe_does_not_block_manifest_creation(tmp_path: Path) -> None: os.mkfifo(root / "pipe") with pytest.raises(artifact_manifest.ManifestError, match="not a regular file: pipe"): artifact_manifest.collect_files(root) + + +def test_invalid_utf8_manifest_reports_error_without_traceback(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: + root = tmp_path / "artifact" + root.mkdir() + manifest = tmp_path / "invalid.json" + manifest.write_bytes(b"\xff\xfe") + + with pytest.raises(artifact_manifest.ManifestError, match="cannot read manifest"): + artifact_manifest.verify_manifest(root, manifest) + assert artifact_manifest.main(["verify", str(root), str(manifest)]) == 2 + captured = capsys.readouterr() + assert "cannot read manifest" in captured.err + assert "Traceback" not in captured.err