|
| 1 | +import fcntl |
| 2 | +import os |
| 3 | +import pty |
| 4 | +import struct |
| 5 | +import sys |
| 6 | +import termios |
| 7 | +import textwrap |
| 8 | +import unittest |
| 9 | + |
| 10 | +try: |
| 11 | + from twisted.internet import reactor |
| 12 | + from twisted.internet.defer import Deferred |
| 13 | + from twisted.internet.protocol import ProcessProtocol |
| 14 | + from twisted.trial.unittest import TestCase as TrialTestCase |
| 15 | +except ImportError: |
| 16 | + reactor = None |
| 17 | + |
| 18 | +TEST_CONFIG = os.path.join(os.path.dirname(__file__), "test.config") |
| 19 | + |
| 20 | +def set_win_size(fd, rows, columns): |
| 21 | + s = struct.pack('HHHH', rows, columns, 0, 0) |
| 22 | + fcntl.ioctl(fd, termios.TIOCSWINSZ, s) |
| 23 | + |
| 24 | +class CrashersTest(object): |
| 25 | + backend = "cli" |
| 26 | + |
| 27 | + def run_bpython(self, input): |
| 28 | + """ |
| 29 | + Run bpython (with `backend` as backend) in a subprocess and |
| 30 | + enter the given input. Uses a test config that disables the |
| 31 | + paste detection. |
| 32 | +
|
| 33 | + Retuns bpython's output. |
| 34 | + """ |
| 35 | + result = Deferred() |
| 36 | + |
| 37 | + class Protocol(ProcessProtocol): |
| 38 | + STATES = (SEND_INPUT, COLLECT) = xrange(2) |
| 39 | + |
| 40 | + def __init__(self): |
| 41 | + self.data = "" |
| 42 | + self.delayed_call = None |
| 43 | + self.states = iter(self.STATES) |
| 44 | + self.state = self.states.next() |
| 45 | + |
| 46 | + def outReceived(self, data): |
| 47 | + self.data += data |
| 48 | + if self.delayed_call is not None: |
| 49 | + self.delayed_call.cancel() |
| 50 | + self.delayed_call = reactor.callLater(0.5, self.next) |
| 51 | + |
| 52 | + def next(self): |
| 53 | + self.delayed_call = None |
| 54 | + if self.state == self.SEND_INPUT: |
| 55 | + index = self.data.find(">>> ") |
| 56 | + if index >= 0: |
| 57 | + self.data = self.data[index + 4:] |
| 58 | + self.transport.write(input) |
| 59 | + self.state = self.states.next() |
| 60 | + else: |
| 61 | + self.transport.closeStdin() |
| 62 | + if self.transport.pid is not None: |
| 63 | + self.delayed_call = None |
| 64 | + self.transport.signalProcess("TERM") |
| 65 | + |
| 66 | + def processExited(self, reason): |
| 67 | + if self.delayed_call is not None: |
| 68 | + self.delayed_call.cancel() |
| 69 | + result.callback(self.data) |
| 70 | + |
| 71 | + (master, slave) = pty.openpty() |
| 72 | + set_win_size(slave, 25, 80) |
| 73 | + reactor.spawnProcess(Protocol(), sys.executable, |
| 74 | + (sys.executable, "-m", "bpython." + self.backend, |
| 75 | + "-c", TEST_CONFIG), |
| 76 | + env=dict(TERM="vt100"), |
| 77 | + usePTY=(master, slave, os.ttyname(slave))) |
| 78 | + return result |
| 79 | + |
| 80 | + def test_issue108(self): |
| 81 | + input = textwrap.dedent( |
| 82 | + """\ |
| 83 | + def spam(): |
| 84 | + u"y\\xe4y" |
| 85 | + \b |
| 86 | + spam(""") |
| 87 | + deferred = self.run_bpython(input) |
| 88 | + return deferred.addCallback(self.check_no_traceback) |
| 89 | + |
| 90 | + def test_issue133(self): |
| 91 | + input = textwrap.dedent( |
| 92 | + """\ |
| 93 | + def spam(a, (b, c)): |
| 94 | + pass |
| 95 | + \b |
| 96 | + spam(1""") |
| 97 | + return self.run_bpython(input).addCallback(self.check_no_traceback) |
| 98 | + |
| 99 | + def check_no_traceback(self, data): |
| 100 | + tb = data[data.find("Traceback"):] |
| 101 | + self.assertTrue("Traceback" not in data, tb) |
| 102 | + |
| 103 | +if reactor is not None: |
| 104 | + class CursesCrashersTest(TrialTestCase, CrashersTest): |
| 105 | + backend = "cli" |
| 106 | + |
| 107 | + class UrwidCrashersTest(TrialTestCase, CrashersTest): |
| 108 | + backend = "urwid" |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + unittest.main() |
0 commit comments