mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
"""Ensure the codegen report describes one object during a concurrent rebuild."""
|
|
|
|
import contextlib
|
|
import hashlib
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import runpy
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
SCRIPT = Path(__file__).resolve().parents[1] / "tools/check_native_codegen.py"
|
|
|
|
|
|
class ReportTests(unittest.TestCase):
|
|
def test_binutils_and_digest_use_the_same_object(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
original = root / "audit.o"
|
|
report = root / "report.json"
|
|
submitted = b"first audit object"
|
|
original.write_bytes(submitted)
|
|
inspected = []
|
|
|
|
def binutils(args, **kwargs):
|
|
inspected.append(Path(args[-1]).read_bytes())
|
|
if len(inspected) == 1:
|
|
rebuilt = root / "rebuilt.o"
|
|
rebuilt.write_bytes(b"replacement audit object")
|
|
rebuilt.replace(original)
|
|
if args[:2] == ("nm", "--defined-only"):
|
|
return "00000000 T AUDIT_ROOT\n"
|
|
return ""
|
|
|
|
with (
|
|
patch.object(sys, "argv", [str(SCRIPT), "--object", str(original),
|
|
"--report", str(report), "--entrypoint", "AUDIT_ROOT"]),
|
|
patch.object(subprocess, "check_output", side_effect=binutils),
|
|
contextlib.redirect_stdout(io.StringIO()),
|
|
):
|
|
with self.assertRaises(SystemExit) as result:
|
|
runpy.run_path(str(SCRIPT), run_name="__main__")
|
|
|
|
evidence = json.loads(report.read_text(encoding="utf-8"))
|
|
self.assertEqual(result.exception.code, 0)
|
|
self.assertEqual(evidence["object_sha256"], hashlib.sha256(submitted).hexdigest())
|
|
self.assertEqual(inspected, [submitted] * 5)
|
|
self.assertEqual(original.read_bytes(), b"replacement audit object")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|