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