diff options
author | jquast <contact@jeffquast.com> | 2013-09-22 19:32:33 -0700 |
---|---|---|
committer | jquast <contact@jeffquast.com> | 2013-09-22 19:32:33 -0700 |
commit | c851a110a61f20c2163aacdf6afe210d572d777b (patch) | |
tree | 6b07f4c3638b22fe5552efe333e0397fa4d9c8fd | |
parent | a83cf59505e18fd4ee0c84c4a1dd724e6f916e16 (diff) | |
download | pexpect-c851a110a61f20c2163aacdf6afe210d572d777b.tar.gz |
py2.5 compatibilities w/six.py
-except Exception as e:
+except Exception, err:
the unfortunate use of six.b('') instead of b''
print(arg0, arg1) => six.print_(arg0, arg1)
some autopep8 -i is definitely called for, some of these test cases are darn unreadable, but did partially pep8 some of the really-long-over-80col-lines.
-rw-r--r-- | tests/PexpectTestCase.py | 5 | ||||
-rwxr-xr-x | tests/adhoc.py | 11 | ||||
-rwxr-xr-x | tests/sigwinch_report.py | 5 | ||||
-rwxr-xr-x | tests/test_ctrl_chars.py | 9 | ||||
-rwxr-xr-x | tests/test_dotall.py | 5 | ||||
-rwxr-xr-x | tests/test_expect.py | 292 | ||||
-rwxr-xr-x | tests/test_filedescriptor.py | 7 | ||||
-rwxr-xr-x | tests/test_interact.py | 13 | ||||
-rwxr-xr-x | tests/test_isalive.py | 2 | ||||
-rwxr-xr-x | tests/test_log.py | 15 | ||||
-rwxr-xr-x | tests/test_misc.py | 49 | ||||
-rwxr-xr-x | tests/test_performance.py | 63 | ||||
-rwxr-xr-x | tests/test_run.py | 6 | ||||
-rwxr-xr-x | tests/test_run_out_of_pty.py | 8 | ||||
-rwxr-xr-x | tests/test_timeout_pattern.py | 17 | ||||
-rw-r--r-- | tests/test_unicode.py | 2 | ||||
-rwxr-xr-x | tests/test_winsize.py | 10 |
17 files changed, 297 insertions, 222 deletions
diff --git a/tests/PexpectTestCase.py b/tests/PexpectTestCase.py index cfdcd58..1e5123d 100644 --- a/tests/PexpectTestCase.py +++ b/tests/PexpectTestCase.py @@ -18,11 +18,10 @@ PEXPECT LICENSE OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ''' -from __future__ import print_function - import unittest import sys import os +from pexpect import six class PexpectTestCase(unittest.TestCase): def setUp(self): @@ -30,7 +29,7 @@ class PexpectTestCase(unittest.TestCase): self.original_path = os.getcwd() newpath = os.path.join (os.environ['PROJECT_PEXPECT_HOME'], 'tests') os.chdir (newpath) - print('\n', self.id(), end='') + six.print_('\n', self.id(), end='') unittest.TestCase.setUp(self) def tearDown(self): os.chdir (self.original_path) diff --git a/tests/adhoc.py b/tests/adhoc.py index 1e7941e..a69553a 100755 --- a/tests/adhoc.py +++ b/tests/adhoc.py @@ -26,9 +26,10 @@ print(p.exitstatus) p.expect (pexpect.EOF) print(p.before) time.sleep(1) -print('exitstatus:', p.exitstatus) -print('isalive',p.isalive()) -print('exitstatus',p.exitstatus) -print('isalive',p.isalive()) -print('exitstatus',p.exitstatus) +from pexpect import six +six.print_('exitstatus:', p.exitstatus) +six.print_('isalive',p.isalive()) +six.print_('exitstatus',p.exitstatus) +six.print_('isalive',p.isalive()) +six.print_('exitstatus',p.exitstatus) diff --git a/tests/sigwinch_report.py b/tests/sigwinch_report.py index b9b4ff1..ae8c680 100755 --- a/tests/sigwinch_report.py +++ b/tests/sigwinch_report.py @@ -17,9 +17,8 @@ PEXPECT LICENSE OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ''' -from __future__ import print_function - import signal, time, struct, fcntl, termios, sys +from pexpect import six def getwinsize(): '''This returns the window size of the child tty. @@ -36,7 +35,7 @@ def getwinsize(): def handler(signum, frame): print('signal') sys.stdout.flush() - print('SIGWINCH:', getwinsize ()) + six.print_('SIGWINCH:', getwinsize ()) sys.stdout.flush() print("setting handler for SIGWINCH") diff --git a/tests/test_ctrl_chars.py b/tests/test_ctrl_chars.py index fe0f4ff..f5f016e 100755 --- a/tests/test_ctrl_chars.py +++ b/tests/test_ctrl_chars.py @@ -44,9 +44,9 @@ class TestCtrlChars(PexpectTestCase.PexpectTestCase): # child.send(unicode('%d'%i, encoding='utf-8')) child.send(chr(i)) child.expect ('%d\r\n' % i) - except Exception as e: + except Exception, err: msg = "Did not echo character value: " + str(i) + "\n" - msg = msg + str(e) + msg = msg + str(err) self.fail(msg) def test_sendintr (self): @@ -54,9 +54,8 @@ class TestCtrlChars(PexpectTestCase.PexpectTestCase): child = pexpect.spawn('python getch.py') child.sendintr() child.expect ('3\r\n') - except Exception as e: - msg = "Did not echo character value: 3\n" - msg = msg + str(e) + except Exception, err: + msg = " ".join("Did not echo character value: 3\n", str(err)) self.fail(msg) def test_bad_sendcontrol_chars (self): diff --git a/tests/test_dotall.py b/tests/test_dotall.py index 7f574b3..f487cec 100755 --- a/tests/test_dotall.py +++ b/tests/test_dotall.py @@ -22,17 +22,18 @@ import pexpect import unittest import re import PexpectTestCase +from pexpect import six testdata = 'BEGIN\nHello world\nEND' class TestCaseDotall(PexpectTestCase.PexpectTestCase): def test_dotall (self): p = pexpect.spawn('echo "%s"' % testdata) - i = p.expect ([b'BEGIN(.*)END', pexpect.EOF]) + i = p.expect ([six.b('BEGIN(.*)END'), pexpect.EOF]) assert i==0, 'DOTALL does not seem to be working.' def test_precompiled (self): p = pexpect.spawn('echo "%s"' % testdata) - pat = re.compile(b'BEGIN(.*)END') # This overrides the default DOTALL. + pat = re.compile(six.b('BEGIN(.*)END')) # This overrides the default DOTALL. i = p.expect ([pat, pexpect.EOF]) assert i==1, 'Precompiled pattern to override DOTALL does not seem to be working.' diff --git a/tests/test_expect.py b/tests/test_expect.py index 1328966..d893449 100755 --- a/tests/test_expect.py +++ b/tests/test_expect.py @@ -24,6 +24,8 @@ import unittest import subprocess import time import PexpectTestCase +from pexpect import six +import platform #import pdb # Many of these test cases blindly assume that sequential directory @@ -45,23 +47,23 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def test_expect_basic (self): p = pexpect.spawn('cat') - 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.sendline (six.b('Hello')) + p.sendline (six.b('there')) + p.sendline (six.b('Mr. Python')) + p.expect (six.b('Hello')) + p.expect (six.b('there')) + p.expect (six.b('Mr. Python')) p.sendeof () p.expect (pexpect.EOF) def test_expect_exact_basic (self): p = pexpect.spawn('cat') - 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.sendline (six.b('Hello')) + p.sendline (six.b('there')) + p.sendline (six.b('Mr. Python')) + p.expect_exact (six.b('Hello')) + p.expect_exact (six.b('there')) + p.expect_exact (six.b('Mr. Python')) p.sendeof () p.expect_exact (pexpect.EOF) @@ -70,10 +72,10 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): even if case is different using the regex (?i) directive. ''' p = pexpect.spawn('cat') - p.sendline (b'HELLO') - p.sendline (b'there') - p.expect (b'(?i)hello') - p.expect (b'(?i)THERE') + p.sendline (six.b('HELLO')) + p.sendline (six.b('there')) + p.expect (six.b('(?i)hello')) + p.expect (six.b('(?i)THERE')) p.sendeof () p.expect (pexpect.EOF) @@ -83,10 +85,10 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): ''' p = pexpect.spawn('cat') p.ignorecase = True - p.sendline (b'HELLO') - p.sendline (b'there') - p.expect (b'hello') - p.expect (b'THERE') + p.sendline (six.b('HELLO')) + p.sendline (six.b('there')) + p.expect (six.b('hello')) + p.expect (six.b('THERE')) p.sendeof () p.expect (pexpect.EOF) @@ -107,26 +109,67 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): self._expect_order(p) def _expect_order (self, p): - p.sendline (b'1234') - p.sendline (b'abcd') - p.sendline (b'wxyz') - p.sendline (b'7890') + p.sendline (six.b('1234')) + p.sendline (six.b('abcd')) + p.sendline (six.b('wxyz')) + p.sendline (six.b('7890')) p.sendeof () - index = p.expect ([b'1234',b'abcd',b'wxyz',pexpect.EOF,b'7890']) + index = p.expect ([ + six.b('1234'), + six.b('abcd'), + six.b('wxyz'), + pexpect.EOF, + six.b('7890') ]) assert index == 0, "index="+str(index) - index = p.expect ([b'1234',b'abcd',b'wxyz',pexpect.EOF,b'7890']) + index = p.expect ([ + six.b('1234'), + six.b('abcd'), + six.b('wxyz'), + pexpect.EOF, + six.b('7890') ]) assert index == 0, "index="+str(index) - index = p.expect ([pexpect.EOF,pexpect.TIMEOUT,b'wxyz',b'abcd',b'1234']) + index = p.expect ([ + pexpect.EOF, + pexpect.TIMEOUT, + six.b('wxyz'), + six.b('abcd'), + six.b('1234') ]) assert index == 3, "index="+str(index) - index = p.expect ([b'54321',pexpect.TIMEOUT,b'1234',b'abcd',b'wxyz',pexpect.EOF], timeout=5) + index = p.expect ([ + six.b('54321'), + pexpect.TIMEOUT, + six.b('1234'), + six.b('abcd'), + six.b('wxyz'), + pexpect.EOF], timeout=5) assert index == 3, "index="+str(index) - index = p.expect ([b'54321',pexpect.TIMEOUT,b'1234',b'abcd',b'wxyz',pexpect.EOF], timeout=5) + index = p.expect ([ + six.b('54321'), + pexpect.TIMEOUT, + six.b('1234'), + six.b('abcd'), + six.b('wxyz'), + pexpect.EOF], timeout=5) assert index == 4, "index="+str(index) - index = p.expect ([b'54321',pexpect.TIMEOUT,b'1234',b'abcd',b'wxyz',pexpect.EOF], timeout=5) + index = p.expect ([ + six.b('54321'), + pexpect.TIMEOUT, + six.b('1234'), + six.b('abcd'), + six.b('wxyz'), + pexpect.EOF], timeout=5) assert index == 4, "index="+str(index) - index = p.expect ([pexpect.EOF,b'abcd',b'wxyz',b'7890']) + index = p.expect ([ + pexpect.EOF, + six.b('abcd'), + six.b('wxyz'), + six.b('7890') ]) assert index == 3, "index="+str(index) - index = p.expect ([pexpect.EOF,b'abcd',b'wxyz',b'7890']) + index = p.expect ([ + pexpect.EOF, + six.b('abcd'), + six.b('wxyz'), + six.b('7890') ]) assert index == 3, "index="+str(index) def test_waitnoecho (self): @@ -171,23 +214,41 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): self._expect_echo(p) def _expect_echo (self, p): - p.sendline (b'1234') # Should see this twice (once from tty echo and again from cat). - index = p.expect ([b'1234',b'abcd',b'wxyz',pexpect.EOF,pexpect.TIMEOUT]) + p.sendline (six.b('1234')) # Should see this twice (once from tty echo and again from cat). + index = p.expect ([ + six.b('1234'), + six.b('abcd'), + six.b('wxyz'), + pexpect.EOF, + pexpect.TIMEOUT]) assert index == 0, "index="+str(index)+"\n"+p.before - index = p.expect ([b'1234',b'abcd',b'wxyz',pexpect.EOF]) + index = p.expect ([ + six.b('1234'), + six.b('abcd'), + six.b('wxyz'), + pexpect.EOF]) assert index == 0, "index="+str(index) p.setecho(0) # Turn off tty echo - p.sendline (b'abcd') # Now, should only see this once. - p.sendline (b'wxyz') # Should also be only once. - index = p.expect ([pexpect.EOF,pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234']) + p.sendline (six.b('abcd')) # Now, should only see this once. + p.sendline (six.b('wxyz')) # Should also be only once. + index = p.expect ([ + pexpect.EOF, + pexpect.TIMEOUT, + six.b('abcd'), + six.b('wxyz'), + six.b('1234')]) assert index == 2, "index="+str(index) - index = p.expect ([pexpect.EOF,b'abcd',b'wxyz',b'7890']) + index = p.expect ([ + pexpect.EOF, + six.b('abcd'), + six.b('wxyz'), + six.b('7890')]) assert index == 2, "index="+str(index) p.setecho(1) # Turn on tty echo - p.sendline (b'7890') # Should see this twice. - index = p.expect ([pexpect.EOF,b'abcd',b'wxyz',b'7890']) + p.sendline (six.b('7890')) # Should see this twice. + index = p.expect ([pexpect.EOF,six.b('abcd'),six.b('wxyz'),six.b('7890')]) assert index == 3, "index="+str(index) - index = p.expect ([pexpect.EOF,b'abcd',b'wxyz',b'7890']) + index = p.expect ([pexpect.EOF,six.b('abcd'),six.b('wxyz'),six.b('7890')]) assert index == 3, "index="+str(index) p.sendeof() @@ -208,60 +269,63 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def _expect_index (self, p): p.setecho(0) - p.sendline (b'1234') - index = p.expect ([b'abcd',b'wxyz',b'1234',pexpect.EOF]) + p.sendline (six.b('1234')) + index = p.expect ([six.b('abcd'),six.b('wxyz'),six.b('1234'),pexpect.EOF]) assert index == 2, "index="+str(index) - p.sendline (b'abcd') - index = p.expect ([pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF]) + p.sendline (six.b('abcd')) + index = p.expect ([pexpect.TIMEOUT,six.b('abcd'),six.b('wxyz'),six.b('1234'),pexpect.EOF]) assert index == 1, "index="+str(index) - p.sendline (b'wxyz') - index = p.expect ([b'54321',pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF], timeout=5) + p.sendline (six.b('wxyz')) + index = p.expect ([six.b('54321'),pexpect.TIMEOUT,six.b('abcd'),six.b('wxyz'),six.b('1234'),pexpect.EOF], timeout=5) assert index == 3, "index="+str(index) # Expect 'wxyz' - p.sendline (b'$*!@?') - index = p.expect ([b'54321',pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF], timeout=5) + p.sendline (six.b('$*!@?')) + index = p.expect ([six.b('54321'),pexpect.TIMEOUT,six.b('abcd'),six.b('wxyz'),six.b('1234'),pexpect.EOF], timeout=5) assert index == 1, "index="+str(index) # Expect TIMEOUT p.sendeof () - index = p.expect ([b'54321',pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF], timeout=5) + index = p.expect ([six.b('54321'),pexpect.TIMEOUT,six.b('abcd'),six.b('wxyz'),six.b('1234'),pexpect.EOF], timeout=5) assert index == 5, "index="+str(index) # Expect EOF def test_expect (self): - the_old_way = subprocess.check_output(['ls', '-l', '/bin']).rstrip() + the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'], + stdout=subprocess.PIPE).communicate()[0].rstrip() p = pexpect.spawn('ls -l /bin') - the_new_way = b'' + the_new_way = six.b('') while 1: - i = p.expect (['\n', pexpect.EOF]) + i = p.expect ([six.b('\n'), pexpect.EOF]) the_new_way = the_new_way + p.before if i == 1: break the_new_way = the_new_way.rstrip() - the_new_way = the_new_way.replace(b'\r',b'\n') + the_new_way = the_new_way.replace(six.b('\r'),six.b('\n')) # For some reason I get an extra newline under OS X evey once in a while. # I found it by looking through the hex_dump(). assert the_old_way == the_new_way, hex_dump(the_new_way) + "\n" + hex_dump(the_old_way) def test_expect_exact (self): - the_old_way = subprocess.check_output(['ls', '-l', '/bin']).rstrip() + the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'], + stdout=subprocess.PIPE).communicate()[0].rstrip() p = pexpect.spawn('ls -l /bin') - the_new_way = b'' + the_new_way = six.b('') while 1: - i = p.expect_exact ([b'\n', pexpect.EOF]) + i = p.expect_exact ([six.b('\n'), pexpect.EOF]) the_new_way = the_new_way + p.before if i == 1: break the_new_way = the_new_way.rstrip() - the_new_way = the_new_way.replace(b'\r',b'\n') + the_new_way = the_new_way.replace(six.b('\r'),six.b('\n')) self.assertEqual(the_old_way, the_new_way) p = pexpect.spawn('echo hello.?world') - i = p.expect_exact(b'.?') - self.assertEqual(p.before, b'hello') - self.assertEqual(p.after, b'.?') + i = p.expect_exact(six.b('.?')) + self.assertEqual(p.before, six.b('hello')) + self.assertEqual(p.after, six.b('.?')) def test_expect_eof (self): - the_old_way = subprocess.check_output(['/bin/ls', '-l', '/bin']).rstrip() + the_old_way = subprocess.Popen(args=['/bin/ls', '-l', '/bin'], + stdout=subprocess.PIPE).communicate()[0].rstrip() p = pexpect.spawn('/bin/ls -l /bin') p.expect(pexpect.EOF) # This basically tells it to read everything. Same as pexpect.run() function. the_new_way = p.before - the_new_way = the_new_way.replace(b'\r',b'') # Remember, pty line endings are '\r\n'. + the_new_way = the_new_way.replace(six.b('\r'),six.b('')) # Remember, pty line endings are '\r\n'. the_new_way = the_new_way.rstrip() self.assertEqual(the_old_way, the_new_way) @@ -282,19 +346,19 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def _before_after(self, p): p.timeout = 5 - p.expect(b'>>> ') - self.assertEqual(p.after, b'>>> ') - assert p.before.startswith(b'Python '), p.before + p.expect(six.b('>>> ')) + self.assertEqual(p.after, six.b('>>> ')) + assert p.before.startswith(six.b('Python ')), p.before - p.sendline(b'list(range(4*3))') + p.sendline(six.b('list(range(4*3))')) - p.expect(b'5') - self.assertEqual(p.after, b'5') - assert p.before.startswith(b'list(range(4*3))'), p.before + p.expect(six.b('5')) + self.assertEqual(p.after, six.b('5')) + assert p.before.startswith(six.b('list(range(4*3))')), p.before - p.expect(b'>>> ') - self.assertEqual(p.after, b'>>> ') - assert p.before.startswith(b', 6, 7, 8'), p.before + p.expect(six.b('>>> ')) + self.assertEqual(p.after, six.b('>>> ')) + assert p.before.startswith(six.b(', 6, 7, 8')), p.before def test_before_after(self): '''This tests expect() for some simple before/after things. @@ -313,26 +377,26 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def _ordering(self, p): p.timeout = 5 - p.expect(b'>>> ') + p.expect(six.b('>>> ')) p.sendline('list(range(4*3))') - self.assertEqual(p.expect([b'5,', b'5,']), 0) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('5,'), six.b('5,')]), 0) + p.expect(six.b('>>> ')) - p.sendline(b'list(range(4*3))') - self.assertEqual(p.expect([b'7,', b'5,']), 1) - p.expect(b'>>> ') + p.sendline(six.b('list(range(4*3))')) + self.assertEqual(p.expect([six.b('7,'), six.b('5,')]), 1) + p.expect(six.b('>>> ')) - p.sendline(b'list(range(4*3))') - self.assertEqual(p.expect([b'5,', b'7,']), 0) - p.expect(b'>>> ') + p.sendline(six.b('list(range(4*3))')) + self.assertEqual(p.expect([six.b('5,'), six.b('7,')]), 0) + p.expect(six.b('>>> ')) - p.sendline(b'list(range(4*5))') - self.assertEqual(p.expect([b'2,', b'12,']), 0) - p.expect(b'>>> ') + p.sendline(six.b('list(range(4*5))')) + self.assertEqual(p.expect([six.b('2,'), six.b('12,')]), 0) + p.expect(six.b('>>> ')) - p.sendline(b'list(range(4*5))') - self.assertEqual(p.expect([b'12,', b'2,']), 1) + p.sendline(six.b('list(range(4*5))')) + self.assertEqual(p.expect([six.b('12,'), six.b('2,')]), 1) def test_ordering(self): '''This tests expect() for which pattern is returned @@ -356,51 +420,57 @@ class ExpectTestCase (PexpectTestCase.PexpectTestCase): def _greed(self, p): p.timeout = 5 - p.expect(b'>>> ') - p.sendline(b'import time') - p.expect(b'>>> ') + p.expect(six.b('>>> ')) + p.sendline(six.b('import time')) + p.expect(six.b('>>> ')) # the newline and sleep will (I hope) guarantee that # pexpect is fed two distinct batches of data, # "foo\r\n" + "bar\r\n". - foo_then_bar = b'print("f"+"o"+"o") ; time.sleep(1); print("b"+"a"+"r")' + foo_then_bar = six.b('print("f"+"o"+"o") ; time.sleep(1); print("b"+"a"+"r")') p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'foo\r\nbar']), 0) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('foo\r\nbar')]), 0) + p.expect(six.b('>>> ')) p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'\r\nbar']), 0) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('\r\nbar')]), 0) + p.expect(six.b('>>> ')) p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'foo\r\nbar', b'foo', b'bar']), 1) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('foo\r\nbar'), six.b('foo'), six.b('bar')]), 1) + p.expect(six.b('>>> ')) p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'foo', b'foo\r\nbar', b'foo', b'bar']), 0) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('foo'), six.b('foo\r\nbar'), six.b('foo'), six.b('bar')]), 0) + p.expect(six.b('>>> ')) p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'bar', b'foo\r\nbar']), 1) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('bar'), six.b('foo\r\nbar')]), 1) + p.expect(six.b('>>> ')) # If the expect works as if we rematch for every new character, - # 'o\r\nb' should win over 'oo\r\nba'. The latter is longer and + # 'o\r\nsix.b(' should win over ')oo\r\nba'. The latter is longer and # matches earlier in the input, but isn't satisfied until the 'a' # arrives. # However, pexpect doesn't do that (version 2.1 didn't). p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'oo\r\nba', b'o\r\nb']), 0) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('oo\r\nba'), six.b('o\r\nb')]), 0) + p.expect(six.b('>>> ')) # distinct patterns, but both suddenly match when the 'r' arrives. p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'foo\r\nbar', b'ar']), 0) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('foo\r\nbar'), six.b('ar')]), 0) + p.expect(six.b('>>> ')) p.sendline(foo_then_bar) - self.assertEqual(p.expect([b'ar', b'foo\r\nbar']), 1) - p.expect(b'>>> ') + self.assertEqual(p.expect([six.b('ar'), six.b('foo\r\nbar')]), 1) + p.expect(six.b('>>> ')) + + try: + p.expect(12345) + assert False, 'TypeError should have been raised' + except TypeError, err: + assert err.message == 'x', err.message def test_greed(self): p = pexpect.spawn(self.PYTHONBIN) @@ -417,10 +487,10 @@ if __name__ == '__main__': suite = unittest.makeSuite(ExpectTestCase,'test') -#fout = open('delete_me_1','wb') +#fout = open('delete_me_1','wsix.b(') #fout.write(the_old_way) #fout.close -#fout = open('delete_me_2', 'wb') +#fout = open(')delete_me_2', 'wsix.b(') #fout.write(the_new_way) #fout.close diff --git a/tests/test_filedescriptor.py b/tests/test_filedescriptor.py index 5ae90c7..ff00aff 100755 --- a/tests/test_filedescriptor.py +++ b/tests/test_filedescriptor.py @@ -23,6 +23,7 @@ from pexpect import fdpexpect import unittest import PexpectTestCase import os +from pexpect import six class ExpectTestCase(PexpectTestCase.PexpectTestCase): def setUp(self): @@ -32,9 +33,9 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase): def test_fd (self): fd = os.open ('TESTDATA.txt', os.O_RDONLY) s = fdpexpect.fdspawn (fd) - s.expect(b'This is the end of test data:') + s.expect(six.b('This is the end of test data:')) s.expect(pexpect.EOF) - self.assertEqual(s.before, b' END\n') + self.assertEqual(s.before, six.b(' END\n')) def test_maxread (self): fd = os.open ('TESTDATA.txt', os.O_RDONLY) @@ -43,7 +44,7 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase): s.expect('2') s.expect ('This is the end of test data:') s.expect (pexpect.EOF) - self.assertEqual(s.before, b' END\n') + self.assertEqual(s.before, six.b(' END\n')) def test_fd_isalive (self): fd = os.open ('TESTDATA.txt', os.O_RDONLY) diff --git a/tests/test_interact.py b/tests/test_interact.py index 891265f..6754f2f 100755 --- a/tests/test_interact.py +++ b/tests/test_interact.py @@ -24,6 +24,7 @@ import unittest import time, tty import PexpectTestCase import threading +from pexpect import six def start_interact (p): p.interact() @@ -67,12 +68,12 @@ class InteractTestCase (PexpectTestCase.PexpectTestCase): def test_interact (self): p = pexpect.spawn('%s interact.py' % self.PYTHONBIN) - 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.sendline (six.b('Hello')) + p.sendline (six.b('there')) + p.sendline (six.b('Mr. Python')) + p.expect (six.b('Hello')) + p.expect (six.b('there')) + p.expect (six.b('Mr. Python')) assert p.isalive() == True, p.isalive() p.sendeof () p.expect (pexpect.EOF) diff --git a/tests/test_isalive.py b/tests/test_isalive.py index d18996c..7f02b9e 100755 --- a/tests/test_isalive.py +++ b/tests/test_isalive.py @@ -43,7 +43,7 @@ class IsAliveTestCase(PexpectTestCase.PexpectTestCase): time.sleep(1) try: p.wait() - except pexpect.ExceptionPexpect as e: + except pexpect.ExceptionPexpect: pass else: self.fail ('Should have raised ExceptionPython because you can\'t call wait on a dead process.') diff --git a/tests/test_log.py b/tests/test_log.py index 669e253..519e774 100755 --- a/tests/test_log.py +++ b/tests/test_log.py @@ -24,9 +24,10 @@ import unittest import os import tempfile import PexpectTestCase +from pexpect import six # the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent -_CAT_EOF = b'^D\x08\x08' +_CAT_EOF = six.b('^D\x08\x08') class TestCaseLog(PexpectTestCase.PexpectTestCase): @@ -58,11 +59,11 @@ class TestCaseLog(PexpectTestCase.PexpectTestCase): with open(filename, 'rb') as f: lf = f.read() os.unlink (filename) - lf = lf.replace(_CAT_EOF, b'') - self.assertEqual(lf, b'This is a test.\r\nThis is a test.\r\n') + lf = lf.replace(_CAT_EOF, six.b('')) + self.assertEqual(lf, six.b('This is a test.\r\nThis is a test.\r\n')) def test_log_logfile_send (self): - log_message = b'This is a test.' + log_message = six.b('This is a test.') filename = tempfile.mktemp() mylog = open (filename, 'wb') p = pexpect.spawn('cat') @@ -75,7 +76,7 @@ class TestCaseLog(PexpectTestCase.PexpectTestCase): with open(filename, 'rb') as f: lf = f.read() os.unlink(filename) - lf = lf.replace(b'\x04', b'') + lf = lf.replace(six.b('\x04'), six.b('')) self.assertEqual(lf.rstrip(), log_message) def test_log_send_and_received (self): @@ -98,9 +99,9 @@ class TestCaseLog(PexpectTestCase.PexpectTestCase): with open(filename, 'rb') as f: lf = f.read() os.unlink(filename) - lf = lf.replace(b'\x04', b'').replace(_CAT_EOF, b'') + lf = lf.replace(six.b('\x04'), six.b('')).replace(_CAT_EOF, six.b('')) self.assertEqual(lf, - b'This is a test.\nThis is a test.\r\nThis is a test.\r\n') + six.b('This is a test.\nThis is a test.\r\nThis is a test.\r\n')) if __name__ == '__main__': unittest.main() diff --git a/tests/test_misc.py b/tests/test_misc.py index d322bdb..03c106a 100755 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -24,9 +24,10 @@ import unittest import PexpectTestCase import os import re +from pexpect import six # the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent -_CAT_EOF = b'^D\x08\x08' +_CAT_EOF = six.b('^D\x08\x08') class TestCaseMisc(PexpectTestCase.PexpectTestCase): @@ -38,13 +39,13 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase): 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') + self.assertEqual(child.read(0), six.b('')) + self.assertEqual(child.read(1), six.b('a')) + self.assertEqual(child.read(1), six.b('b')) + self.assertEqual(child.read(1), six.b('c')) + self.assertEqual(child.read(2), six.b('\r\n')) + remaining = child.read().replace(_CAT_EOF, six.b('')) + self.assertEqual(remaining, six.b('abc\r\n')) def test_readline (self): '''See the note in test_readlines() for an explaination as to why @@ -60,13 +61,13 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase): line3 = child.readline(2) line4 = child.readline(1) line5 = child.readline() - self.assertEqual(line1, b'') - self.assertEqual(line2, b'abc\r\n') - assert (line3 == b'abc\r\n' or line3 == '123\r\n'), \ + self.assertEqual(line1, six.b('')) + self.assertEqual(line2, six.b('abc\r\n')) + assert (line3 == six.b('abc\r\n') or line3 == '123\r\n'), \ "readline(2) did not return 'abc\\r\\n'. Returned: " + repr(line3) - assert (line4 == b'123\r\n' or line4 == 'abc\r\n'), \ + assert (line4 == six.b('123\r\n') or line4 == 'abc\r\n'), \ "readline(1) did not return '123\\r\\n'. Returned: " + repr(line4) - self.assertEqual(line5, b'123\r\n') + self.assertEqual(line5, six.b('123\r\n')) def test_iter (self): '''See the note in test_readlines() for an explaination as to why @@ -78,12 +79,12 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase): child.sendline ("123") child.sendeof() # Don't use ''.join() because we want to test the ITERATOR. - page = b"" + page = six.b('') for line in child: page += line - page = page.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') , \ + page = page.replace(_CAT_EOF, six.b('')) + assert (page == six.b('abc\r\nabc\r\n123\r\n123\r\n') or + page == six.b('abc\r\n123\r\nabc\r\n123\r\n')) , \ "iterator did not work. page=%s"%repr(page) def test_readlines(self): @@ -104,23 +105,23 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase): child.sendline ("abc") child.sendline ("123") 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'), \ + page = six.b('').join(child.readlines()).replace(_CAT_EOF, six.b('')) + assert (page == six.b('abc\r\nabc\r\n123\r\n123\r\n') or + page == six.b('abc\r\n123\r\nabc\r\n123\r\n')), \ "readlines() did not work. page=%s"%repr(page) def test_write (self): child = pexpect.spawn('cat') child.write('a') child.write('\r') - self.assertEqual(child.readline(), b'a\r\n') + self.assertEqual(child.readline(), six.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=%s"%repr(line) + assert line == six.b('abc123xyz\r\n'), "writelines() did not work. line=%s"%repr(line) def test_eof(self): child = pexpect.spawn('cat') @@ -192,13 +193,13 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase): 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'" + assert six.b('foo') in userenv and six.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" + assert (six.b('tmp') in tmpdir), "'tmp' should be returned by 'pwd' command" def test_which (self): p = os.defpath diff --git a/tests/test_performance.py b/tests/test_performance.py index d2bfe5f..519e168 100755 --- a/tests/test_performance.py +++ b/tests/test_performance.py @@ -18,18 +18,13 @@ PEXPECT LICENSE OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ''' -from __future__ import print_function, with_statement +from __future__ import with_statement import unittest, time, sys import pexpect import PexpectTestCase -if sys.version_info[0] >= 3: - def b(s): - return s.encode('ascii') -else: - def b(s): - return s +from pexpect import six # This isn't exactly a unit test, but it fits in nicely with the rest of the tests. @@ -40,58 +35,58 @@ class PerformanceTestCase (PexpectTestCase.PexpectTestCase): def plain_range(self, n): e = pexpect.spawn('python') - self.assertEqual(e.expect(b'>>>'), 0) - e.sendline(b('for n in range(1, %d+1): print(n)' % n)) - self.assertEqual(e.expect(br'\.{3}'), 0) - e.sendline(b'') - self.assertEqual(e.expect([b'inquisition', b('%d' % n)]), 1) + self.assertEqual(e.expect(six.b('>>>')), 0) + e.sendline(six.b('for n in range(1, %d+1): print(n)' % n)) + self.assertEqual(e.expect(six.b(r'\.{3}')), 0) + e.sendline(six.b('')) + self.assertEqual(e.expect([six.b('inquisition'), six.b('%d' % n)]), 1) def window_range(self, n): e = pexpect.spawn('python') - self.assertEqual(e.expect(b'>>>'), 0) - e.sendline(b('for n in range(1, %d+1): print(n)' % n)) + self.assertEqual(e.expect(six.b('>>>')), 0) + e.sendline(six.b('for n in range(1, %d+1): print(n)' % n)) self.assertEqual(e.expect(r'\.{3}'), 0) - e.sendline(b'') - self.assertEqual(e.expect([b'inquisition', b('%d' % n)], searchwindowsize=10), 1) + e.sendline(six.b('')) + self.assertEqual(e.expect([six.b('inquisition'), six.b('%d' % n)], searchwindowsize=10), 1) def exact_range(self, n): e = pexpect.spawn('python') - self.assertEqual(e.expect_exact([b'>>>']), 0) - e.sendline(b('for n in range(1, %d+1): print(n)' % n)) - self.assertEqual(e.expect_exact([b'...']), 0) - e.sendline(b'') - self.assertEqual(e.expect_exact([b'inquisition', b('%d' % n)],timeout=520), 1) + self.assertEqual(e.expect_exact([six.b('>>>')]), 0) + e.sendline(six.b('for n in range(1, %d+1): print(n)' % n)) + self.assertEqual(e.expect_exact([six.b('...')]), 0) + e.sendline(six.b('')) + self.assertEqual(e.expect_exact([six.b('inquisition'), six.b('%d' % n)],timeout=520), 1) def ewin_range(self, n): e = pexpect.spawn('python') - self.assertEqual(e.expect_exact([b'>>>']), 0) - e.sendline(b('for n in range(1, %d+1): print(n)' % n)) - self.assertEqual(e.expect_exact([b'...']), 0) - e.sendline(b'') - self.assertEqual(e.expect_exact([b'inquisition', b('%d' % n)], searchwindowsize=10), 1) + self.assertEqual(e.expect_exact([six.b('>>>')]), 0) + e.sendline(six.b('for n in range(1, %d+1): print(n)' % n)) + self.assertEqual(e.expect_exact([six.b('...')]), 0) + e.sendline(six.b('')) + self.assertEqual(e.expect_exact([six.b('inquisition'), six.b('%d' % n)], searchwindowsize=10), 1) def faster_range(self, n): e = pexpect.spawn('python') - self.assertEqual(e.expect(b'>>>'), 0) - e.sendline(b('range(1, %d+1)' % n)) - self.assertEqual(e.expect([b'inquisition', b('%d' % n)]), 1) + self.assertEqual(e.expect(six.b('>>>')), 0) + e.sendline(six.b('range(1, %d+1)' % n)) + self.assertEqual(e.expect([six.b('inquisition'), six.b('%d' % n)]), 1) def test_100000(self): start_time = time.time() self.plain_range (100000) - print("100000 calls to plain_range:", (time.time() - start_time)) + six.print_("100000 calls to plain_range:", (time.time() - start_time)) start_time = time.time() self.window_range(100000) - print("100000 calls to window_range:", (time.time() - start_time)) + six.print_("100000 calls to window_range:", (time.time() - start_time)) start_time = time.time() self.exact_range (100000) - print("100000 calls to exact_range:", (time.time() - start_time)) + six.print_("100000 calls to exact_range:", (time.time() - start_time)) start_time = time.time() self.ewin_range (100000) - print("100000 calls to ewin_range:", (time.time() - start_time)) + six.print_("100000 calls to ewin_range:", (time.time() - start_time)) start_time = time.time() self.faster_range(100000) - print("100000 calls to faster_range:", (time.time() - start_time)) + six.print_("100000 calls to faster_range:", (time.time() - start_time)) if __name__ == "__main__": unittest.main() diff --git a/tests/test_run.py b/tests/test_run.py index 3088ff2..50bae78 100755 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -22,6 +22,7 @@ import pexpect import unittest import subprocess import PexpectTestCase +from pexpect import six # TODO Many of these test cases blindly assume that sequential # TODO listing of the /bin directory will yield the same results. @@ -40,9 +41,10 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase): assert exitstatus == 1, "Exit status of 'python exit1.py' should be 1." def test_run (self): - the_old_way = subprocess.check_output(['ls', '-l', '/bin']).rstrip() + the_old_way = subprocess.subprocess.Popen(args=['ls', '-l', '/bin'] + ).communicate()[0].rstrip() (the_new_way, exitstatus) = pexpect.run ('ls -l /bin', withexitstatus=1) - the_new_way = the_new_way.replace(b'\r',b'').rstrip() + the_new_way = the_new_way.replace(six.b('\r'),six.b('')).rstrip() self.assertEqual(the_old_way, the_new_way) self.assertEqual(exitstatus, 0) diff --git a/tests/test_run_out_of_pty.py b/tests/test_run_out_of_pty.py index e817966..69529f1 100755 --- a/tests/test_run_out_of_pty.py +++ b/tests/test_run_out_of_pty.py @@ -35,12 +35,12 @@ class ExpectTestCase(PexpectTestCase.PexpectTestCase): for count in range (0,10000): try: plist.append (pexpect.spawn('ls -l')) - except pexpect.ExceptionPexpect as e: - for c in range (0,count): + except pexpect.ExceptionPexpect: + for c in range (0, count): plist[c].close() return - except Exception as e: - self.fail ('Expected ExceptionPexpect. ' + str(e)) + except Exception, err: + self.fail ('Expected ExceptionPexpect. ' + str(err)) self.fail ('Could not run out of pty devices. This may be OK.') if __name__ == '__main__': diff --git a/tests/test_timeout_pattern.py b/tests/test_timeout_pattern.py index dbeefcf..ef7ba72 100755 --- a/tests/test_timeout_pattern.py +++ b/tests/test_timeout_pattern.py @@ -43,7 +43,7 @@ class Exp_TimeoutTestCase(PexpectTestCase.PexpectTestCase): p.sendline('Hello') p.expect('Hello') p.expect('Goodbye',timeout=5) - except pexpect.TIMEOUT as expTimeoutInst: + except pexpect.TIMEOUT: assert p.match_index == None else: self.fail("Did not generate a TIMEOUT exception.") @@ -63,9 +63,10 @@ class Exp_TimeoutTestCase(PexpectTestCase.PexpectTestCase): p = pexpect.spawn('cat') p.sendline('Hello') p.expect('Goodbye',timeout=5) - except pexpect.TIMEOUT as e: - if e.get_trace().count("pexpect.py") != 0: - self.fail("The TIMEOUT get_trace() referenced pexpect.py. It should only reference the caller.\n"+e.get_trace()) + except pexpect.TIMEOUT, err: + if err.get_trace().count("pexpect.py") != 0: + self.fail("The TIMEOUT get_trace() referenced pexpect.py. " + "It should only reference the caller.\n" + err.get_trace()) def test_correctStackTrace (self): '''Verify that the stack trace returned with a TIMEOUT instance correctly handles function calls.''' @@ -76,9 +77,11 @@ class Exp_TimeoutTestCase(PexpectTestCase.PexpectTestCase): p = pexpect.spawn('cat') p.sendline('Hello') nestedFunction(p) - except pexpect.TIMEOUT as e: - if e.get_trace().count("nestedFunction") == 0: - self.fail("The TIMEOUT get_trace() did not show the call to the nestedFunction function.\n" + str(e) + "\n" + e.get_trace()) + except pexpect.TIMEOUT, err: + if err.get_trace().count("nestedFunction") == 0: + self.fail("The TIMEOUT get_trace() did not show the call " + "to the nestedFunction function.\n" + str(err) + "\n" + + err.get_trace()) if __name__ == '__main__': unittest.main() diff --git a/tests/test_unicode.py b/tests/test_unicode.py index c613424..8c740cc 100644 --- a/tests/test_unicode.py +++ b/tests/test_unicode.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import print_function, unicode_literals, with_statement +from __future__ import with_statement import platform import tempfile diff --git a/tests/test_winsize.py b/tests/test_winsize.py index 384e611..57b697d 100755 --- a/tests/test_winsize.py +++ b/tests/test_winsize.py @@ -23,6 +23,8 @@ import unittest import PexpectTestCase import time +from pexpect import six + class TestCaseWinsize(PexpectTestCase.PexpectTestCase): def test_winsize (self): @@ -34,20 +36,20 @@ class TestCaseWinsize(PexpectTestCase.PexpectTestCase): time.sleep(10) p1.setwinsize (11,22) time.sleep(3) - index = p1.expect ([pexpect.TIMEOUT, b'SIGWINCH: \(([0-9]*), ([0-9]*)\)'], timeout=30) + index = p1.expect ([pexpect.TIMEOUT, six.b('SIGWINCH: \(([0-9]*), ([0-9]*)\)')], timeout=30) if index == 0: self.fail ("TIMEOUT -- this platform may not support sigwinch properly.\n" + str(p1)) r = p1.match.group(1) c = p1.match.group(2) - assert (r==b"11" and c==b"22") + assert (r==six.b("11") and c==six.b("22")) time.sleep(1) p1.setwinsize (24,80) - index = p1.expect ([pexpect.TIMEOUT, b'SIGWINCH: \(([0-9]*), ([0-9]*)\)'], timeout=10) + index = p1.expect ([pexpect.TIMEOUT, six.b('SIGWINCH: \(([0-9]*), ([0-9]*)\)')], timeout=10) if index == 0: self.fail ("TIMEOUT -- this platform may not support sigwinch properly.\n" + str(p1)) r = p1.match.group(1) c = p1.match.group(2) - assert (r==b"24" and c==b"80") + assert (r==six.b("24") and c==six.b("80")) p1.close() # def test_parent_resize (self): |