| 59 | self.skipTest(f"CLI {self.root_module} has not been installed -- skipping tests.") |
| 60 | |
| 61 | def test_basic(self): |
| 62 | # Test that invoking an invalid command group or command exits with error. |
| 63 | with mock.patch("sys.stderr", new=StringIO()) as stderr: |
| 64 | with self.assertRaises(SystemExit) as e: |
| 65 | _parse([self.root_module, "unknown"]) |
| 66 | self.assertEqual(e.exception.code, 2) |
| 67 | self.assertIn( |
| 68 | "invalid choice: 'unknown' (choose from 'child1', 'child2')", stderr.getvalue() |
| 69 | ) |
| 70 | |
| 71 | # Test that invoking CLI by itself prints the help message. |
| 72 | returncode, stdout, stderr = _run(_parse([self.root_module])) |
| 73 | self.assertRegex(stdout, r"usage: .*") |
| 74 | self.assertEqual(returncode, 0, msg=stderr) |
| 75 | |
| 76 | # Test that invoking subcommand by itself injects --help. |
| 77 | args = _parse([self.root_module, "child1"]) |
| 78 | self.assertEqual(args.command_type, CommandType.HELP) |
| 79 | |
| 80 | def test_help(self): |
| 81 | # Test that passing --help exits without error. |