summaryrefslogtreecommitdiff
path: root/tests/test_interact.py
blob: 6b60f8fa0727a9e2afd9639c1b7e12c7cb347e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
PEXPECT LICENSE

    This license is approved by the OSI and FSF as GPL-compatible.
        http://opensource.org/licenses/isc-license.txt

    Copyright (c) 2012, Noah Spurrier <noah@noah.org>
    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

'''
from __future__ import print_function
from __future__ import unicode_literals

import os
import pexpect
import unittest
import sys
from . import PexpectTestCase


class InteractTestCase (PexpectTestCase.PexpectTestCase):
    def setUp(self):
        super(InteractTestCase, self).setUp()
        self.env = env = os.environ.copy()

        # Ensure 'import pexpect' works in subprocess interact*.py
        if 'PYTHONPATH' in env:
            env['PYTHONPATH'] = os.pathsep.join((self.project_dir,
                                                    env['PYTHONPATH']))
        else:
            env['PYTHONPATH'] = self.project_dir

        self.interact_py = ' '.join((sys.executable,
                                     'interact.py',))
        self.interact_ucs_py = ' '.join((sys.executable,
                                         'interact_unicode.py',))

    def test_interact_escape(self):
        " Ensure `escape_character' value exits interactive mode. "
        p = pexpect.spawn(self.interact_py, timeout=5, env=self.env)
        p.expect('<in >')
        p.sendcontrol(']')  # chr(29), the default `escape_character'
                            # value of pexpect.interact().
        p.expect_exact('Escaped interact')
        p.expect(pexpect.EOF)
        assert not p.isalive()
        assert p.exitstatus == 0

    def test_interact_escape_None(self):
        " Return only after Termination when `escape_character=None'. "
        p = pexpect.spawn('{self.interact_py} --no-escape'.format(self=self),
                          timeout=5, env=self.env)
        p.expect('<in >')
        p.sendcontrol(']')
        p.sendline('')
        p.expect('<out>\x1d')
        p.sendcontrol('c')
        p.expect_exact('Escaped interact')
        p.expect(pexpect.EOF)
        assert not p.isalive()
        assert p.exitstatus == 0

    def test_interact_spawn_eof(self):
        " Ensure subprocess receives EOF and exit. "
        p = pexpect.spawn(self.interact_py, timeout=5, env=self.env)
        p.expect('<in >')
        p.sendline(b'alpha')
        p.sendline(b'beta')
        p.expect(b'<out>alpha')
        p.expect(b'<out>beta')
        p.sendeof()
        # strangely, on travis-ci, sendeof() terminates the subprocess,
        # it doesn't receive ^D, just immediately throws EOF.
        idx = p.expect_exact(['<eof>', pexpect.EOF])
        if idx == 0:
            p.expect_exact('Escaped interact')
            p.expect(pexpect.EOF)
        assert not p.isalive()
        assert p.exitstatus == 0

    def test_interact_spawnu_eof(self):
        " Ensure subprocess receives unicode, EOF, and exit. "
        p = pexpect.spawnu(self.interact_ucs_py, timeout=5, env=self.env)
        p.expect('<in >')
        p.sendline('ɑlpha')
        p.sendline('Βeta')
        p.expect('<out>ɑlpha')
        p.expect('<out>Βeta')
        p.sendeof()
        # strangely, on travis-ci, sendeof() terminates the subprocess,
        # it doesn't receive ^D, just immediately throws EOF.
        idx = p.expect_exact(['<eof>', pexpect.EOF])
        if idx == 0:
            p.expect_exact('Escaped interact')
            p.expect(pexpect.EOF)
        assert not p.isalive()
        assert p.exitstatus == 0

if __name__ == '__main__':
    unittest.main()

suite = unittest.makeSuite(InteractTestCase, 'test')