Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit e842511

Browse files
committed
Cleanup session module
Session implementation was a bit messy causing code to be hard to read. With this patch we will fix this issue and cleanup code a bit. Signed-off-by: Andrea Cervesato <[email protected]>
1 parent 75c97e8 commit e842511

File tree

3 files changed

+235
-273
lines changed

3 files changed

+235
-273
lines changed

ltp/main.py

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from argparse import Namespace
1717
import ltp
1818
from ltp.sut import SUT
19-
from ltp.tempfile import TempDir
2019
from ltp.session import Session
2120
from ltp.ui import SimpleUserInterface
2221
from ltp.ui import VerboseUserInterface
@@ -136,33 +135,27 @@ def _discover_sut(folder: str) -> list:
136135
LOADED_SUT.sort(key=lambda x: x.name)
137136

138137

139-
def _ltp_run(parser: ArgumentParser, args: Namespace) -> None:
138+
def _get_sut(sut_name: str) -> SUT:
140139
"""
141-
Handle runltp-ng command options.
140+
Return the SUT with name `sut_name`.
142141
"""
143-
if args.sut and "help" in args.sut:
144-
print(args.sut["help"])
145-
return
146-
147-
if args.json_report and os.path.exists(args.json_report):
148-
parser.error(f"JSON report file already exists: {args.json_report}")
142+
sut = None
143+
for mysut in LOADED_SUT:
144+
if mysut.name == sut_name:
145+
sut = mysut
146+
break
149147

150-
if not args.run_suite and not args.run_cmd:
151-
parser.error("--run-suite/--run-cmd are required")
148+
return sut
152149

153-
if args.skip_file and not os.path.isfile(args.skip_file):
154-
parser.error(f"'{args.skip_file}' skip file doesn't exist")
155-
156-
if args.tmp_dir and not os.path.isdir(args.tmp_dir):
157-
parser.error(f"'{args.tmp_dir}' temporary folder doesn't exist")
158150

159-
# create regex of tests to skip
160-
skip_tests = args.skip_tests
161-
162-
if args.skip_file:
151+
def _get_skip_tests(skip_tests: str, skip_file: str) -> str:
152+
"""
153+
Return the skipped tests regexp.
154+
"""
155+
if skip_file:
163156
lines = None
164-
with open(args.skip_file, 'r', encoding="utf-8") as skip_file:
165-
lines = skip_file.readlines()
157+
with open(skip_file, 'r', encoding="utf-8") as skip_file_data:
158+
lines = skip_file_data.readlines()
166159

167160
toskip = [
168161
line.rstrip()
@@ -171,36 +164,49 @@ def _ltp_run(parser: ArgumentParser, args: Namespace) -> None:
171164
]
172165
skip_tests = '|'.join(toskip) + '|' + skip_tests
173166

167+
return skip_tests
168+
169+
170+
def _start_session(parser: ArgumentParser, args: Namespace) -> None:
171+
"""
172+
Handle runltp-ng command options.
173+
"""
174+
# create regex of tests to skip
175+
skip_tests = _get_skip_tests(args.skip_tests, args.skip_file)
174176
if skip_tests:
175177
try:
176178
re.compile(skip_tests)
177179
except re.error:
178180
parser.error(f"'{skip_tests}' is not a valid regular expression")
179181

180-
ltp.events.start_event_loop()
182+
# get the current SUT communication object
183+
sut_name = args.sut["name"]
184+
sut = _get_sut(sut_name)
185+
if not sut:
186+
parser.error(f"'{sut_name}' is not an available SUT")
181187

182-
if args.verbose:
183-
VerboseUserInterface(args.no_colors)
184-
else:
185-
SimpleUserInterface(args.no_colors)
188+
ltp.events.start_event_loop()
186189

187190
session = Session(
188-
LOADED_SUT,
191+
sut=sut,
192+
sut_config=args.sut,
193+
tmpdir=args.tmp_dir,
194+
ltpdir=args.ltp_dir,
189195
suite_timeout=args.suite_timeout,
190196
exec_timeout=args.exec_timeout,
191-
no_colors=args.no_colors)
197+
no_colors=args.no_colors,
198+
skip_tests=skip_tests,
199+
env=args.env)
192200

193-
tmpdir = TempDir(args.tmp_dir)
201+
if args.verbose:
202+
VerboseUserInterface(args.no_colors)
203+
else:
204+
SimpleUserInterface(args.no_colors)
194205

195206
exit_code = session.run_single(
196-
args.sut,
197-
args.json_report,
198-
args.run_suite,
199-
args.run_cmd,
200-
args.ltp_dir,
201-
tmpdir,
202-
skip_tests=skip_tests,
203-
env=args.env)
207+
command=args.run_cmd,
208+
suites=args.run_suite,
209+
report_path=args.json_report)
204210

205211
ltp.events.stop_event_loop()
206212

@@ -286,7 +292,23 @@ def run() -> None:
286292

287293
args = parser.parse_args()
288294

289-
_ltp_run(parser, args)
295+
if args.sut and "help" in args.sut:
296+
print(args.sut["help"])
297+
return
298+
299+
if args.json_report and os.path.exists(args.json_report):
300+
parser.error(f"JSON report file already exists: {args.json_report}")
301+
302+
if not args.run_suite and not args.run_cmd:
303+
parser.error("--run-suite/--run-cmd are required")
304+
305+
if args.skip_file and not os.path.isfile(args.skip_file):
306+
parser.error(f"'{args.skip_file}' skip file doesn't exist")
307+
308+
if args.tmp_dir and not os.path.isdir(args.tmp_dir):
309+
parser.error(f"'{args.tmp_dir}' temporary folder doesn't exist")
310+
311+
_start_session(parser, args)
290312

291313

292314
if __name__ == "__main__":

0 commit comments

Comments
 (0)