Skip to content

Commit 3eaad96

Browse files
committed
twister: Improve error handling for user's errors
Exit from twiser in case of user's error e.g. empty list of tests provided by user instead of throwing unhandled exception. Signed-off-by: Lukasz Fundakowski <[email protected]>
1 parent 1a49b41 commit 3eaad96

File tree

3 files changed

+29
-41
lines changed

3 files changed

+29
-41
lines changed

scripts/pylib/twister/twisterlib/testplan.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,10 @@ def discover(self):
217217
testsuite_pattern=self.options.test_pattern)
218218

219219
if num == 0:
220-
raise TwisterRuntimeError("No testsuites found at the specified location...")
220+
print("No testsuites found at the specified location...", file=sys.stderr)
221+
raise SystemExit(1)
221222
if self.load_errors:
222-
raise TwisterRuntimeError(
223+
raise SystemExit(
223224
f"Found {self.load_errors} errors loading {num} test configurations."
224225
)
225226

@@ -236,7 +237,7 @@ def discover(self):
236237
qv = self.options.quarantine_verify
237238
if qv and not ql:
238239
logger.error("No quarantine list given to be verified")
239-
raise TwisterRuntimeError("No quarantine list given to be verified")
240+
raise SystemExit("No quarantine list given to be verified")
240241
if ql:
241242
for quarantine_file in ql:
242243
try:

scripts/tests/twister/test_testplan.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ def test_testplan_find_subtests(
629629

630630

631631
TESTDATA_3 = [
632-
(0, 0, [], False, [], TwisterRuntimeError, []),
633-
(1, 1, [], False, [], TwisterRuntimeError, []),
634-
(1, 0, [], True, [], TwisterRuntimeError, ['No quarantine list given to be verified']),
632+
(0, 0, [], False, [], SystemExit, []),
633+
(1, 1, [], False, [], SystemExit, []),
634+
(1, 0, [], True, [], SystemExit, ['No quarantine list given to be verified']),
635635
(1, 0, ['qfile.yaml'], False, ['- platforms:\n - demo_board_3\n comment: "board_3"'], None, []),
636636
]
637637

@@ -702,10 +702,10 @@ def test_testplan_discover(
702702
]
703703

704704
@pytest.mark.parametrize(
705-
'report_suffix, only_failed, load_tests, test_only, subset,' \
706-
' exception, expected_selected_platforms, expected_generate_subset_args',
705+
'report_suffix, only_failed, load_tests, test_only, subset, ' \
706+
'exception, expected_selected_platforms, expected_generate_subset_args',
707707
TESTDATA_4,
708-
ids=['apply_filters only', 'only failed', 'load tests', 'test only']
708+
ids=['apply-filters-only', 'only-failed', 'load-tests', 'test-only']
709709
)
710710
def test_testplan_load(
711711
tmp_path,

scripts/tests/twister_blackbox/test_error.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
Blackbox tests for twister's command line functions - simple does-error-out or not tests
77
"""
88

9-
import importlib
10-
from unittest import mock
119
import os
1210
import pytest
13-
import sys
1411
import re
12+
import sys
13+
from unittest import mock
1514

1615
# pylint: disable=no-name-in-module
17-
from conftest import ZEPHYR_BASE, TEST_DATA, suite_filename_mock
16+
from conftest import TEST_DATA, suite_filename_mock
1817
from twisterlib.testplan import TestPlan
19-
from twisterlib.error import TwisterRuntimeError
18+
from twisterlib.twister_main import main as twister_main
2019

2120

2221
class TestError:
@@ -26,17 +25,17 @@ class TestError:
2625
os.path.join('scripts', 'tests', 'twister_blackbox', 'test_data', 'tests',
2726
'dummy', 'agnostic', 'group1', 'subgroup1',
2827
'dummy.agnostic.group1.subgroup1'),
29-
SystemExit
28+
(0, '')
3029
),
3130
(
3231
None,
3332
'dummy.agnostic.group1.subgroup1',
34-
TwisterRuntimeError
33+
(1, 'No testsuites found at the specified location...')
3534
),
3635
(
3736
os.path.join(TEST_DATA, 'tests', 'dummy'),
3837
'dummy.agnostic.group1.subgroup1',
39-
SystemExit
38+
(0, '')
4039
)
4140
]
4241
TESTDATA_2 = [
@@ -50,24 +49,13 @@ class TestError:
5049
)
5150
]
5251

53-
@classmethod
54-
def setup_class(cls):
55-
apath = os.path.join(ZEPHYR_BASE, 'scripts', 'twister')
56-
cls.loader = importlib.machinery.SourceFileLoader('__main__', apath)
57-
cls.spec = importlib.util.spec_from_loader(cls.loader.name, cls.loader)
58-
cls.twister_module = importlib.util.module_from_spec(cls.spec)
59-
60-
@classmethod
61-
def teardown_class(cls):
62-
pass
63-
6452
@pytest.mark.parametrize(
65-
'testroot, test, expected_exception',
53+
'testroot, test, expected_return',
6654
TESTDATA_1,
6755
ids=['valid', 'invalid', 'valid']
6856
)
6957
@mock.patch.object(TestPlan, 'TESTSUITE_FILENAME', suite_filename_mock)
70-
def test_test(self, out_path, testroot, test, expected_exception):
58+
def test_test(self, out_path, testroot, test, expected_return, capsys):
7159
test_platforms = ['qemu_x86', 'intel_adl_crb']
7260
args = []
7361
if testroot:
@@ -77,13 +65,14 @@ def test_test(self, out_path, testroot, test, expected_exception):
7765
['-p'] * len(test_platforms), test_platforms
7866
) for val in pair]
7967

80-
with mock.patch.object(sys, 'argv', [sys.argv[0]] + args), \
81-
pytest.raises(expected_exception) as exc:
82-
self.loader.exec_module(self.twister_module)
68+
expected_return_code, expected_message = expected_return
69+
70+
return_code = twister_main(args)
71+
captured = capsys.readouterr()
8372

84-
if expected_exception == SystemExit:
85-
assert str(exc.value) == '0'
86-
assert True
73+
assert return_code == expected_return_code
74+
if expected_message:
75+
assert expected_message in captured.err
8776

8877
@pytest.mark.parametrize(
8978
'switch, expected',
@@ -106,18 +95,16 @@ def test_overflow_as_errors(self, capfd, out_path, switch, expected):
10695
if switch:
10796
args += [switch]
10897

109-
with mock.patch.object(sys, 'argv', [sys.argv[0]] + args), \
110-
pytest.raises(SystemExit) as sys_exit:
111-
self.loader.exec_module(self.twister_module)
98+
return_code = twister_main(args)
11299

113100
out, err = capfd.readouterr()
114101
sys.stdout.write(out)
115102
sys.stderr.write(err)
116103

117104
print(args)
118105
if switch:
119-
assert str(sys_exit.value) == '1'
106+
assert return_code == 1
120107
else:
121-
assert str(sys_exit.value) == '0'
108+
assert return_code == 0
122109

123110
assert re.search(expected, err)

0 commit comments

Comments
 (0)