1"""
2Test completion in our IOHandlers.
3"""
4
5import os
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test.lldbpexpect import PExpectTest
11
12class IOHandlerCompletionTest(PExpectTest):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    # PExpect uses many timeouts internally and doesn't play well
17    # under ASAN on a loaded machine..
18    @skipIfAsan
19    @skipIfEditlineSupportMissing
20    @expectedFailureAll(oslist=['freebsd'], bugnumber='llvm.org/pr49408')
21    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])
22    def test_completion(self):
23        self.build()
24        self.launch(dimensions=(100,500), executable=self.getBuildArtifact("a.out"))
25
26        # Start tab completion, go to the next page and then display all with 'a'.
27        self.child.send("\t\ta")
28        self.child.expect_exact("register")
29
30        # Try tab completing regi to register.
31        self.child.send("regi\t")
32        # editline might move the cursor back to the start of the line and
33        # then back to its original position.
34        self.child.expect(re.compile(b"regi(\r" + self.cursor_forward_escape_seq(len(self.PROMPT + "regi")) + b")?ster"))
35        self.child.send("\n")
36        self.expect_prompt()
37
38        # Try tab completing directories and files. Also tests the partial
39        # completion where LLDB shouldn't print a space after the directory
40        # completion (as it didn't completed the full token).
41        dir_without_slashes = os.path.realpath(os.path.dirname(__file__)).rstrip("/")
42        self.child.send("file " + dir_without_slashes + "\t")
43        self.child.expect_exact("iohandler/completion/")
44        # If we get a correct partial completion without a trailing space, then this
45        # should complete the current test file.
46        self.child.send("TestIOHandler\t")
47        # As above, editline might move the cursor to the start of the line and
48        # then back to its original position. We only care about the fact
49        # that this is completing a partial completion, so skip the exact cursor
50        # position calculation.
51        self.child.expect(re.compile(b"TestIOHandler(\r" + self.cursor_forward_escape_seq("\d+") + b")?Completion.py"))
52        self.child.send("\n")
53        self.expect_prompt()
54
55        # Complete a file path.
56        # FIXME: This should complete to './main.c' and not 'main.c'
57        self.child.send("breakpoint set --file ./main\t")
58        self.child.expect_exact("main.c")
59        self.child.send("\n")
60        self.expect_prompt()
61
62        # Start tab completion and abort showing more commands with 'n'.
63        self.child.send("\t")
64        self.child.expect_exact("More (Y/n/a)")
65        self.child.send("n")
66        self.expect_prompt()
67
68        # Shouldn't crash or anything like that.
69        self.child.send("regoinvalid\t")
70        self.expect_prompt()
71
72        self.quit()
73