xref: /llvm-project/lldb/test/API/commands/command/script/import/TestImport.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""Test custom import command to import files by path."""
299451b44SJordan Rupprecht
399451b44SJordan Rupprecht
499451b44SJordan Rupprechtimport lldb
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass ImportTestCase(TestBase):
11*2238dcc3SJonas Devlieghere    @add_test_categories(["pyapi"])
1299451b44SJordan Rupprecht    @no_debug_info_test
1399451b44SJordan Rupprecht    def test_import_command(self):
1499451b44SJordan Rupprecht        """Import some Python scripts by path and test them"""
1599451b44SJordan Rupprecht        self.run_test()
1699451b44SJordan Rupprecht
1799451b44SJordan Rupprecht    def run_test(self):
1899451b44SJordan Rupprecht        """Import some Python scripts by path and test them."""
1999451b44SJordan Rupprecht
2099451b44SJordan Rupprecht        # This is the function to remove the custom commands in order to have a
2199451b44SJordan Rupprecht        # clean slate for the next test case.
2299451b44SJordan Rupprecht        def cleanup():
23*2238dcc3SJonas Devlieghere            self.runCmd("command script delete foo2cmd", check=False)
24*2238dcc3SJonas Devlieghere            self.runCmd("command script delete foocmd", check=False)
25*2238dcc3SJonas Devlieghere            self.runCmd("command script delete foobarcmd", check=False)
26*2238dcc3SJonas Devlieghere            self.runCmd("command script delete barcmd", check=False)
27*2238dcc3SJonas Devlieghere            self.runCmd("command script delete barothercmd", check=False)
28*2238dcc3SJonas Devlieghere            self.runCmd("command script delete TPcommandA", check=False)
29*2238dcc3SJonas Devlieghere            self.runCmd("command script delete TPcommandB", check=False)
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht        # Execute the cleanup function during test case tear down.
3299451b44SJordan Rupprecht        self.addTearDownHook(cleanup)
3399451b44SJordan Rupprecht
3499451b44SJordan Rupprecht        self.runCmd("command script import ./foo/foo.py --allow-reload")
3599451b44SJordan Rupprecht        self.runCmd("command script import ./foo/foo2.py --allow-reload")
3699451b44SJordan Rupprecht        self.runCmd("command script import ./foo/bar/foobar.py --allow-reload")
3799451b44SJordan Rupprecht        self.runCmd("command script import ./bar/bar.py --allow-reload")
3899451b44SJordan Rupprecht
39*2238dcc3SJonas Devlieghere        self.expect(
40*2238dcc3SJonas Devlieghere            "command script import ''",
41*2238dcc3SJonas Devlieghere            error=True,
42*2238dcc3SJonas Devlieghere            startstr="error: module importing failed: empty path",
43*2238dcc3SJonas Devlieghere        )
44*2238dcc3SJonas Devlieghere        self.expect(
45*2238dcc3SJonas Devlieghere            "command script import ./nosuchfile.py",
46*2238dcc3SJonas Devlieghere            error=True,
47*2238dcc3SJonas Devlieghere            startstr="error: module importing failed: invalid pathname './nosuchfile.py'",
48*2238dcc3SJonas Devlieghere        )
49*2238dcc3SJonas Devlieghere        self.expect(
50*2238dcc3SJonas Devlieghere            "command script import ./nosuchfolder/",
51*2238dcc3SJonas Devlieghere            error=True,
52*2238dcc3SJonas Devlieghere            startstr="error: module importing failed: invalid pathname './nosuchfolder/'",
53*2238dcc3SJonas Devlieghere        )
5499451b44SJordan Rupprecht        self.expect("command script import ./foo/foo.py", error=False)
5599451b44SJordan Rupprecht        self.runCmd("command script import --allow-reload ./thepackage")
5699451b44SJordan Rupprecht        self.expect("TPcommandA", substrs=["hello world A"])
5799451b44SJordan Rupprecht        self.expect("TPcommandB", substrs=["hello world B"])
5899451b44SJordan Rupprecht
5999451b44SJordan Rupprecht        self.runCmd("script import dummymodule")
6099451b44SJordan Rupprecht        self.expect("command script import ./dummymodule.py", error=False)
6199451b44SJordan Rupprecht        self.expect(
62*2238dcc3SJonas Devlieghere            "command script import --allow-reload ./dummymodule.py", error=False
63*2238dcc3SJonas Devlieghere        )
6499451b44SJordan Rupprecht
6599451b44SJordan Rupprecht        self.runCmd("command script add -f foo.foo_function foocmd")
6699451b44SJordan Rupprecht        self.runCmd("command script add -f foobar.foo_function foobarcmd")
6799451b44SJordan Rupprecht        self.runCmd("command script add -f bar.bar_function barcmd")
68*2238dcc3SJonas Devlieghere        self.expect("foocmd hello", substrs=["foo says", "hello"])
69*2238dcc3SJonas Devlieghere        self.expect("foo2cmd hello", substrs=["foo2 says", "hello"])
70*2238dcc3SJonas Devlieghere        self.expect("barcmd hello", substrs=["barutil says", "bar told me", "hello"])
71*2238dcc3SJonas Devlieghere        self.expect(
72*2238dcc3SJonas Devlieghere            "barothercmd hello", substrs=["barutil says", "bar told me", "hello"]
73*2238dcc3SJonas Devlieghere        )
74*2238dcc3SJonas Devlieghere        self.expect("foobarcmd hello", substrs=["foobar says", "hello"])
75