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

Commit b0b8b97

Browse files
committed
Move return codes into main module
Signed-off-by: Andrea Cervesato <[email protected]>
1 parent 2a99de2 commit b0b8b97

File tree

3 files changed

+46
-53
lines changed

3 files changed

+46
-53
lines changed

ltp/main.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88
import os
99
import re
10-
import sys
1110
import inspect
1211
import argparse
1312
import importlib
@@ -19,11 +18,17 @@
1918
from ltp.session import Session
2019
from ltp.ui import SimpleUserInterface
2120
from ltp.ui import VerboseUserInterface
21+
from ltp.dispatcher import SuiteTimeoutError
2222

2323

2424
# runtime loaded SUT(s)
2525
LOADED_SUT = []
2626

27+
RC_OK = 0
28+
RC_ERROR = 1
29+
RC_TIMEOUT = 124
30+
RC_INTERRUPT = 130
31+
2732

2833
def _from_params_to_config(params: list) -> dict:
2934
"""
@@ -211,17 +216,30 @@ def _start_session(parser: ArgumentParser, args: Namespace) -> None:
211216
else:
212217
SimpleUserInterface(args.no_colors)
213218

214-
exit_code = session.run_single(
215-
command=args.run_cmd,
216-
suites=args.run_suite,
217-
report_path=args.json_report)
218-
219-
ltp.events.stop_event_loop()
220-
221-
sys.exit(exit_code)
222-
223-
224-
def run() -> None:
219+
exit_code = RC_OK
220+
221+
try:
222+
session.run_single(
223+
command=args.run_cmd,
224+
suites=args.run_suite,
225+
report_path=args.json_report)
226+
except KeyboardInterrupt:
227+
session.stop(timeout=60)
228+
exit_code = RC_INTERRUPT
229+
ltp.events.fire("session_stopped")
230+
except SuiteTimeoutError:
231+
exit_code = RC_TIMEOUT
232+
except ltp.LTPException as err:
233+
session.stop(timeout=60)
234+
exit_code = RC_ERROR
235+
ltp.events.fire("session_error", str(err))
236+
finally:
237+
ltp.events.stop_event_loop()
238+
239+
parser.exit(exit_code)
240+
241+
242+
def run(cmd_args: list = None) -> None:
225243
"""
226244
Entry point of the application.
227245
"""
@@ -298,11 +316,11 @@ def run() -> None:
298316
type=str,
299317
help="JSON output report")
300318

301-
args = parser.parse_args()
319+
args = parser.parse_args(cmd_args)
302320

303321
if args.sut and "help" in args.sut:
304322
print(args.sut["help"])
305-
return
323+
parser.exit(RC_OK)
306324

307325
if args.json_report and os.path.exists(args.json_report):
308326
parser.error(f"JSON report file already exists: {args.json_report}")

ltp/session.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from ltp.sut import IOBuffer
1616
from ltp.tempfile import TempDir
1717
from ltp.dispatcher import SerialDispatcher
18-
from ltp.dispatcher import SuiteTimeoutError
1918
from ltp.export import JSONExporter
2019
from ltp.utils import Timeout
2120

@@ -50,11 +49,6 @@ class Session:
5049
The main session handler.
5150
"""
5251

53-
RC_OK = 0
54-
RC_ERROR = 1
55-
RC_TIMEOUT = 124
56-
RC_INTERRUPT = 130
57-
5852
def __init__(self, **kwargs) -> None:
5953
"""
6054
:param tmpdir: temporary directory path
@@ -212,7 +206,7 @@ def run_single(
212206
self,
213207
command: str = None,
214208
suites: list = None,
215-
report_path: str = None) -> int:
209+
report_path: str = None) -> None:
216210
"""
217211
Run some testing suites with a specific SUT configurations.
218212
:param command: command to execute
@@ -221,10 +215,7 @@ def run_single(
221215
:type suites: list
222216
:param report_path: path of the report file. If None, it won't be saved
223217
:type report_path: None | str
224-
:returns: exit code for the session
225218
"""
226-
exit_code = self.RC_OK
227-
228219
with self._lock_run:
229220
ltp.events.fire("session_started", self._tmpdir.abspath)
230221

@@ -252,21 +243,12 @@ def run_single(
252243
suites, skip_tests=self._skip_tests)
253244

254245
self._dispatcher.stop()
255-
except SuiteTimeoutError:
256-
exit_code = self.RC_TIMEOUT
257246
except LTPException as err:
258-
self._stop_all(timeout=60)
259-
260247
self._logger.exception(err)
261-
ltp.events.fire("session_error", str(err))
262-
263-
exit_code = self.RC_ERROR
264-
except KeyboardInterrupt:
248+
raise err
249+
except KeyboardInterrupt as err:
265250
self._logger.info("Keyboard interrupt")
266-
self._stop_all(timeout=60)
267-
ltp.events.fire("session_stopped")
268-
269-
exit_code = self.RC_INTERRUPT
251+
raise err
270252
finally:
271253
results = self._dispatcher.last_results
272254
if results:
@@ -288,5 +270,3 @@ def run_single(
288270
self._stop_sut(timeout=60)
289271
ltp.events.fire("session_completed", results)
290272
self._logger.info("Session completed")
291-
292-
return exit_code

ltp/tests/test_session.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99
import ltp
1010
from ltp.session import Session
11-
from ltp.tempfile import TempDir
11+
from ltp.dispatcher import SuiteTimeoutError
1212
from ltp.host import HostSUT
1313

1414

@@ -186,9 +186,8 @@ def test_run_cmd(self, sut, tmpdir, sut_config, ltpdir):
186186
ltpdir=ltpdir,
187187
tmpdir=str(tmpdir))
188188

189-
retcode = session.run_single(command="ls -l")
189+
session.run_single(command="ls -l")
190190

191-
assert retcode == Session.RC_OK
192191
assert tracer.next_event() == "session_started"
193192
assert tracer.next_event() == "sut_start"
194193
assert tracer.next_event() == "run_cmd_start"
@@ -228,12 +227,11 @@ def test_run_single(
228227
ltpdir=ltpdir,
229228
tmpdir=str(tmpdir))
230229

231-
retcode = session.run_single(
230+
session.run_single(
232231
report_path=report_path,
233232
suites=suites,
234233
command=command)
235234

236-
assert retcode == Session.RC_OK
237235
assert tracer.next_event() == "session_started"
238236
assert tracer.next_event() == "sut_start"
239237

@@ -274,7 +272,7 @@ def test_skip_tests(
274272
tmpdir=str(tmpdir),
275273
skip_tests="dir0[12]|dir0(1|3)|dir05")
276274

277-
retcode = session.run_single(
275+
session.run_single(
278276
report_path=report_path,
279277
suites=[
280278
"dirsuite0",
@@ -288,7 +286,6 @@ def test_skip_tests(
288286
with open(report_path, 'r') as report_f:
289287
report_d = json.loads(report_f.read())
290288

291-
assert retcode == Session.RC_OK
292289
tests = [item['test_fqn'] for item in report_d["results"]]
293290
assert "dir01" not in tests
294291
assert "dir02" not in tests
@@ -321,9 +318,8 @@ def stop_exec_suites(test):
321318
sut_config["name"],
322319
None)
323320

324-
retcode = session.run_single(report_path=report_path, suites=suites)
321+
session.run_single(report_path=report_path, suites=suites)
325322

326-
assert retcode == Session.RC_OK
327323
assert os.path.exists(report_path)
328324
assert tracer.next_event() == "session_started"
329325
assert tracer.next_event() == "sut_start"
@@ -349,11 +345,11 @@ def test_suite_timeout_report(self, sut, tmpdir, sut_config, ltpdir):
349345
sut_config["name"],
350346
None)
351347

352-
retcode = session.run_single(
353-
suites=["sleep"],
354-
report_path=report_path)
348+
with pytest.raises(SuiteTimeoutError):
349+
session.run_single(
350+
suites=["sleep"],
351+
report_path=report_path)
355352

356-
assert retcode == Session.RC_TIMEOUT
357353
assert os.path.exists(report_path)
358354
assert tracer.next_event() == "session_started"
359355
assert tracer.next_event() == "sut_start"
@@ -385,11 +381,10 @@ def test_env(self, sut, tmpdir, sut_config, ltpdir):
385381
tmpdir=str(tmpdir),
386382
env=dict(VAR0="0", VAR1="1"))
387383

388-
retcode = session.run_single(
384+
session.run_single(
389385
report_path=report_path,
390386
suites=["suite"])
391387

392-
assert retcode == Session.RC_OK
393388
assert os.path.isfile(report_path)
394389

395390
report_d = None

0 commit comments

Comments
 (0)