xref: /llvm-project/lldb/test/API/commands/settings/quoting/TestQuoting.py (revision 7ce3dd49eb80816e3af52022ba2521b28a068c7b)
1"""
2Test quoting of arguments to lldb commands.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class SettingsCommandTestCase(TestBase):
12    output_file_name = "output.txt"
13
14    @classmethod
15    def classCleanup(cls):
16        """Cleanup the test byproducts."""
17        cls.RemoveTempFile(SettingsCommandTestCase.output_file_name)
18
19    @no_debug_info_test
20    def test(self):
21        self.build()
22        exe = self.getBuildArtifact("a.out")
23        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
24
25        # No quotes.
26        self.expect_args("a b c", "a\0b\0c\0")
27        # Single quotes.
28        self.expect_args("'a b c'", "a b c\0")
29        # Double quotes.
30        self.expect_args('"a b c"', "a b c\0")
31        # Single quote escape.
32        self.expect_args("'a b\\' c", "a b\\\0c\0")
33        # Double quote escape.
34        self.expect_args('"a b\\" c"', 'a b" c\0')
35        self.expect_args('"a b\\\\" c', "a b\\\0c\0")
36        # Single quote in double quotes.
37        self.expect_args('"a\'b"', "a'b\0")
38        # Double quotes in single quote.
39        self.expect_args("'a\"b'", 'a"b\0')
40        # Combined quotes.
41        self.expect_args("\"a b\"c'd e'", "a bcd e\0")
42        # Bare single/double quotes.
43        self.expect_args("a\\'b", "a'b\0")
44        self.expect_args('a\\"b', 'a"b\0')
45
46    def expect_args(self, args_in, args_out):
47        """Test argument parsing. Run the program with args_in. The program dumps its arguments
48        to stdout. Compare the stdout with args_out."""
49
50        filename = SettingsCommandTestCase.output_file_name
51        outfile = self.getBuildArtifact(filename)
52
53        if lldb.remote_platform:
54            outfile_arg = lldbutil.append_to_process_working_directory(self, filename)
55        else:
56            outfile_arg = outfile
57
58        self.runCmd("process launch -- %s %s" % (outfile_arg, args_in))
59
60        if lldb.remote_platform:
61            src_file_spec = lldb.SBFileSpec(outfile_arg, False)
62            dst_file_spec = lldb.SBFileSpec(outfile, True)
63            lldb.remote_platform.Get(src_file_spec, dst_file_spec)
64
65        with open(outfile, "r") as f:
66            output = f.read()
67
68        self.RemoveTempFile(outfile)
69        self.assertEqual(output, args_out)
70