1#!/usr/bin/env python 2''' 3PEXPECT LICENSE 4 5 This license is approved by the OSI and FSF as GPL-compatible. 6 http://opensource.org/licenses/isc-license.txt 7 8 Copyright (c) 2012, Noah Spurrier <noah@noah.org> 9 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY 10 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE 11 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. 12 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 20''' 21from __future__ import with_statement # bring 'with' stmt to py25 22import pexpect 23import unittest 24from . import PexpectTestCase 25import sys 26 27class Exp_TimeoutTestCase(PexpectTestCase.PexpectTestCase): 28 def test_matches_exp_timeout (self): 29 '''This tests that we can raise and catch TIMEOUT. 30 ''' 31 try: 32 raise pexpect.TIMEOUT("TIMEOUT match test") 33 except pexpect.TIMEOUT: 34 pass 35 #print "Correctly caught TIMEOUT when raising TIMEOUT." 36 else: 37 self.fail('TIMEOUT not caught by an except TIMEOUT clause.') 38 39 def test_pattern_printout (self): 40 '''Verify that a TIMEOUT returns the proper patterns it is trying to match against. 41 Make sure it is returning the pattern from the correct call.''' 42 try: 43 p = pexpect.spawn('cat') 44 p.sendline('Hello') 45 p.expect('Hello') 46 p.expect('Goodbye',timeout=5) 47 except pexpect.TIMEOUT: 48 assert p.match_index == None 49 else: 50 self.fail("Did not generate a TIMEOUT exception.") 51 52 def test_exp_timeout_notThrown (self): 53 '''Verify that a TIMEOUT is not thrown when we match what we expect.''' 54 try: 55 p = pexpect.spawn('cat') 56 p.sendline('Hello') 57 p.expect('Hello') 58 except pexpect.TIMEOUT: 59 self.fail("TIMEOUT caught when it shouldn't be raised because we match the proper pattern.") 60 61 def test_stacktraceMunging (self): 62 '''Verify that the stack trace returned with a TIMEOUT instance does not contain references to pexpect.''' 63 try: 64 p = pexpect.spawn('cat') 65 p.sendline('Hello') 66 p.expect('Goodbye',timeout=5) 67 except pexpect.TIMEOUT: 68 err = sys.exc_info()[1] 69 if err.get_trace().count("pexpect/__init__.py") != 0: 70 self.fail("The TIMEOUT get_trace() referenced pexpect.py. " 71 "It should only reference the caller.\n" + err.get_trace()) 72 73 def test_correctStackTrace (self): 74 '''Verify that the stack trace returned with a TIMEOUT instance correctly handles function calls.''' 75 def nestedFunction (spawnInstance): 76 spawnInstance.expect("junk", timeout=3) 77 78 try: 79 p = pexpect.spawn('cat') 80 p.sendline('Hello') 81 nestedFunction(p) 82 except pexpect.TIMEOUT: 83 err = sys.exc_info()[1] 84 if err.get_trace().count("nestedFunction") == 0: 85 self.fail("The TIMEOUT get_trace() did not show the call " 86 "to the nestedFunction function.\n" + str(err) + "\n" 87 + err.get_trace()) 88 89if __name__ == '__main__': 90 unittest.main() 91 92suite = unittest.makeSuite(Exp_TimeoutTestCase,'test') 93