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