|
6 | 6 | """ |
7 | 7 |
|
8 | 8 |
|
9 | | -#----------------------------------------------------------------------------- |
10 | | -# Copyright (C) 2009 The IPython Development Team |
11 | | -# |
12 | | -# Distributed under the terms of the BSD License. The full license is in |
13 | | -# the file COPYING, distributed as part of this software. |
14 | | -#----------------------------------------------------------------------------- |
15 | | - |
16 | | -#----------------------------------------------------------------------------- |
17 | | -# Imports |
18 | | -#----------------------------------------------------------------------------- |
| 9 | +# Copyright (c) IPython Development Team. |
| 10 | +# Distributed under the terms of the Modified BSD License. |
19 | 11 |
|
20 | 12 | import os |
21 | 13 | import re |
|
25 | 17 | from contextlib import contextmanager |
26 | 18 | from io import StringIO |
27 | 19 | from subprocess import Popen, PIPE |
| 20 | +from unittest.mock import patch |
28 | 21 |
|
29 | 22 | try: |
30 | 23 | # These tools are used by parts of the runtime, so we make the nose |
|
45 | 38 | from . import decorators as dec |
46 | 39 | from . import skipdoctest |
47 | 40 |
|
48 | | -#----------------------------------------------------------------------------- |
49 | | -# Functions and classes |
50 | | -#----------------------------------------------------------------------------- |
51 | 41 |
|
52 | 42 | # The docstring for full_path doctests differently on win32 (different path |
53 | 43 | # separator) so just skip the doctest there. The example remains informative. |
@@ -443,6 +433,25 @@ def make_tempfile(name): |
443 | 433 | finally: |
444 | 434 | os.unlink(name) |
445 | 435 |
|
| 436 | +def fake_input(inputs): |
| 437 | + """Temporarily replace the input() function to return the given values |
| 438 | +
|
| 439 | + Use as a context manager: |
| 440 | +
|
| 441 | + with fake_input(['result1', 'result2']): |
| 442 | + ... |
| 443 | +
|
| 444 | + Values are returned in order. If input() is called again after the last value |
| 445 | + was used, EOFError is raised. |
| 446 | + """ |
| 447 | + it = iter(inputs) |
| 448 | + def mock_input(prompt=''): |
| 449 | + try: |
| 450 | + return next(it) |
| 451 | + except StopIteration: |
| 452 | + raise EOFError('No more inputs given') |
| 453 | + |
| 454 | + return patch('builtins.input', mock_input) |
446 | 455 |
|
447 | 456 | def help_output_test(subcommand=''): |
448 | 457 | """test that `ipython [subcommand] -h` works""" |
|
0 commit comments