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