xref: /llvm-project/lldb/test/API/terminal/TestSTTYBeforeAndAfter.py (revision 1b70580dd867195b0442e582eccd42abc41ee12d)
1"""
2Test that 'stty -a' displays the same output before and after running the lldb command.
3"""
4
5import lldb
6import io
7import sys
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class TestSTTYBeforeAndAfter(TestBase):
14    @classmethod
15    def classCleanup(cls):
16        """Cleanup the test byproducts."""
17        cls.RemoveTempFile("child_send1.txt")
18        cls.RemoveTempFile("child_read1.txt")
19        cls.RemoveTempFile("child_send2.txt")
20        cls.RemoveTempFile("child_read2.txt")
21
22    @skipIf(macos_version=["<", "14.0"], asan=True)
23    @add_test_categories(["pexpect"])
24    @no_debug_info_test
25    def test_stty_dash_a_before_and_afetr_invoking_lldb_command(self):
26        """Test that 'stty -a' displays the same output before and after running the lldb command."""
27        import pexpect
28
29        if not which("expect"):
30            self.skipTest("The 'expect' program cannot be located, skip the test")
31
32        # The expect prompt.
33        expect_prompt = "expect[0-9.]+> "
34        # The default lldb prompt.
35        lldb_prompt = "(lldb) "
36
37        # So that the child gets torn down after the test.
38        self.child = pexpect.spawn("expect", encoding="utf-8")
39        child = self.child
40
41        child.expect(expect_prompt)
42        child.setecho(True)
43        if self.TraceOn():
44            child.logfile = sys.stdout
45
46        if self.platformIsDarwin():
47            child.sendline("set env(TERM) xterm")
48        else:
49            child.sendline("set env(TERM) vt100")
50        child.expect(expect_prompt)
51        child.sendline("puts $env(TERM)")
52        child.expect(expect_prompt)
53
54        # Turn on loggings for input/output to/from the child.
55        child.logfile_send = child_send1 = io.StringIO()
56        child.logfile_read = child_read1 = io.StringIO()
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(lldbtest_config.lldbExec)
67        child.expect_exact(lldb_prompt)
68
69        # Immediately quit.
70        child.sendline("quit")
71        child.expect(expect_prompt)
72
73        child.logfile_send = child_send2 = io.StringIO()
74        child.logfile_read = child_read2 = io.StringIO()
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
84
85        if self.TraceOn():
86            print("\n\nContents of child_send1:")
87            print(child_send1.getvalue())
88            print("\n\nContents of child_read1:")
89            print(child_read1.getvalue())
90            print("\n\nContents of child_send2:")
91            print(child_send2.getvalue())
92            print("\n\nContents of child_read2:")
93            print(child_read2.getvalue())
94
95        stty_output1_lines = child_read1.getvalue().splitlines()
96        stty_output2_lines = child_read2.getvalue().splitlines()
97        zipped = list(zip(stty_output1_lines, stty_output2_lines))
98        for tuple in zipped:
99            if self.TraceOn():
100                print("tuple->%s" % str(tuple))
101            # Every line should compare equal until the first blank line.
102            if len(tuple[0]) == 0:
103                break
104            self.assertEqual(tuple[0], tuple[1])
105