xref: /llvm-project/lldb/test/API/commands/command/script/import/TestImport.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""Test custom import command to import files by path."""
2
3
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class ImportTestCase(TestBase):
11    @add_test_categories(["pyapi"])
12    @no_debug_info_test
13    def test_import_command(self):
14        """Import some Python scripts by path and test them"""
15        self.run_test()
16
17    def run_test(self):
18        """Import some Python scripts by path and test them."""
19
20        # This is the function to remove the custom commands in order to have a
21        # clean slate for the next test case.
22        def cleanup():
23            self.runCmd("command script delete foo2cmd", check=False)
24            self.runCmd("command script delete foocmd", check=False)
25            self.runCmd("command script delete foobarcmd", check=False)
26            self.runCmd("command script delete barcmd", check=False)
27            self.runCmd("command script delete barothercmd", check=False)
28            self.runCmd("command script delete TPcommandA", check=False)
29            self.runCmd("command script delete TPcommandB", check=False)
30
31        # Execute the cleanup function during test case tear down.
32        self.addTearDownHook(cleanup)
33
34        self.runCmd("command script import ./foo/foo.py --allow-reload")
35        self.runCmd("command script import ./foo/foo2.py --allow-reload")
36        self.runCmd("command script import ./foo/bar/foobar.py --allow-reload")
37        self.runCmd("command script import ./bar/bar.py --allow-reload")
38
39        self.expect(
40            "command script import ''",
41            error=True,
42            startstr="error: module importing failed: empty path",
43        )
44        self.expect(
45            "command script import ./nosuchfile.py",
46            error=True,
47            startstr="error: module importing failed: invalid pathname './nosuchfile.py'",
48        )
49        self.expect(
50            "command script import ./nosuchfolder/",
51            error=True,
52            startstr="error: module importing failed: invalid pathname './nosuchfolder/'",
53        )
54        self.expect("command script import ./foo/foo.py", error=False)
55        self.runCmd("command script import --allow-reload ./thepackage")
56        self.expect("TPcommandA", substrs=["hello world A"])
57        self.expect("TPcommandB", substrs=["hello world B"])
58
59        self.runCmd("script import dummymodule")
60        self.expect("command script import ./dummymodule.py", error=False)
61        self.expect(
62            "command script import --allow-reload ./dummymodule.py", error=False
63        )
64
65        self.runCmd("command script add -f foo.foo_function foocmd")
66        self.runCmd("command script add -f foobar.foo_function foobarcmd")
67        self.runCmd("command script add -f bar.bar_function barcmd")
68        self.expect("foocmd hello", substrs=["foo says", "hello"])
69        self.expect("foo2cmd hello", substrs=["foo2 says", "hello"])
70        self.expect("barcmd hello", substrs=["barutil says", "bar told me", "hello"])
71        self.expect(
72            "barothercmd hello", substrs=["barutil says", "bar told me", "hello"]
73        )
74        self.expect("foobarcmd hello", substrs=["foobar says", "hello"])
75