xref: /llvm-project/lldb/test/API/commands/command/source/TestCommandSource.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest that lldb command "command source" works correctly.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass CommandSourceTestCase(TestBase):
1399451b44SJordan Rupprecht    @no_debug_info_test
1499451b44SJordan Rupprecht    def test_command_source(self):
1599451b44SJordan Rupprecht        """Test that lldb command "command source" works correctly."""
1699451b44SJordan Rupprecht
1799451b44SJordan Rupprecht        # Sourcing .lldb in the current working directory, which in turn imports
1899451b44SJordan Rupprecht        # the "my" package that defines the date() function.
1999451b44SJordan Rupprecht        self.runCmd("command source .lldb")
203bf3b966SJim Ingham        self.check_results()
2199451b44SJordan Rupprecht
223bf3b966SJim Ingham    @no_debug_info_test
233bf3b966SJim Ingham    def test_command_source_relative(self):
243bf3b966SJim Ingham        """Test that lldb command "command source" works correctly with relative paths."""
253bf3b966SJim Ingham
263bf3b966SJim Ingham        # Sourcing .lldb in the current working directory, which in turn imports
273bf3b966SJim Ingham        # the "my" package that defines the date() function.
283bf3b966SJim Ingham        self.runCmd("command source commands2.txt")
293bf3b966SJim Ingham        self.check_results()
303bf3b966SJim Ingham
313bf3b966SJim Ingham    def check_results(self, failure=False):
3299451b44SJordan Rupprecht        # Python should evaluate "my.date()" successfully.
3399451b44SJordan Rupprecht        command_interpreter = self.dbg.GetCommandInterpreter()
3499451b44SJordan Rupprecht        self.assertTrue(command_interpreter, VALID_COMMAND_INTERPRETER)
3599451b44SJordan Rupprecht        result = lldb.SBCommandReturnObject()
3699451b44SJordan Rupprecht        command_interpreter.HandleCommand("script my.date()", result)
3799451b44SJordan Rupprecht
3899451b44SJordan Rupprecht        import datetime
39*2238dcc3SJonas Devlieghere
403bf3b966SJim Ingham        if failure:
41*2238dcc3SJonas Devlieghere            self.expect(
42*2238dcc3SJonas Devlieghere                result.GetOutput(),
43*2238dcc3SJonas Devlieghere                "script my.date() runs successfully",
4499451b44SJordan Rupprecht                exe=False,
45*2238dcc3SJonas Devlieghere                error=True,
46*2238dcc3SJonas Devlieghere            )
47*2238dcc3SJonas Devlieghere        else:
48*2238dcc3SJonas Devlieghere            self.expect(
49*2238dcc3SJonas Devlieghere                result.GetOutput(),
50*2238dcc3SJonas Devlieghere                "script my.date() runs successfully",
51*2238dcc3SJonas Devlieghere                exe=False,
52*2238dcc3SJonas Devlieghere                substrs=[str(datetime.date.today())],
53*2238dcc3SJonas Devlieghere            )
543bf3b966SJim Ingham
553bf3b966SJim Ingham    @no_debug_info_test
563bf3b966SJim Ingham    def test_command_source_relative_error(self):
573bf3b966SJim Ingham        """Test that 'command source -C' gives an error for a relative path"""
583bf3b966SJim Ingham        source_dir = self.getSourceDir()
593bf3b966SJim Ingham        result = lldb.SBCommandReturnObject()
603bf3b966SJim Ingham        self.runCmd("command source --stop-on-error 1 not-relative.txt")
613bf3b966SJim Ingham        self.check_results(failure=True)
62