xref: /llvm-project/lldb/test/API/python_api/interpreter/TestRunCommandInterpreterAPI.py (revision 1eeeab82c6eb185f5139e633a59c2dbcb15616e4)
1"""Test the RunCommandInterpreter API."""
2
3import os
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7
8
9class CommandRunInterpreterLegacyAPICase(TestBase):
10    NO_DEBUG_INFO_TESTCASE = True
11
12    def setUp(self):
13        TestBase.setUp(self)
14
15        self.stdin_path = self.getBuildArtifact("stdin.txt")
16
17        with open(self.stdin_path, "w") as input_handle:
18            input_handle.write("nonexistingcommand\nquit")
19
20        # Python will close the file descriptor if all references
21        # to the filehandle object lapse, so we need to keep one
22        # around.
23        self.filehandle = open(self.stdin_path, "r")
24        self.dbg.SetInputFileHandle(self.filehandle, False)
25
26        # No need to track the output
27        self.devnull = open(os.devnull, "w")
28        self.dbg.SetOutputFileHandle(self.devnull, False)
29        self.dbg.SetErrorFileHandle(self.devnull, False)
30
31    def test_run_session_with_error_and_quit_legacy(self):
32        """Run non-existing and quit command returns appropriate values"""
33
34        n_errors, quit_requested, has_crashed = self.dbg.RunCommandInterpreter(
35            True, False, lldb.SBCommandInterpreterRunOptions(), 0, False, False
36        )
37
38        self.assertGreater(n_errors, 0)
39        self.assertTrue(quit_requested)
40        self.assertFalse(has_crashed)
41
42
43class CommandRunInterpreterAPICase(TestBase):
44    NO_DEBUG_INFO_TESTCASE = True
45
46    def setUp(self):
47        TestBase.setUp(self)
48
49        self.stdin_path = self.getBuildArtifact("stdin.txt")
50
51        with open(self.stdin_path, "w") as input_handle:
52            input_handle.write("nonexistingcommand\nquit")
53
54        self.dbg.SetInputFile(open(self.stdin_path, "r"))
55
56        # No need to track the output
57        devnull = open(os.devnull, "w")
58        self.dbg.SetOutputFile(devnull)
59        self.dbg.SetErrorFile(devnull)
60
61    def test_run_session_with_error_and_quit(self):
62        """Run non-existing and quit command returns appropriate values"""
63
64        n_errors, quit_requested, has_crashed = self.dbg.RunCommandInterpreter(
65            True, False, lldb.SBCommandInterpreterRunOptions(), 0, False, False
66        )
67
68        self.assertGreater(n_errors, 0)
69        self.assertTrue(quit_requested)
70        self.assertFalse(has_crashed)
71
72
73class SBCommandInterpreterRunOptionsCase(TestBase):
74    NO_DEBUG_INFO_TESTCASE = True
75
76    def test_command_interpreter_run_options(self):
77        """Test SBCommandInterpreterRunOptions default values, getters & setters"""
78
79        opts = lldb.SBCommandInterpreterRunOptions()
80
81        # Check getters with default values
82        self.assertFalse(opts.GetStopOnContinue())
83        self.assertFalse(opts.GetStopOnError())
84        self.assertFalse(opts.GetStopOnCrash())
85        self.assertTrue(opts.GetEchoCommands())
86        self.assertTrue(opts.GetPrintResults())
87        self.assertTrue(opts.GetPrintErrors())
88        self.assertTrue(opts.GetAddToHistory())
89
90        # Invert values
91        opts.SetStopOnContinue(not opts.GetStopOnContinue())
92        opts.SetStopOnError(not opts.GetStopOnError())
93        opts.SetStopOnCrash(not opts.GetStopOnCrash())
94        opts.SetEchoCommands(not opts.GetEchoCommands())
95        opts.SetPrintResults(not opts.GetPrintResults())
96        opts.SetPrintErrors(not opts.GetPrintErrors())
97        opts.SetAddToHistory(not opts.GetAddToHistory())
98
99        # Check the value changed
100        self.assertTrue(opts.GetStopOnContinue())
101        self.assertTrue(opts.GetStopOnError())
102        self.assertTrue(opts.GetStopOnCrash())
103        self.assertFalse(opts.GetEchoCommands())
104        self.assertFalse(opts.GetPrintResults())
105        self.assertFalse(opts.GetPrintErrors())
106        self.assertFalse(opts.GetAddToHistory())
107