summaryrefslogtreecommitdiff
path: root/tests/test_interact.py
blob: 76a071e34e4bf2ab1da490fcbd04e2e29aaf8596 (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
#!/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 platform
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 = ('{sys.executable} interact.py'.format(sys=sys))

    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('READY')
        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('READY')
        p.sendcontrol(']')
        p.expect('29<STOP>')
        p.send('\x00')

        if not platform.system() == 'Linux':
            # on Linux, we sometimes miss trailing stdout from the
            # chain of child processes through a subprocess call to
            # interact() method.  This condition so far is only reproduced
            # within these constraints so far, skipped for now.
            p.expect('0<STOP>')
            p.expect_exact('Escaped interact')
        p.expect(pexpect.EOF)
        assert not p.isalive()
        assert p.exitstatus == 0

    def test_interact_exit_unicode(self):
        " Ensure subprocess receives utf8. "
        p = pexpect.spawnu('{self.interact_py} --utf8'.format(self=self),
                           timeout=5, env=self.env)
        p.expect('READY')
        p.send('ɑ')              # >>> map(ord, u'ɑ'.encode('utf8'))
        p.expect('201<STOP>')    # [201, 145]
        p.expect('145<STOP>')
        p.send('Β')              # >>> map(ord, u'Β'.encode('utf8'))
        p.expect('206<STOP>')    # [206, 146]
        p.expect('146<STOP>')
        p.send('\x00')
        if not platform.system() == 'Linux':
            # on Linux, we sometimes miss trailing stdout from the
            # chain of child processes through a subprocess call to
            # interact() method.  This condition so far is only reproduced
            # within these constraints so far, skipped for now.
            p.expect('0<STOP>')
            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')