Commit 17eb3e81 authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

tools: parser: Minor change and tests fixes.

Showing with 77 additions and 27 deletions
+77 -27
...@@ -92,8 +92,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -92,8 +92,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_unix_simple(self): def test_unix_simple(self):
cmd = "/foo/bar a -b -c" cmd = "/foo/bar a -b -c"
...@@ -101,8 +102,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -101,8 +102,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_unix_quoted(self): def test_unix_quoted(self):
cmd = "\"/foo/bar\" -a -b -c" cmd = "\"/foo/bar\" -a -b -c"
...@@ -110,8 +112,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -110,8 +112,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_unix_quoted_with_space(self): def test_unix_quoted_with_space(self):
cmd = "\"/foo/bar baz\" -a -b -c" cmd = "\"/foo/bar baz\" -a -b -c"
...@@ -119,8 +122,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -119,8 +122,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_unix_quoted_args(self): def test_unix_quoted_args(self):
cmd = "/foo/bar -a -b -c=\"baz\"" cmd = "/foo/bar -a -b -c=\"baz\""
...@@ -128,8 +132,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -128,8 +132,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_unix_quoted_args_with_space(self): def test_unix_quoted_args_with_space(self):
cmd = "/foo/bar -a -b -c=\"baz bazz\"" cmd = "/foo/bar -a -b -c=\"baz bazz\""
...@@ -137,8 +142,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -137,8 +142,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_unix_quoted2_args_with_space(self): def test_unix_quoted2_args_with_space(self):
cmd = "\'/foo/bar baz\' -a -b -c='baz bazz'" cmd = "\'/foo/bar baz\' -a -b -c='baz bazz'"
...@@ -146,8 +152,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -146,8 +152,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_unix_escape_space(self): def test_unix_escape_space(self):
cmd = r"/foo/bar\ baz -a -b -c" cmd = r"/foo/bar\ baz -a -b -c"
...@@ -155,8 +162,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -155,8 +162,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_windows_prog_files(self): def test_windows_prog_files(self):
cmd = "\"C:\\Program Files (x86)\foo\bar\" a -b -c" cmd = "\"C:\\Program Files (x86)\foo\bar\" a -b -c"
...@@ -164,8 +172,9 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -164,8 +172,9 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
def test_windows_prog_files_args(self): def test_windows_prog_files_args(self):
cmd = "\"C:\\Program Files (x86)\foo\bar\" a -b=\"baz bazz\" -c" cmd = "\"C:\\Program Files (x86)\foo\bar\" a -b=\"baz bazz\" -c"
...@@ -176,5 +185,41 @@ class ToolsCMDParserTestCase(unittest.TestCase): ...@@ -176,5 +185,41 @@ class ToolsCMDParserTestCase(unittest.TestCase):
res = parse_command_line(cmd) res = parse_command_line(cmd)
for i, s in enumerate(expect): self.assertEqual(len(res), len(expect))
self.assertEqual(res[i], s) for i, s in enumerate(res):
self.assertEqual(expect[i], s)
# Parse must detect malformed command line... But is not :\
@unittest.expectedFailure
def test_wrong_format_0(self):
cmd = "'toto -a -b='baz bazz' -c"
expect = []
res = parse_command_line(cmd)
self.assertEqual(len(res), len(expect))
for i, s in enumerate(res):
self.assertEqual(expect[i], s)
@unittest.expectedFailure
def test_wrong_format_1(self):
cmd = "\"toto -a -b=\"baz bazz\" -c"
expect = []
res = parse_command_line(cmd)
self.assertEqual(len(res), len(expect))
for i, s in enumerate(res):
self.assertEqual(expect[i], s)
@unittest.expectedFailure
def test_wrong_format_2(self):
cmd = "'toto -a -b=\"baz bazz\" -c"
expect = []
res = parse_command_line(cmd)
self.assertEqual(len(res), len(expect))
for i, s in enumerate(res):
self.assertEqual(expect[i], s)
...@@ -367,12 +367,17 @@ def parse_command_line(cmd): ...@@ -367,12 +367,17 @@ def parse_command_line(cmd):
words = [] words = []
rest = cmd rest = cmd
while True: try:
if len(rest) == 0: while True:
break if len(rest) == 0:
break
word, rest = _parse_next_word(rest) word, rest = _parse_next_word(rest)
words.append(word) words.append(word)
except Exception as e:
logger.error(f"{parse_command_line}: Failed to parse command line '{cmd}'")
logger.error(f" exception raise {e}")
return []
return words return words
...@@ -442,9 +447,9 @@ def _parse_word_up_to_next_sep(words, sep=" "): ...@@ -442,9 +447,9 @@ def _parse_word_up_to_next_sep(words, sep=" "):
i += len(sub_word) + 1 i += len(sub_word) + 1
word += sub_word + cur word += sub_word + cur
# Get next symbol # Get next token
i += 1 i += 1
if i == len(words): if i >= len(words):
cur = "" cur = ""
else: else:
cur = words[i] cur = words[i]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment