blob: a79dec81221ffdb978b1e3debda57bedde6d2429 [file] [log] [blame]
Johnny Chen8f82f822012-02-09 00:27:01 +00001"""
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 system(["/bin/sh", "-c", "rm -f child_send1.txt"])
19 system(["/bin/sh", "-c", "rm -f child_read1.txt"])
20 system(["/bin/sh", "-c", "rm -f child_send2.txt"])
21 system(["/bin/sh", "-c", "rm -f 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
Johnny Chen7be5d352012-02-09 02:01:59 +000026 if not which('expect'):
27 self.skipTest("The 'expect' program cannot be located, skip the test")
28
Johnny Chen8f82f822012-02-09 00:27:01 +000029 # The expect prompt.
Johnny Chenb82faf72012-02-09 00:51:41 +000030 expect_prompt = "expect[0-9.]+> "
Johnny Chen8f82f822012-02-09 00:27:01 +000031 # 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
Johnny Chenf9fa91b2012-02-09 01:16:04 +000043 child.sendline('set env(TERM) xterm')
44 child.expect(expect_prompt)
45 child.sendline('puts $env(TERM)')
46 child.expect(expect_prompt)
47
Johnny Chen8f82f822012-02-09 00:27:01 +000048 # Turn on loggings for input/output to/from the child.
49 with open('child_send1.txt', 'w') as f_send1:
50 with open('child_read1.txt', 'w') as f_read1:
51 child.logfile_send = f_send1
52 child.logfile_read = f_read1
53
54 child.sendline('stty -a')
55 child.expect(expect_prompt)
56
57 # Now that the stage1 logging is done, restore logfile to None to
58 # stop further logging.
59 child.logfile_send = None
60 child.logfile_read = None
61
62 # Invoke the lldb command.
63 child.sendline('%s %s' % (self.lldbHere, self.lldbOption))
64 child.expect_exact(lldb_prompt)
65
66 # Immediately quit.
67 child.sendline('quit')
68 child.expect(expect_prompt)
69
70 with open('child_send2.txt', 'w') as f_send2:
71 with open('child_read2.txt', 'w') as f_read2:
72 child.logfile_send = f_send2
73 child.logfile_read = f_read2
74
75 child.sendline('stty -a')
76 child.expect(expect_prompt)
77
78 child.sendline('exit')
79
80 # Now that the stage2 logging is done, restore logfile to None to
81 # stop further logging.
82 child.logfile_send = None
83 child.logfile_read = None
Johnny Chenf9fa91b2012-02-09 01:16:04 +000084
Johnny Chen8f82f822012-02-09 00:27:01 +000085 with open('child_send1.txt', 'r') as fs:
86 if self.TraceOn():
87 print "\n\nContents of child_send1.txt:"
88 print fs.read()
89 with open('child_read1.txt', 'r') as fr:
90 from_child1 = fr.read()
91 if self.TraceOn():
92 print "\n\nContents of child_read1.txt:"
93 print from_child1
94
95 with open('child_send2.txt', 'r') as fs:
96 if self.TraceOn():
97 print "\n\nContents of child_send2.txt:"
98 print fs.read()
99 with open('child_read2.txt', 'r') as fr:
100 from_child2 = fr.read()
101 if self.TraceOn():
102 print "\n\nContents of child_read2.txt:"
103 print from_child2
104
105 stty_output1_lines = from_child1.splitlines()
106 stty_output2_lines = from_child2.splitlines()
107 zipped = zip(stty_output1_lines, stty_output2_lines)
108 for tuple in zipped:
109 if self.TraceOn():
110 print "tuple->%s" % str(tuple)
111 # Every line should compare equal until the first blank line.
112 if len(tuple[0]) == 0:
113 break
114 self.assertTrue(tuple[0] == tuple[1])
115
116
117if __name__ == '__main__':
118 import atexit
119 lldb.SBDebugger.Initialize()
120 atexit.register(lambda: lldb.SBDebugger.Terminate())
121 unittest2.main()