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