xref: /llvm-project/lldb/test/API/python_api/interpreter/TestRunCommandInterpreterAPI.py (revision 05d7d6949c7cd3f1566d4c8394fa59160a7ffd05)
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