OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 ''' |
| 3 PEXPECT 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 ''' |
| 21 import pexpect |
| 22 import unittest |
| 23 import os |
| 24 import tempfile |
| 25 from . import PexpectTestCase |
| 26 |
| 27 # the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent |
| 28 _CAT_EOF = b'^D\x08\x08' |
| 29 |
| 30 class TestCaseLog(PexpectTestCase.PexpectTestCase): |
| 31 |
| 32 def test_log (self): |
| 33 log_message = 'This is a test.' |
| 34 filename = tempfile.mktemp() |
| 35 mylog = open(filename, 'wb') |
| 36 p = pexpect.spawn('echo', [log_message]) |
| 37 p.logfile = mylog |
| 38 p.expect(pexpect.EOF) |
| 39 p.logfile = None |
| 40 mylog.close() |
| 41 with open(filename, 'rb') as f: |
| 42 lf = f.read() |
| 43 os.unlink(filename) |
| 44 self.assertEqual(lf.rstrip(), log_message.encode('ascii')) |
| 45 |
| 46 def test_log_logfile_read (self): |
| 47 log_message = 'This is a test.' |
| 48 filename = tempfile.mktemp() |
| 49 mylog = open(filename, 'wb') |
| 50 p = pexpect.spawn('cat') |
| 51 p.logfile_read = mylog |
| 52 p.sendline(log_message) |
| 53 p.sendeof() |
| 54 p.expect(pexpect.EOF) |
| 55 p.logfile = None |
| 56 mylog.close() |
| 57 with open(filename, 'rb') as f: |
| 58 lf = f.read() |
| 59 os.unlink (filename) |
| 60 lf = lf.replace(_CAT_EOF, b'') |
| 61 self.assertEqual(lf, b'This is a test.\r\nThis is a test.\r\n') |
| 62 |
| 63 def test_log_logfile_send (self): |
| 64 log_message = b'This is a test.' |
| 65 filename = tempfile.mktemp() |
| 66 mylog = open (filename, 'wb') |
| 67 p = pexpect.spawn('cat') |
| 68 p.logfile_send = mylog |
| 69 p.sendline(log_message) |
| 70 p.sendeof() |
| 71 p.expect (pexpect.EOF) |
| 72 p.logfile = None |
| 73 mylog.close() |
| 74 with open(filename, 'rb') as f: |
| 75 lf = f.read() |
| 76 os.unlink(filename) |
| 77 lf = lf.replace(b'\x04', b'') |
| 78 self.assertEqual(lf.rstrip(), log_message) |
| 79 |
| 80 def test_log_send_and_received (self): |
| 81 |
| 82 '''The logfile should have the test message three time -- once for the |
| 83 data we sent. Once for the data that cat echos back as characters are |
| 84 typed. And once for the data that cat prints after we send a linefeed |
| 85 (sent by sendline). ''' |
| 86 |
| 87 log_message = 'This is a test.' |
| 88 filename = tempfile.mktemp() |
| 89 mylog = open(filename, 'wb') |
| 90 p = pexpect.spawn('cat') |
| 91 p.logfile = mylog |
| 92 p.sendline(log_message) |
| 93 p.sendeof() |
| 94 p.expect (pexpect.EOF) |
| 95 p.logfile = None |
| 96 mylog.close() |
| 97 with open(filename, 'rb') as f: |
| 98 lf = f.read() |
| 99 os.unlink(filename) |
| 100 lf = lf.replace(b'\x04', b'').replace(_CAT_EOF, b'') |
| 101 self.assertEqual(lf, |
| 102 b'This is a test.\nThis is a test.\r\nThis is a test.\r\n') |
| 103 |
| 104 if __name__ == '__main__': |
| 105 unittest.main() |
| 106 |
| 107 suite = unittest.makeSuite(TestCaseLog,'test') |
| 108 |
OLD | NEW |