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