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