xref: /llvm-project/lldb/test/API/commands/command/script/import/TestImport.py (revision e83d47f6b7bd2f8dcf419e03b3a00d5db5bd61c4)
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 ''",
42                    error=True, startstr="error: module importing failed: empty path")
43        self.expect("command script import ./nosuchfile.py",
44                    error=True, startstr="error: module importing failed: invalid pathname './nosuchfile.py'")
45        self.expect("command script import ./nosuchfolder/",
46                    error=True, startstr="error: module importing failed: invalid pathname './nosuchfolder/'")
47        self.expect("command script import ./foo/foo.py", error=False)
48        self.runCmd("command script import --allow-reload ./thepackage")
49        self.expect("TPcommandA", substrs=["hello world A"])
50        self.expect("TPcommandB", substrs=["hello world B"])
51
52        self.runCmd("script import dummymodule")
53        self.expect("command script import ./dummymodule.py", error=False)
54        self.expect(
55            "command script import --allow-reload ./dummymodule.py",
56            error=False)
57
58        self.runCmd("command script add -f foo.foo_function foocmd")
59        self.runCmd("command script add -f foobar.foo_function foobarcmd")
60        self.runCmd("command script add -f bar.bar_function barcmd")
61        self.expect("foocmd hello",
62                    substrs=['foo says', 'hello'])
63        self.expect("foo2cmd hello",
64                    substrs=['foo2 says', 'hello'])
65        self.expect("barcmd hello",
66                    substrs=['barutil says', 'bar told me', 'hello'])
67        self.expect("barothercmd hello",
68                    substrs=['barutil says', 'bar told me', 'hello'])
69        self.expect("foobarcmd hello",
70                    substrs=['foobar says', 'hello'])
71