1"""
2Test that 'stty -a' displays the same output before and after running the lldb command.
3"""
4
5from __future__ import print_function
6
7
8import lldb
9import six
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class TestSTTYBeforeAndAfter(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    @classmethod
20    def classCleanup(cls):
21        """Cleanup the test byproducts."""
22        cls.RemoveTempFile("child_send1.txt")
23        cls.RemoveTempFile("child_read1.txt")
24        cls.RemoveTempFile("child_send2.txt")
25        cls.RemoveTempFile("child_read2.txt")
26
27    @expectedFailureAll(
28        hostoslist=["windows"],
29        bugnumber="llvm.org/pr22274: need a pexpect replacement for windows")
30    @no_debug_info_test
31    def test_stty_dash_a_before_and_afetr_invoking_lldb_command(self):
32        """Test that 'stty -a' displays the same output before and after running the lldb command."""
33        import pexpect
34        if not which('expect'):
35            self.skipTest(
36                "The 'expect' program cannot be located, skip the test")
37
38        # The expect prompt.
39        expect_prompt = "expect[0-9.]+> "
40        # The default lldb prompt.
41        lldb_prompt = "(lldb) "
42
43        # So that the child gets torn down after the test.
44        import sys
45        if sys.version_info.major == 3:
46          self.child = pexpect.spawnu('expect')
47        else:
48          self.child = pexpect.spawn('expect')
49        child = self.child
50
51        child.expect(expect_prompt)
52        child.setecho(True)
53        if self.TraceOn():
54            child.logfile = sys.stdout
55
56        if self.platformIsDarwin():
57            child.sendline('set env(TERM) xterm')
58        else:
59            child.sendline('set env(TERM) vt100')
60        child.expect(expect_prompt)
61        child.sendline('puts $env(TERM)')
62        child.expect(expect_prompt)
63
64        # Turn on loggings for input/output to/from the child.
65        child.logfile_send = child_send1 = six.StringIO()
66        child.logfile_read = child_read1 = six.StringIO()
67        child.sendline('stty -a')
68        child.expect(expect_prompt)
69
70        # Now that the stage1 logging is done, restore logfile to None to
71        # stop further logging.
72        child.logfile_send = None
73        child.logfile_read = None
74
75        # Invoke the lldb command.
76        child.sendline(lldbtest_config.lldbExec)
77        child.expect_exact(lldb_prompt)
78
79        # Immediately quit.
80        child.sendline('quit')
81        child.expect(expect_prompt)
82
83        child.logfile_send = child_send2 = six.StringIO()
84        child.logfile_read = child_read2 = six.StringIO()
85        child.sendline('stty -a')
86        child.expect(expect_prompt)
87
88        child.sendline('exit')
89
90        # Now that the stage2 logging is done, restore logfile to None to
91        # stop further logging.
92        child.logfile_send = None
93        child.logfile_read = None
94
95        if self.TraceOn():
96            print("\n\nContents of child_send1:")
97            print(child_send1.getvalue())
98            print("\n\nContents of child_read1:")
99            print(child_read1.getvalue())
100            print("\n\nContents of child_send2:")
101            print(child_send2.getvalue())
102            print("\n\nContents of child_read2:")
103            print(child_read2.getvalue())
104
105        stty_output1_lines = child_read1.getvalue().splitlines()
106        stty_output2_lines = child_read2.getvalue().splitlines()
107        zipped = list(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.assertEqual(tuple[0], tuple[1])
115