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