summaryrefslogtreecommitdiff
path: root/tests/test_popen_spawn.py
blob: fca7493d3beea49a6234cd6d0e68aa147e04b48a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
'''
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.

'''
import unittest
import subprocess


import pexpect
from pexpect.popen_spawn import PopenSpawn
from . import PexpectTestCase


class ExpectTestCase (PexpectTestCase.PexpectTestCase):

    def test_expect_basic(self):
        p = PopenSpawn('cat', timeout=5)
        p.sendline(b'Hello')
        p.sendline(b'there')
        p.sendline(b'Mr. Python')
        p.expect(b'Hello')
        p.expect(b'there')
        p.expect(b'Mr. Python')
        p.sendeof()
        p.expect(pexpect.EOF)

    def test_expect_exact_basic(self):
        p = PopenSpawn('cat', timeout=5)
        p.sendline(b'Hello')
        p.sendline(b'there')
        p.sendline(b'Mr. Python')
        p.expect_exact(b'Hello')
        p.expect_exact(b'there')
        p.expect_exact(b'Mr. Python')
        p.sendeof()
        p.expect_exact(pexpect.EOF)

    def test_expect(self):
        the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
                                       stdout=subprocess.PIPE).communicate()[0].rstrip()
        p = PopenSpawn('ls -l /bin')
        the_new_way = b''
        while 1:
            i = p.expect([b'\n', pexpect.EOF])
            the_new_way = the_new_way + p.before
            if i == 1:
                break
            the_new_way += b'\n'
        the_new_way = the_new_way.rstrip()
        assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)

    def test_expect_exact(self):
        the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
                                       stdout=subprocess.PIPE).communicate()[0].rstrip()
        p = PopenSpawn('ls -l /bin')
        the_new_way = b''
        while 1:
            i = p.expect_exact([b'\n', pexpect.EOF])
            the_new_way = the_new_way + p.before
            if i == 1:
                break
            the_new_way += b'\n'
        the_new_way = the_new_way.rstrip()

        assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
        p = PopenSpawn('echo hello.?world')
        i = p.expect_exact(b'.?')
        self.assertEqual(p.before, b'hello')
        self.assertEqual(p.after, b'.?')

    def test_expect_eof(self):
        the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
                                       stdout=subprocess.PIPE).communicate()[0].rstrip()
        p = PopenSpawn('ls -l /bin')
        # This basically tells it to read everything. Same as pexpect.run()
        # function.
        p.expect(pexpect.EOF)
        the_new_way = p.before.rstrip()
        assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)

    def test_expect_timeout(self):
        p = PopenSpawn('cat', timeout=5)
        p.expect(pexpect.TIMEOUT)  # This tells it to wait for timeout.
        self.assertEqual(p.after, pexpect.TIMEOUT)

    def test_unexpected_eof(self):
        p = PopenSpawn('ls -l /bin')
        try:
            p.expect('_Z_XY_XZ')  # Probably never see this in ls output.
        except pexpect.EOF:
            pass
        else:
            self.fail('Expected an EOF exception.')

    def test_bad_arg(self):
        p = PopenSpawn('cat')
        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
            p.expect(1)
        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
            p.expect([1, b'2'])
        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
            p.expect_exact(1)
        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
            p.expect_exact([1, b'2'])

    def test_timeout_none(self):
        p = PopenSpawn('echo abcdef', timeout=None)
        p.expect('abc')
        p.expect_exact('def')
        p.expect(pexpect.EOF)

    def test_crlf(self):
        p = PopenSpawn('echo alpha beta')
        assert p.read() == b'alpha beta' + p.crlf

    def test_crlf_encoding(self):
        p = PopenSpawn('echo alpha beta', encoding='utf-8')
        assert p.read() == 'alpha beta' + p.crlf

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

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