|
3 | 3 | # License: MIT
|
4 | 4 | # some functional tests, to see if this is really working
|
5 | 5 |
|
6 |
| -import pytest |
| 6 | +import os |
| 7 | +import signal |
7 | 8 | import sys
|
| 9 | +import textwrap |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +try: |
| 14 | + import pexpect |
| 15 | +except ImportError as exc: |
| 16 | + pytest.skip("could not import pexpect: {}".format(exc), |
| 17 | + allow_module_level=True) |
8 | 18 |
|
9 | 19 |
|
10 | 20 | @pytest.fixture
|
11 |
| -def child(request): |
12 |
| - try: |
13 |
| - pexpect = pytest.importorskip('pexpect') |
14 |
| - except SyntaxError: |
15 |
| - pytest.skip('pexpect wont work on py3k') |
16 |
| - child = pexpect.spawn(sys.executable, ['-S'], timeout=10) |
17 |
| - if sys.version_info >= (3, ): |
18 |
| - child.logfile = sys.stdout.buffer |
19 |
| - else: |
20 |
| - child.logfile = sys.stdout |
21 |
| - child.sendline('from pyrepl.python_reader import main') |
22 |
| - child.sendline('main()') |
| 21 | +def start_child(): |
| 22 | + ret_childs = [] |
| 23 | + |
| 24 | + def start_child_func(env_update=None): |
| 25 | + assert not ret_childs, "child started already" |
| 26 | + |
| 27 | + env = {k: v for k, v in os.environ.items() if k in ( |
| 28 | + "TERM", |
| 29 | + )} |
| 30 | + if env_update: |
| 31 | + env.update(env_update) |
| 32 | + child = pexpect.spawn(sys.executable, timeout=5, env=env) |
| 33 | + if sys.version_info >= (3, ): |
| 34 | + child.logfile = sys.stdout.buffer |
| 35 | + else: |
| 36 | + child.logfile = sys.stdout |
| 37 | + child.expect_exact(">>> ") |
| 38 | + child.sendline('from pyrepl.python_reader import main') |
| 39 | + ret_childs.append(child) |
| 40 | + return child |
| 41 | + |
| 42 | + yield start_child_func |
| 43 | + |
| 44 | + assert ret_childs, "child was not started" |
| 45 | + child = ret_childs[0] |
| 46 | + |
| 47 | + child.sendeof() |
| 48 | + child.expect_exact(">>> ") |
| 49 | + # Verify there's no error, e.g. when signal.SIG_DFL would be called. |
| 50 | + before = child.before.decode() |
| 51 | + assert "Traceback (most recent call last):" not in before |
| 52 | + child.sendeof() |
| 53 | + assert child.wait() == 0 |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture |
| 57 | +def child(start_child): |
| 58 | + child = start_child() |
| 59 | + child.sendline("main()") |
23 | 60 | return child
|
24 | 61 |
|
25 | 62 |
|
26 | 63 | def test_basic(child):
|
27 |
| - child.sendline('a = 3') |
| 64 | + child.expect_exact("->> ") |
| 65 | + child.sendline('a = 40 + 2') |
| 66 | + child.expect_exact("->> ") |
28 | 67 | child.sendline('a')
|
29 |
| - child.expect('3') |
| 68 | + child.expect_exact('42') |
| 69 | + child.expect_exact("->> ") |
| 70 | + |
| 71 | + |
| 72 | +def test_sigwinch_default(child): |
| 73 | + child.expect_exact("->> ") |
| 74 | + os.kill(child.pid, signal.SIGWINCH) |
| 75 | + |
| 76 | + |
| 77 | +def test_sigwinch_forwarded(start_child, tmpdir): |
| 78 | + with open(str(tmpdir.join("initfile")), "w") as initfile: |
| 79 | + initfile.write(textwrap.dedent( |
| 80 | + """ |
| 81 | + import signal |
| 82 | +
|
| 83 | + called = [] |
| 84 | +
|
| 85 | + def custom_handler(signum, frame): |
| 86 | + called.append([signum, frame]) |
| 87 | +
|
| 88 | + signal.signal(signal.SIGWINCH, custom_handler) |
| 89 | +
|
| 90 | + print("PYREPLSTARTUP called") |
| 91 | + """ |
| 92 | + )) |
| 93 | + |
| 94 | + child = start_child(env_update={"PYREPLSTARTUP": initfile.name}) |
| 95 | + child.sendline("main()") |
| 96 | + child.expect_exact("PYREPLSTARTUP called") |
| 97 | + child.expect_exact("->> ") |
| 98 | + os.kill(child.pid, signal.SIGWINCH) |
| 99 | + child.sendline('"called={}".format(len(called))') |
| 100 | + child.expect_exact("called=1") |
0 commit comments