1"""
2Test that 'stty -a' displays the same output before and after running the lldb command.
3"""
4
5import os
6import unittest2
7import lldb
8import pexpect
9from lldbtest import *
10
11class CommandLineCompletionTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "completion")
14
15    @classmethod
16    def classCleanup(cls):
17        """Cleanup the test byproducts."""
18        cls.RemoveTempFile("child_send1.txt")
19        cls.RemoveTempFile("child_read1.txt")
20        cls.RemoveTempFile("child_send2.txt")
21        cls.RemoveTempFile("child_read2.txt")
22
23    def test_stty_dash_a_before_and_afetr_invoking_lldb_command(self):
24        """Test that 'stty -a' displays the same output before and after running the lldb command."""
25
26        if not which('expect'):
27            self.skipTest("The 'expect' program cannot be located, skip the test")
28
29        # The expect prompt.
30        expect_prompt = "expect[0-9.]+> "
31        # The default lldb prompt.
32        lldb_prompt = "(lldb) "
33
34        # So that the child gets torn down after the test.
35        self.child = pexpect.spawn('expect')
36        child = self.child
37
38        child.expect(expect_prompt)
39        child.setecho(True)
40        if self.TraceOn():
41            child.logfile = sys.stdout
42
43        if sys.platform.startswith("darwin"):
44            child.sendline('set env(TERM) xterm')
45        else:
46            child.sendline('set env(TERM) vt100')
47        child.expect(expect_prompt)
48        child.sendline('puts $env(TERM)')
49        child.expect(expect_prompt)
50
51        # Turn on loggings for input/output to/from the child.
52        with open('child_send1.txt', 'w') as f_send1:
53            with open('child_read1.txt', 'w') as f_read1:
54                child.logfile_send = f_send1
55                child.logfile_read = f_read1
56
57                child.sendline('stty -a')
58                child.expect(expect_prompt)
59
60        # Now that the stage1 logging is done, restore logfile to None to
61        # stop further logging.
62        child.logfile_send = None
63        child.logfile_read = None
64
65        # Invoke the lldb command.
66        child.sendline('%s %s' % (self.lldbHere, self.lldbOption))
67        child.expect_exact(lldb_prompt)
68
69        # Immediately quit.
70        child.sendline('quit')
71        child.expect(expect_prompt)
72
73        with open('child_send2.txt', 'w') as f_send2:
74            with open('child_read2.txt', 'w') as f_read2:
75                child.logfile_send = f_send2
76                child.logfile_read = f_read2
77
78                child.sendline('stty -a')
79                child.expect(expect_prompt)
80
81                child.sendline('exit')
82
83        # Now that the stage2 logging is done, restore logfile to None to
84        # stop further logging.
85        child.logfile_send = None
86        child.logfile_read = None
87
88        with open('child_send1.txt', 'r') as fs:
89            if self.TraceOn():
90                print "\n\nContents of child_send1.txt:"
91                print fs.read()
92        with open('child_read1.txt', 'r') as fr:
93            from_child1 = fr.read()
94            if self.TraceOn():
95                print "\n\nContents of child_read1.txt:"
96                print from_child1
97
98        with open('child_send2.txt', 'r') as fs:
99            if self.TraceOn():
100                print "\n\nContents of child_send2.txt:"
101                print fs.read()
102        with open('child_read2.txt', 'r') as fr:
103            from_child2 = fr.read()
104            if self.TraceOn():
105                print "\n\nContents of child_read2.txt:"
106                print from_child2
107
108        stty_output1_lines = from_child1.splitlines()
109        stty_output2_lines = from_child2.splitlines()
110        zipped = zip(stty_output1_lines, stty_output2_lines)
111        for tuple in zipped:
112            if self.TraceOn():
113                print "tuple->%s" % str(tuple)
114            # Every line should compare equal until the first blank line.
115            if len(tuple[0]) == 0:
116                break
117            self.assertTrue(tuple[0] == tuple[1])
118
119
120if __name__ == '__main__':
121    import atexit
122    lldb.SBDebugger.Initialize()
123    atexit.register(lambda: lldb.SBDebugger.Terminate())
124    unittest2.main()
125