summaryrefslogtreecommitdiff
path: root/pexpect/tests/test_log.py
blob: 844be1313a680d0baaa3be2ffbab3a74cffa7787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import pexpect
import unittest
import os
import tempfile 
import PexpectTestCase

class TestCaseLog(PexpectTestCase.PexpectTestCase):

    def test_log (self):
        log_message = 'This is a test.'
        filename = tempfile.mktemp()
        mylog = open (filename, 'w')
        p = pexpect.spawn('echo', [log_message])
        p.logfile = mylog
        p.expect (pexpect.EOF)
        p.logfile = None
        mylog.close()
        lf = open(filename).read()
        lf = lf[:-2]
        os.unlink (filename)
        assert lf == log_message

    def test_log_logfile_read (self):
        log_message = 'This is a test.'
        filename = tempfile.mktemp()
        mylog = open (filename, 'w')
        p = pexpect.spawn('cat')
        p.logfile_read = mylog
        p.sendline(log_message)
        p.sendeof()
        p.expect (pexpect.EOF)
        p.logfile = None
        mylog.close()
        lf = open(filename).read()
        lf = lf[:-2]
        os.unlink (filename)
        lf = lf.replace(chr(4),'')
        assert lf == 'This is a test.\r\nThis is a test.', "The read log file has bad data. Note logfile_read should only record what we read from child and nothing else.\n" + repr(lf)

    def test_log_logfile_send (self):
        log_message = 'This is a test.'
        filename = tempfile.mktemp()
        mylog = open (filename, 'w')
        p = pexpect.spawn('cat')
        p.logfile_send = mylog
        p.sendline(log_message)
        p.sendeof()
        p.expect (pexpect.EOF)
        p.logfile = None
        mylog.close()
        lf = open(filename).read()
        lf = lf[:-2]
        os.unlink (filename)
        assert lf == log_message, "The send log file has bad data. Note logfile_send should only record what we sent to child and nothing else."

    def test_log_send_and_received (self):

        """The logfile should have the test message three time -- once for the
        data we sent. Once for the data that cat echos back as characters are
        typed. And once for the data that cat prints after we send a linefeed
        (sent by sendline). """

        log_message = 'This is a test.'
        filename = tempfile.mktemp()
        mylog = open (filename, 'w')
        p = pexpect.spawn('cat')
        p.logfile = mylog
        p.sendline(log_message)
        p.sendeof()
        p.expect (pexpect.EOF)
        p.logfile = None
        mylog.close()
        lf = open(filename).read()
        os.unlink (filename)
        lf = lf.replace(chr(4),'')
        assert lf == 'This is a test.\nThis is a test.\r\nThis is a test.\r\n', repr(lf)

if __name__ == '__main__':
    unittest.main()

suite = unittest.makeSuite(TestCaseLog,'test')