~ubuntu-branches/ubuntu/trusty/pexpect/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/test_run.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio, Thomas Kluyver, Jakub Wilk, Jackson Doak, Andrew Starr-Bochicchio
  • Date: 2013-12-06 20:20:26 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131206202026-9k9oixbv7e8ke30q
Tags: 3.0-1
* Team upload.

[ Thomas Kluyver ]
* New upstream release. Closes: #729518
* Add packaging for Python 3.
* Use pybuild for packaging.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Jackson Doak ]
* Create debian/python3-pexpect.docs

[ Andrew Starr-Bochicchio ]
* Remove empty debian/patches dir.
* Move documentation and examples into a new python-pexpect-doc
  package. They are shared between the Python 2 and Python 3 packages,
  so there is no need to install them with both.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# encoding: utf-8
 
3
'''
 
4
PEXPECT LICENSE
 
5
 
 
6
    This license is approved by the OSI and FSF as GPL-compatible.
 
7
        http://opensource.org/licenses/isc-license.txt
 
8
 
 
9
    Copyright (c) 2012, Noah Spurrier <noah@noah.org>
 
10
    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
 
11
    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
 
12
    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
 
13
    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
14
    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
15
    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
16
    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
17
    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
18
    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
19
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
20
 
 
21
'''
 
22
import pexpect
 
23
import unittest
 
24
import subprocess
 
25
import sys
 
26
import PexpectTestCase
 
27
 
 
28
# TODO Many of these test cases blindly assume that sequential
 
29
# TODO listing of the /bin directory will yield the same results.
 
30
# TODO This may not always be true, but seems adequate for testing for now.
 
31
# TODO I should fix this at some point.
 
32
 
 
33
unicode_type = str if pexpect.PY3 else unicode
 
34
 
 
35
def timeout_callback (d):
 
36
#    print d["event_count"],
 
37
    if d["event_count"]>3:
 
38
        return 1
 
39
    return 0
 
40
 
 
41
class RunFuncTestCase(PexpectTestCase.PexpectTestCase):
 
42
    runfunc = staticmethod(pexpect.run)
 
43
    cr = b'\r'
 
44
    empty = b''
 
45
    prep_subprocess_out = staticmethod(lambda x: x)
 
46
 
 
47
    def test_run_exit (self):
 
48
        (data, exitstatus) = self.runfunc('python exit1.py', withexitstatus=1)
 
49
        assert exitstatus == 1, "Exit status of 'python exit1.py' should be 1."
 
50
 
 
51
    def test_run (self):
 
52
        the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
 
53
                stdout=subprocess.PIPE).communicate()[0].rstrip()
 
54
        (the_new_way, exitstatus) = self.runfunc('ls -l /bin', withexitstatus=1)
 
55
        the_new_way = the_new_way.replace(self.cr, self.empty).rstrip()
 
56
        self.assertEqual(self.prep_subprocess_out(the_old_way), the_new_way)
 
57
        self.assertEqual(exitstatus, 0)
 
58
 
 
59
    def test_run_callback (self): # TODO it seems like this test could block forever if run fails...
 
60
        self.runfunc("cat", timeout=1, events={pexpect.TIMEOUT:timeout_callback})
 
61
 
 
62
    def test_run_bad_exitstatus (self):
 
63
        (the_new_way, exitstatus) = self.runfunc('ls -l /najoeufhdnzkxjd',
 
64
                                                    withexitstatus=1)
 
65
        assert exitstatus != 0
 
66
 
 
67
class RunUnicodeFuncTestCase(RunFuncTestCase):
 
68
    runfunc = staticmethod(pexpect.runu)
 
69
    cr = b'\r'.decode('ascii')
 
70
    empty = b''.decode('ascii')
 
71
    prep_subprocess_out = staticmethod(lambda x: x.decode('utf-8', 'replace'))
 
72
    def test_run_unicode(self):
 
73
        if pexpect.PY3:
 
74
            c = chr(254)   # þ
 
75
            pattern = '<in >'
 
76
        else:
 
77
            c = unichr(254)  # analysis:ignore
 
78
            pattern = '<in >'.decode('ascii')
 
79
 
 
80
        def callback(d):
 
81
            if d['event_count'] == 0:
 
82
                return c + '\n'
 
83
            else:
 
84
                return True  # Stop the child process
 
85
 
 
86
        output = pexpect.runu(sys.executable + ' echo_w_prompt.py',
 
87
                              env={'PYTHONIOENCODING':'utf-8'},
 
88
                              events={pattern:callback})
 
89
        assert isinstance(output, unicode_type), type(output)
 
90
        assert '<out>'+c in output, output
 
91
 
 
92
if __name__ == '__main__':
 
93
    unittest.main()