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
11 changes: 10 additions & 1 deletion artifactManifest/artifact_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,18 @@ def _validate_relative_path(value: object) -> str:
return value


def _unique_json_object(pairs: list[tuple[str, object]]) -> dict[str, object]:
result: dict[str, object] = {}
for key, value in pairs:
if key in result:
raise ManifestError(f"duplicate JSON key: {key!r}")
result[key] = value
return result


def _load_expected(manifest_path: Path) -> dict[str, ManifestFile]:
try:
payload = json.loads(manifest_path.read_text(encoding="utf-8"))
payload = json.loads(manifest_path.read_text(encoding="utf-8"), object_pairs_hook=_unique_json_object)
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
raise ManifestError(f"cannot read manifest {manifest_path}: {exc}") from exc
if not isinstance(payload, dict):
Expand Down
13 changes: 13 additions & 0 deletions artifactManifest/tests/test_artifact_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ def test_schema_version_requires_integer(tmp_path: Path, version: object) -> Non
manifest.write_text(json.dumps({"schema_version": version, "root": ".", "files": []}), encoding="utf-8")
with pytest.raises(artifact_manifest.ManifestError, match="unsupported schema_version"):
artifact_manifest.verify_manifest(root, manifest)


@pytest.mark.parametrize("content", [
'{"schema_version": 1, "schema_version": 1, "root": ".", "files": []}',
'{"schema_version": 1, "root": ".", "files": [{"path":"x", "path":"y"}]}',
])
def test_manifest_rejects_duplicate_json_keys(tmp_path: Path, content: str) -> None:
root = tmp_path / "artifact"
root.mkdir()
manifest = tmp_path / "manifest.json"
manifest.write_text(content, encoding="utf-8")
with pytest.raises(artifact_manifest.ManifestError, match="duplicate JSON key"):
artifact_manifest.verify_manifest(root, manifest)
Loading