summaryrefslogtreecommitdiff
path: root/tests/test_misc.py
blob: 3a9e12aa70bb9035e1e11fef773ca579b10d93e4 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/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 pexpect
import unittest
import PexpectTestCase
import os
import sys
import tempfile
import re
import signal
import time

# the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent
_CAT_EOF = b'^D\x08\x08'

class TestCaseMisc(PexpectTestCase.PexpectTestCase):

    def test_isatty (self):
        child = pexpect.spawn('cat')
        assert child.isatty(), "Not returning True. Should always be True."

    def test_read (self):
        child = pexpect.spawn('cat')
        child.sendline ("abc")
        child.sendeof()
        self.assertEqual(child.read(0), b'')
        self.assertEqual(child.read(1), b'a')
        self.assertEqual(child.read(1), b'b')
        self.assertEqual(child.read(1), b'c')
        self.assertEqual(child.read(2), b'\r\n')
        remaining = child.read().replace(_CAT_EOF, b'')
        self.assertEqual(remaining, b'abc\r\n')

    def test_readline (self):
        '''See the note in test_readlines() for an explaination as to why
        I allow line3 and line4 to return multiple patterns.
        Basically, this is done to handle a valid condition on slow systems.
        '''
        child = pexpect.spawn('cat')
        child.sendline ("abc")
        time.sleep(0.5)
        child.sendline ("123")
        time.sleep(0.5)
        child.sendeof()
        line1 = child.readline(0)
        line2 = child.readline()
        line3 = child.readline(2)
        line4 = child.readline(1)
        line5 = child.readline()
        time.sleep(1) # time for child to "complete" ;/
        assert not child.isalive(), child.isalive()
        assert child.exitstatus == 0, child.exitstatus
        self.assertEqual(line1, b'')
        self.assertEqual(line2, b'abc\r\n')
        assert (line3 == b'abc\r\n' or line3 == b'123\r\n'), line3
        assert (line4 == b'123\r\n' or line4 == b'abc\r\n'), line4
        self.assertEqual(line5, b'123\r\n')

    def test_iter (self):
        '''See the note in test_readlines() for an explaination as to why
        I allow line3 and line4 to return multiple patterns.
        Basically, this is done to handle a valid condition on slow systems.
        '''
        child = pexpect.spawn('cat')
        child.sendline ("abc")
        time.sleep(0.5)
        child.sendline ("123")
        time.sleep(0.5)
        child.sendeof()
        # Don't use ''.join() because we want to test the ITERATOR.
        page = b''
        for line in child:
            page += line
        page = page.replace(_CAT_EOF, b'')
        # This is just a really bad test all together, we should write our
        # own 'cat' utility that only writes to stdout after EOF is recv,
        # this must take into consideration all possible platform impl.'s
        # of `cat', and their related terminal and line-buffer handling
        assert (page == b'abc\r\nabc\r\n123\r\n123\r\n' or
                page == b'abc\r\n123\r\nabc\r\n123\r\n' or
                page == b'abc\r\n123abc\r\n\r\n123\r\n') , \
               "iterator did not work. page=%r"(page,)

    def test_readlines(self):
        '''Note that on some slow or heavily loaded systems that the lines
        coming back from 'cat' may come even after the EOF.
        We except to see two copies of the lines we send 'cat'.
        The first line is the TTY echo, the second line is from 'cat'.
        Usually 'cat' will respond with 'abc' before we have a chance to
        send the second line, '123'. If this does not happen then the
        lines may appear out of order. This is technically not an error.
        That's just the nature of asynchronous communication.
        This is why the assert will allow either of the two possible
        patterns to be returned by lineslined(). The (lame) alternative is
        to put a long sleep between the two sendline() calls, but then
        I have to make assumptions about how fast 'cat' can reply.
        '''
        child = pexpect.spawn('cat')
        child.sendline ("abc")
        time.sleep(0.5)
        child.sendline ("123")
        time.sleep(0.5)
        child.sendeof()
        page = b''.join(child.readlines()).replace(_CAT_EOF, b'')
        assert (page == b'abc\r\nabc\r\n123\r\n123\r\n' or
                page == b'abc\r\n123\r\nabc\r\n123\r\n'), (
               "readlines() did not work. page=%r" % (page,))

        time.sleep(1) # time for child to "complete" ;/
        assert not child.isalive(), child.isalive()
        assert child.exitstatus == 0, child.exitstatus

    def test_write (self):
        child = pexpect.spawn('cat')
        child.write('a')
        child.write('\r')
        self.assertEqual(child.readline(), b'a\r\n')

    def test_writelines (self):
        child = pexpect.spawn('cat')
        child.writelines(['abc','123','xyz','\r'])
        child.sendeof()
        line = child.readline()
        assert line == b'abc123xyz\r\n', (
            "writelines() did not work. line=%r" % (line,))

    def test_eof(self):
        child = pexpect.spawn('cat')
        child.sendeof()
        try:
            child.expect ('the unexpected')
        except:
            pass
        assert child.eof(), "child.eof() did not return True"

    def test_terminate(self):
        child = pexpect.spawn('cat')
        child.terminate(force=1)
        assert child.terminated, "child.terminated is not True"

    def test_sighup(self):
        # If a parent process sets an Ignore handler for SIGHUP (as on Fedora's
        # build machines), this test breaks. We temporarily restore the default
        # handler, so the child process will quit. However, we can't simply
        # replace any installed handler, because getsignal returns None for
        # handlers not set in Python code, so we wouldn't be able to restore
        # them.
        if signal.getsignal(signal.SIGHUP) == signal.SIG_IGN:
            signal.signal(signal.SIGHUP, signal.SIG_DFL)
            restore_sig_ign = True
        else:
            restore_sig_ign = False

        try:
            child = pexpect.spawn(sys.executable + ' getch.py', ignore_sighup=True)
            child.expect('READY')
            child.kill(signal.SIGHUP)
            for _ in range(10):
                if not child.isalive():
                    raise AssertionError('Child should not have exited.')
                time.sleep(0.1)

            child = pexpect.spawn(sys.executable + ' getch.py', ignore_sighup=False)
            child.expect('READY')
            child.kill(signal.SIGHUP)
            for _ in range(10):
                if not child.isalive():
                    break
                time.sleep(0.1)
            else:
                raise AssertionError('Child should have exited.')

        finally:
            if restore_sig_ign:
                signal.signal(signal.SIGHUP, signal.SIG_IGN)

    def test_bad_child_pid(self):
        child = pexpect.spawn('cat')
        child.terminate(force=1)
        child.terminated = 0 # Force invalid state to test code
        try:
            child.isalive()
        except pexpect.ExceptionPexpect:
            pass
        else:
            self.fail ("child.isalive() should have raised a pexpect.ExceptionPexpect")
        child.terminated = 1 # Force back to valid state so __del__ won't complain
    def test_bad_arguments (self):
        '''This tests that we get a graceful error when passing bad arguments.'''
        try:
            p = pexpect.spawn(1)
        except pexpect.ExceptionPexpect:
            pass
        else:
            self.fail ("pexpect.spawn(1) should have raised a pexpect.ExceptionPexpect.")
        try:
            p = pexpect.spawn('ls', '-la') # should really use pexpect.spawn('ls', ['-ls'])
        except TypeError:
            pass
        else:
            self.fail ("pexpect.spawn('ls', '-la') should have raised a TypeError.")
        try:
            p = pexpect.spawn('cat')
            p.close()
            p.read_nonblocking(size=1, timeout=3)
        except ValueError:
            pass
        else:
            self.fail ("read_nonblocking on closed spawn object should have raised a ValueError.")
    def test_isalive(self):
        child = pexpect.spawn('cat')
        assert child.isalive(), child.isalive()
        child.sendeof()
        child.expect(pexpect.EOF)
        assert not child.isalive(), child.isalive()
    def test_bad_type_in_expect(self):
        child = pexpect.spawn('cat')
        try:
            child.expect({}) # We don't support dicts yet. Should give TypeError
        except TypeError:
            pass
        else:
            self.fail ("child.expect({}) should have raised a TypeError")

    def test_env(self):
        default = pexpect.run('env')
        userenv = pexpect.run('env', env={'foo':'pexpect'})
        assert default!=userenv, "'default' and 'userenv' should be different"
        assert b'foo' in userenv and b'pexpect' in userenv, "'foo' and 'pexpect' should be in 'userenv'"

    def test_cwd (self): # This assumes 'pwd' and '/tmp' exist on this platform.
        default = pexpect.run('pwd')
        tmpdir =  pexpect.run('pwd', cwd='/tmp')
        assert default!=tmpdir, "'default' and 'tmpdir' should be different"
        assert (b'tmp' in tmpdir), "'tmp' should be returned by 'pwd' command"

    def test_basic_which(self):
        # should find at least 'ls' program, and it should begin with '/'
        exercise = pexpect.which("ls")
        assert exercise is not None and exercise.startswith('/')

    def test_absolute_which(self):
        # make up a path and insert first a non-executable,
        # then, make it executable, and assert we may which() find it.
        fname = 'gcc'
        bin_dir = tempfile.mkdtemp()
        bin_path = os.path.join(bin_dir, fname)
        save_path = os.environ['PATH']
        try:
            # setup
            os.environ['PATH'] = bin_dir
            with open(bin_path, 'w') as fp:
                fp.write('#!/bin/sh\necho hello, world\n')
            os.chmod(bin_path, 0o400)

            # it should not be found because it is not executable
            assert pexpect.which(fname) is None, fname

            # but now it should -- because it is executable
            os.chmod(bin_path, 0o700)
            assert pexpect.which(fname) == bin_path, (fname, bin_path)

        finally:
            # restore,
            os.environ['PATH'] = save_path
            # destroy scratch files and folders,
            if os.path.exists(bin_path):
                os.unlink(bin_path)
            if os.path.exists(bin_dir):
                os.rmdir(bin_dir)

    def test_which_should_not_match_folders(self):
        # make up a path and insert a folder, which is 'executable', which
        # a naive implementation might match (previously pexpect versions
        # 3.2 and sh versions 1.0.8, reported by @lcm337.)
        fname = 'g++'
        bin_dir = tempfile.mkdtemp()
        bin_dir2 = os.path.join(bin_dir, fname)
        save_path = os.environ['PATH']
        try:
            os.environ['PATH'] = bin_dir
            os.mkdir(bin_dir2, 0o755)
            # it should not be found because it is not executable *file*,
            # but rather, has the executable bit set, as a good folder
            # should -- it shouldn't be returned because it fails isdir()
            exercise = pexpect.which(fname)
            assert exercise is None, exercise

        finally:
            # restore,
            os.environ['PATH'] = save_path
            # destroy scratch folders,
            for _dir in (bin_dir2, bin_dir,):
                if os.path.exists(_dir):
                    os.rmdir(_dir)

    def test_which (self):
        p = os.defpath
        ep = os.environ['PATH']
        os.defpath = ":/tmp"
        os.environ['PATH'] = ":/tmp"
        wp = pexpect.which ("ticker.py")
        assert wp == 'ticker.py', "Should return a string. Returned %s" % wp
        os.defpath = "/tmp"
        os.environ['PATH'] = "/tmp"
        wp = pexpect.which ("ticker.py")
        assert wp == None, "Executable should not be found. Returned %s" % wp
        os.defpath = p
        os.environ['PATH'] = ep

    def test_searcher_re (self):
        # This should be done programatically, if we copied and pasted output,
        # there wouldnt be a whole lot to test, really, other than our ability
        # to copy and paste correctly :-)
        ss = pexpect.searcher_re ([
            re.compile('this'), re.compile('that'),
            re.compile('and'), re.compile('the'),
            re.compile('other') ])
        out = ('searcher_re:\n    0: re.compile("this")\n    '
               '1: re.compile("that")\n    2: re.compile("and")\n    '
               '3: re.compile("the")\n    4: re.compile("other")')
        assert ss.__str__() == out, (ss.__str__(), out)
        ss = pexpect.searcher_re ([
            pexpect.TIMEOUT, re.compile('this'),
            re.compile('that'), re.compile('and'),
            pexpect.EOF,re.compile('other')
            ])
        out = ('searcher_re:\n    0: TIMEOUT\n    1: re.compile("this")\n    '
               '2: re.compile("that")\n    3: re.compile("and")\n    '
               '4: EOF\n    5: re.compile("other")')
        assert ss.__str__() == out, (ss.__str__(), out)

    def test_searcher_string (self):
        ss = pexpect.searcher_string ([
            'this', 'that', 'and', 'the', 'other' ])
        out = ('searcher_string:\n    0: "this"\n    1: "that"\n    '
               '2: "and"\n    3: "the"\n    4: "other"')
        assert ss.__str__() == out, (ss.__str__(), out)
        ss = pexpect.searcher_string ([
            'this', pexpect.EOF, 'that', 'and',
            'the', 'other', pexpect.TIMEOUT ])
        out = ('searcher_string:\n    0: "this"\n    1: EOF\n    '
               '2: "that"\n    3: "and"\n    4: "the"\n    '
               '5: "other"\n    6: TIMEOUT')
        assert ss.__str__() == out, (ss.__str__(), out)

    def test_nonnative_pty_fork(self):
        class spawn_ourptyfork(pexpect.spawn):
            def _spawn(self, command, args=[]):
                self.use_native_pty_fork = False
                pexpect.spawn._spawn(self, command, args)

        p = spawn_ourptyfork('cat')
        p.sendline('abc')
        p.expect('abc')
        p.sendeof()

    def test_exception_tb(self):
        p = pexpect.spawn('sleep 1')
        try:
            p.expect('BLAH')
        except pexpect.ExceptionPexpect as e:
            # get_trace should filter out frames in pexpect's own code
            tb = e.get_trace()
            assert 'raise ' not in tb, tb
        else:
            assert False, "Should have raised an exception."

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

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