Files
mandy/tests/test_shader_diff_exit.py
bobcatchris 780a0a479d shader_diff: refuse an option below 1 instead of running nothing
--programs and --limit took bare ints, so 0 or a negative value ran no
program and still printed "0 programs (seed 1): 0 pass, 0 mismatch ..."
with exit 0. That reads as a pass from the differential oracle the docs
tell people to run, and a negative --limit is easy to get from shell
arithmetic. --max-ops had the same gap and died with
ValueError: empty range in randrange(1, 1).

All three now require at least 1: one argparse line, exit 2, which is what
anim_index, rbf0_index, rpf9_carve and shader_disasm already do. Defaults,
the output format and the 2/1/0 exit codes are unchanged, and the new
tests cover both the refusal and a valid --limit still running that many
programs.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-22 05:00:37 -06:00

75 lines
2.7 KiB
Python

"""shader_diff.py exit codes: a harness error is 2, not a mismatch (no GPU, no game data)."""
import contextlib
import io
import os
import sys
import unittest
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import shader_diff as sd # noqa: E402
class HarnessErrorExit(unittest.TestCase):
def test_harness_error_alone_exits_2(self):
def boom(*_a):
raise RuntimeError("broken harness")
with mock.patch.object(sd, "run_one", boom), mock.patch("sys.stdout"):
self.assertEqual(sd.main(["--programs", "2"]), 2)
def test_mismatch_still_exits_1(self):
with mock.patch.object(sd, "run_one", lambda *_a: ("mismatch", "lane word 0")), mock.patch("sys.stdout"):
self.assertEqual(sd.main(["--programs", "1"]), 1)
class ArgumentValidation(unittest.TestCase):
"""--programs, --limit and --max-ops must be at least 1.
Before this, `--limit 0` and `--limit -1` ran no program and printed
"0 programs (seed 1): 0 pass, 0 mismatch ..." with exit 0, which reads as a
pass, and `--max-ops 0` died with `ValueError: empty range in randrange(1, 1)`.
"""
def _rejected(self, argv, option):
err = io.StringIO()
with contextlib.redirect_stderr(err):
with self.assertRaises(SystemExit) as caught:
sd.main(argv)
self.assertEqual(caught.exception.code, 2)
self.assertIn(option, err.getvalue())
self.assertIn("at least 1", err.getvalue())
def test_limit_zero_is_rejected(self):
self._rejected(["--limit", "0"], "--limit")
def test_limit_negative_is_rejected(self):
self._rejected(["--limit", "-5"], "--limit")
def test_programs_zero_is_rejected(self):
self._rejected(["--programs", "0"], "--programs")
def test_programs_negative_is_rejected(self):
self._rejected(["--programs", "-1"], "--programs")
def test_max_ops_zero_is_rejected(self):
self._rejected(["--max-ops", "0"], "--max-ops")
def test_max_ops_negative_is_rejected(self):
self._rejected(["--max-ops", "-2"], "--max-ops")
def test_a_valid_limit_still_runs_that_many_programs(self):
out = io.StringIO()
with mock.patch.object(sd, "run_one", lambda *_a: ("pass", "")), contextlib.redirect_stdout(out):
self.assertEqual(sd.main(["--programs", "5", "--limit", "1"]), 0)
self.assertIn("1 programs", out.getvalue())
def test_the_defaults_are_unchanged(self):
a = sd.main(["--programs", "1", "--limit", "1"])
self.assertIn(a, (0, 1)) # 1 pass or 1 mismatch; never an argparse exit
if __name__ == "__main__":
unittest.main()