(self, argv, inputs="", close_input=False, expected_exit_code=None)
| 26 | |
| 27 | class CLITests(unittest.TestCase): |
| 28 | def run_main(self, argv, inputs="", close_input=False, expected_exit_code=None): |
| 29 | # Replace sys.stdin with a file-like object backed by a file descriptor |
| 30 | # for compatibility with loop.connect_read_pipe(). |
| 31 | stdin_read_fd, stdin_write_fd = os.pipe() |
| 32 | stdin = io.FileIO(stdin_read_fd) |
| 33 | self.addCleanup(stdin.close) |
| 34 | os.write(stdin_write_fd, inputs.encode()) |
| 35 | if close_input: |
| 36 | os.close(stdin_write_fd) |
| 37 | else: |
| 38 | self.addCleanup(os.close, stdin_write_fd) |
| 39 | # Replace sys.stdout with a file-like object to record outputs. |
| 40 | stdout = io.StringIO() |
| 41 | with patch("sys.stdin", new=stdin), patch("sys.stdout", new=stdout): |
| 42 | # Catch sys.exit() calls when expected. |
| 43 | if expected_exit_code is not None: |
| 44 | with self.assertRaises(SystemExit) as raised: |
| 45 | main(argv) |
| 46 | self.assertEqual(raised.exception.code, expected_exit_code) |
| 47 | else: |
| 48 | main(argv) |
| 49 | return stdout.getvalue() |
| 50 | |
| 51 | def test_version(self): |
| 52 | output = self.run_main(["--version"]) |
no test coverage detected