"""Unit tests for tools/cpu_feature_scan.py on hand-written dumpbin-style listings and a small synthetic PE. The listings are written here line by line and the PE carries only headers and a .pdata table, so the test needs no game files, no dumpbin and no third-party packages. python -I tests/test_cpu_feature_scan.py """ import io import json import os import struct import sys import tempfile import unittest from contextlib import redirect_stderr, redirect_stdout from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOLS_DIR = os.path.join(ROOT, "tools") if TOOLS_DIR not in sys.path: sys.path.insert(0, TOOLS_DIR) import cpu_feature_scan as scan # noqa: E402 BASE = 0x140000000 def listing(*rows): """Rows are (rva, 'mnemonic operands'); written as `dumpbin /disasm:nobytes` prints them.""" out = ["Dump of file synthetic.exe\n", "\n", "File Type: EXECUTABLE IMAGE\n", "\n"] for rva, text in rows: mnem, _, ops = text.partition(" ") out.append(f" {BASE + rva:016X}: {mnem:<11} {ops}".rstrip() + "\n") return out def pe_with_pdata(ranges, image_base=BASE, size_override=None): """An x64 PE whose only content is a .pdata section holding `ranges` as RUNTIME_FUNCTION entries.""" pdata = b"".join(struct.pack("", err.getvalue()) def test_unreadable_source_exits_2_without_traceback(self): with mock.patch.object(scan.inline_scan, "read_lines", side_effect=PermissionError("denied")): code, out, err = self.run_main(self.listing) self.assertEqual(code, 2) self.assertEqual(out, "") self.assertIn("denied", err) self.assertNotIn("Traceback", err) if __name__ == "__main__": unittest.main()