diff options
author | 2021-12-26 13:48:36 -0800 | |
---|---|---|
committer | 2021-12-26 13:48:36 -0800 | |
commit | b419329aeb4dfea7f32e31a39dc5fbd9e96a0571 (patch) | |
tree | 1ccaced580b8ffd9f5434819932e6a7840579116 | |
parent | Add option to pass Python args via main entry point (diff) |
Test argument parsing for entry point
-rw-r--r-- | tests/test_main.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..1017123 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,51 @@ +import contextlib +import io +import unittest +from argparse import Namespace +from unittest.mock import patch + +import snekbox.__main__ as snekbox_main + + +class ArgParseTests(unittest.TestCase): + def test_parse_args(self): + subtests = ( + ( + ["", "code"], + Namespace(code="code", nsjail_args=[], py_args=["-c"]) + ), + ( + ["", "code", "--time_limit", "0"], + Namespace(code="code", nsjail_args=["--time_limit", "0"], py_args=["-c"]) + ), + ( + ["", "code", "---", "-m", "timeit"], + Namespace(code="code", nsjail_args=[], py_args=["-m", "timeit"]) + ), + ( + ["", "code", "--time_limit", "0", "---", "-m", "timeit"], + Namespace(code="code", nsjail_args=["--time_limit", "0"], py_args=["-m", "timeit"]) + ), + ( + ["", "code", "--time_limit", "0", "---"], + Namespace(code="code", nsjail_args=["--time_limit", "0"], py_args=[]) + ), + ( + ["", "code", "---"], + Namespace(code="code", nsjail_args=[], py_args=[]) + ) + ) + + for argv, expected in subtests: + with self.subTest(argv=argv, expected=expected), patch("sys.argv", argv): + args = snekbox_main.parse_args() + self.assertEqual(args, expected) + + @patch("sys.argv", [""]) + def test_parse_args_code_missing_exits(self): + with self.assertRaises(SystemExit) as cm: + with contextlib.redirect_stderr(io.StringIO()) as stderr: + snekbox_main.parse_args() + + self.assertEqual(cm.exception.code, 2) + self.assertIn("the following arguments are required: code", stderr.getvalue()) |