diff --git a/tests/test_recomp_census.py b/tests/test_recomp_census.py index c4130257cd..a7eea32f07 100644 --- a/tests/test_recomp_census.py +++ b/tests/test_recomp_census.py @@ -131,5 +131,61 @@ def test_non_x64_rejected(self): os.unlink(path) +class WrongInputTests(unittest.TestCase): + """A wrong input is one ERROR line and exit 2, not a traceback.""" + + def run_main(self, argv): + import contextlib + import io + err, out = io.StringIO(), io.StringIO() + with contextlib.redirect_stderr(err), contextlib.redirect_stdout(out): + code = rc.main(argv) + return code, err.getvalue(), out.getvalue() + + def assert_error_line(self, code, err): + self.assertEqual(code, 2) + self.assertTrue(err.startswith("ERROR: "), err) + self.assertEqual(err.count("\n"), 1, err) + self.assertNotIn("Traceback", err) + + def test_missing_image(self): + with tempfile.TemporaryDirectory() as tmp: + code, err, _ = self.run_main([os.path.join(tmp, "missing.exe"), "--out", tmp]) + self.assert_error_line(code, err) + self.assertEqual(os.listdir(tmp), []) + + def test_folder_as_image(self): + with tempfile.TemporaryDirectory() as tmp: + folder = os.path.join(tmp, "image") + os.mkdir(folder) + code, err, _ = self.run_main([folder, "--out", tmp]) + self.assert_error_line(code, err) + self.assertEqual(os.listdir(tmp), ["image"]) + + def test_out_is_a_file(self): + with tempfile.TemporaryDirectory() as tmp: + image = os.path.join(tmp, "good.exe") + with open(image, "wb") as f: + f.write(build_pe()) + out = os.path.join(tmp, "out") + with open(out, "wb") as f: + f.write(b"keep") + code, err, _ = self.run_main([image, "--out", out]) + self.assert_error_line(code, err) + with open(out, "rb") as f: + self.assertEqual(f.read(), b"keep") + + def test_good_image_still_writes_both_reports(self): + with tempfile.TemporaryDirectory() as tmp: + image = os.path.join(tmp, "good.exe") + with open(image, "wb") as f: + f.write(build_pe()) + out = os.path.join(tmp, "out") + code, err, stdout = self.run_main([image, "--out", out]) + self.assertEqual(code, 0, err) + self.assertEqual(sorted(os.listdir(out)), ["good.exe.recomp.json", "good.exe.recomp.md"]) + self.assertIn("3 fns", stdout) + + if __name__ == "__main__": unittest.main() diff --git a/tools/recomp_census.py b/tools/recomp_census.py index 1363e67989..5b37d816a6 100644 --- a/tools/recomp_census.py +++ b/tools/recomp_census.py @@ -14,8 +14,9 @@ * what are the blockers: decode gaps, unhandled mnemonics, indirect control flow that needs the pc-table dispatch, x87/SIMD/sys instructions? -Writes /.json and /.md. Standard library only; reads the -file, never runs it. +Writes /.recomp.json and /.recomp.md. A wrong input or an +--out that cannot be written is one ERROR line and exit code 2. Standard library +only; reads the file, never runs it. """ from __future__ import annotations @@ -205,14 +206,21 @@ def main(argv=None): except pe_inspect.PeFormatError as e: print(f"{a.image}: {e}", file=sys.stderr) return 1 + except OSError as e: # missing image, a folder, no permission: one line, not a traceback + print(f"ERROR: {e}", file=sys.stderr) + return 2 r["top_mnemonics"] = r["top_mnemonics"][:a.top] r["unhandled"] = r["unhandled"][:a.top] - os.makedirs(a.out, exist_ok=True) name = os.path.basename(a.image) - with open(os.path.join(a.out, name + ".recomp.json"), "w", encoding="utf-8", newline="\n") as f: - json.dump(r, f, indent=1) - with open(os.path.join(a.out, name + ".recomp.md"), "w", encoding="utf-8", newline="\n") as f: - f.write(to_markdown(r)) + try: # --out naming a file, or a folder that cannot be written + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, name + ".recomp.json"), "w", encoding="utf-8", newline="\n") as f: + json.dump(r, f, indent=1) + with open(os.path.join(a.out, name + ".recomp.md"), "w", encoding="utf-8", newline="\n") as f: + f.write(to_markdown(r)) + except OSError as e: + print(f"ERROR: {e}", file=sys.stderr) + return 2 print(f"{r['file']}: {r['pdata_functions']:,} fns, {r['insns']:,} insns, " f"{r['insn_coverage_pct']}% covered, {len(r['unhandled'])} hazard kinds") return 0