mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Merge pull request #2278 from kirigayakazima/fix/dev-cli-safety
dev: accept argv, guard stdout.reconfigure, and reject whitespace --out
This commit is contained in:
@ -90,15 +90,15 @@ def guest_scene(args: list[str]) -> int:
|
||||
parser.add_argument("-h", "--help", action="store_true")
|
||||
parser.add_argument("--out", metavar="PATH")
|
||||
options, _ = parser.parse_known_args(args)
|
||||
if not options.help and not options.out:
|
||||
if not options.help and not (options.out and options.out.strip()):
|
||||
parser.error("--out PATH is required to keep the scene results")
|
||||
|
||||
return run_producer(GUEST_SCENE, args)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
# The producer owns its arguments and help. Forward before argparse handles them.
|
||||
argv = sys.argv[1:]
|
||||
argv = sys.argv[1:] if argv is None else list(argv)
|
||||
if argv[:1] == ["boot-trace"]:
|
||||
return run_producer(BOOT_TRACE, argv[1:])
|
||||
if argv[:1] == ["guest-scene"]:
|
||||
@ -119,7 +119,10 @@ def main() -> int:
|
||||
if args.json:
|
||||
print(json.dumps(result, indent=2))
|
||||
else:
|
||||
sys.stdout.reconfigure(encoding="utf-8")
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8")
|
||||
except (AttributeError, ValueError, OSError):
|
||||
pass
|
||||
print("Paths and help commands are relative to the Leonida checkout root.")
|
||||
for tool in result["tools"]:
|
||||
status = "present" if tool["present"] else "missing"
|
||||
|
||||
@ -92,6 +92,33 @@ class DiscoveryTests(unittest.TestCase):
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertTrue(result.stderr)
|
||||
|
||||
def test_main_argv_redirected_stringio_and_whitespace_out(self):
|
||||
import importlib.util
|
||||
import io
|
||||
from contextlib import redirect_stderr, redirect_stdout
|
||||
|
||||
spec = importlib.util.spec_from_file_location("leonida_dev_entry", ENTRY)
|
||||
dev = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(dev)
|
||||
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
rc = dev.main(["discover"])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertIn("boot-trace", out.getvalue())
|
||||
|
||||
out_json = io.StringIO()
|
||||
with redirect_stdout(out_json):
|
||||
rc = dev.main(["discover", "--json"])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertIn("tools", json.loads(out_json.getvalue()))
|
||||
|
||||
err = io.StringIO()
|
||||
with redirect_stderr(err), self.assertRaises(SystemExit) as ctx:
|
||||
dev.main(["guest-scene", "--out", " "])
|
||||
self.assertEqual(ctx.exception.code, 2)
|
||||
self.assertIn("--out PATH is required", err.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user