Skip to content

Commit f1da15e

Browse files
committed
ltp: use regex to parse test arguments in runtest file
Replace lexer with regex when we read test arguments from the runtest files, so we manage shell variables, as well as quoted/double-quoted strings in a better way. Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com> Fixes: #61
1 parent e61baec commit f1da15e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

libkirk/ltp.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import os
99
import re
1010
import json
11-
import shlex
1211
import logging
1312
from libkirk.results import TestResults
1413
from libkirk.results import ResultStatus
@@ -41,6 +40,7 @@ def __init__(self) -> None:
4140
self._env = None
4241
self._max_runtime = None
4342
self._tc_folder = None
43+
self._cmd_matcher = re.compile(r'(?:"[^"]*"|\'[^\']*\'|\S+)')
4444

4545
@property
4646
def config_help(self) -> dict:
@@ -126,6 +126,20 @@ def _is_addable(self, test_params: dict) -> bool:
126126

127127
return addable
128128

129+
def _get_cmd_args(self, line: str) -> list:
130+
"""
131+
Return a command with arguments inside a list(str).
132+
The command can have the following syntax:
133+
1. cmd
134+
2. cmd -t myarg1 ..
135+
3. cmd myfolder=$TMPDIR/folder -t myarg1 ..
136+
4. cmd -c "cmd2 -g arg1 -t arg2" ..
137+
"""
138+
matches = self._cmd_matcher.findall(line)
139+
parts = [match for match in matches if match]
140+
141+
return parts
142+
129143
# pylint: disable=too-many-locals
130144
async def _read_runtest(
131145
self,
@@ -149,14 +163,12 @@ async def _read_runtest(
149163
lines = content.split('\n')
150164

151165
for line in lines:
152-
if not line.strip() or line.strip().startswith("#"):
166+
parts = self._get_cmd_args(line)
167+
if not parts:
153168
continue
154169

155170
self._logger.debug("Test declaration: %s", line)
156171

157-
lexer = shlex.shlex(line, posix=False, punctuation_chars=True)
158-
159-
parts = list(lexer)
160172
if len(parts) < 2:
161173
raise FrameworkError(
162174
"runtest file is not defining test command")
@@ -251,7 +263,7 @@ async def find_command(self, sut: SUT, command: str) -> Test:
251263
if not command:
252264
raise ValueError("command is empty")
253265

254-
cmd_args = shlex.split(command)
266+
cmd_args = self._get_cmd_args(command)
255267
cwd = None
256268
env = None
257269

0 commit comments

Comments
 (0)