Add mcoplib mcoplib artifact manifest#49
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new Python script, tools/artifact_manifest.py, designed to generate a reproducible artifact manifest with file sizes and SHA-256 hashes. The feedback suggests two key improvements: first, replacing the assert statement and hardcoded filename in self_test() with tempfile.NamedTemporaryFile and an explicit RuntimeError to prevent silent optimization bypasses and file overwrites; second, validating that the --root path exists and is a directory to avoid silent failures when an invalid path is provided.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| def self_test() -> None: | ||
| sample = Path("_artifact_manifest_sample.txt") | ||
| sample.write_text("maca artifact\n", encoding="utf-8") | ||
| try: | ||
| data = collect(Path.cwd()) | ||
| assert any(item["path"] == sample.name for item in data["artifacts"]) | ||
| print(json.dumps({"ok": True, "count": data["count"]}, ensure_ascii=False)) | ||
| finally: | ||
| sample.unlink(missing_ok=True) |
There was a problem hiding this comment.
Using assert statements for runtime validation or test assertions in executable scripts is risky because they are completely stripped out when Python is run with optimization flags (e.g., python -O). If that happens, the self-test will silently pass even if the sample artifact is missing.
Additionally, writing directly to a hardcoded filename like _artifact_manifest_sample.txt in the current working directory can accidentally overwrite an existing user file.
Using tempfile.NamedTemporaryFile with dir=Path.cwd() and a .txt suffix is a much safer approach. It guarantees a unique filename to prevent overwriting, and raising an explicit RuntimeError ensures the validation is never optimized away.
| def self_test() -> None: | |
| sample = Path("_artifact_manifest_sample.txt") | |
| sample.write_text("maca artifact\n", encoding="utf-8") | |
| try: | |
| data = collect(Path.cwd()) | |
| assert any(item["path"] == sample.name for item in data["artifacts"]) | |
| print(json.dumps({"ok": True, "count": data["count"]}, ensure_ascii=False)) | |
| finally: | |
| sample.unlink(missing_ok=True) | |
| def self_test() -> None: | |
| import tempfile | |
| with tempfile.NamedTemporaryFile(dir=Path.cwd(), suffix=".txt", delete=False) as f: | |
| temp_path = Path(f.name) | |
| f.write(b"maca artifact\n") | |
| try: | |
| data = collect(Path.cwd()) | |
| if not any(item["path"] == temp_path.name for item in data["artifacts"]): | |
| raise RuntimeError(f"Self-test failed: {temp_path.name} not found in artifacts") | |
| print(json.dumps({"ok": True, "count": data["count"]}, ensure_ascii=False)) | |
| finally: | |
| temp_path.unlink(missing_ok=True) |
| args = parser.parse_args() | ||
| if args.self_test: | ||
| self_test() | ||
| return 0 | ||
| print(json.dumps(collect(Path(args.root)), ensure_ascii=False, indent=2)) | ||
| return 0 |
There was a problem hiding this comment.
If the directory specified by --root does not exist or is not a directory, collect will silently return an empty list of artifacts with a success exit code (0). This can be highly misleading to users who might have misspelled the path.
Validating that the path exists and is a directory using parser.error() provides immediate, clear feedback and prevents silent failures.
| args = parser.parse_args() | |
| if args.self_test: | |
| self_test() | |
| return 0 | |
| print(json.dumps(collect(Path(args.root)), ensure_ascii=False, indent=2)) | |
| return 0 | |
| args = parser.parse_args() | |
| if args.self_test: | |
| self_test() | |
| return 0 | |
| root_path = Path(args.root) | |
| if not root_path.is_dir(): | |
| parser.error(f"Root path '{root_path}' is not a directory or does not exist.") | |
| print(json.dumps(collect(root_path), ensure_ascii=False, indent=2)) | |
| return 0 |
a607647 to
76d76c0
Compare
Summary
Validation
Review notes