Skip to content

Commit a0d1b77

Browse files
LukaszMrugalaaescolar
authored andcommitted
scripts: twister: Fix overbroad Mock in environment unit tests
os.path.abspath was mocked too broadly, leading to errors for some users, while being undetected in the CI. This change narrows down the mock effect, fixing the problem. Signed-off-by: Lukasz Mrugala <[email protected]>
1 parent 0794b22 commit a0d1b77

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

scripts/tests/twister/test_environment.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ def test_parse_arguments(zephyr_base, additional_args):
274274
testsuite_root=[
275275
os.path.join('dummy', 'path', "tests"),
276276
os.path.join('dummy', 'path', "samples")
277-
]
277+
],
278+
outdir='dummy_abspath',
278279
),
279280
mock.Mock(
280281
generator_cmd='ninja',
@@ -294,7 +295,8 @@ def test_parse_arguments(zephyr_base, additional_args):
294295
testsuite_root=[
295296
os.path.join('dummy', 'path', "tests"),
296297
os.path.join('dummy', 'path', "samples")
297-
]
298+
],
299+
outdir='dummy_abspath',
298300
),
299301
mock.Mock(
300302
generator_cmd='make',
@@ -320,9 +322,17 @@ def test_parse_arguments(zephyr_base, additional_args):
320322
]
321323
)
322324
def test_twisterenv_init(options, expected_env):
323-
with mock.patch(
324-
'os.path.abspath',
325-
mock.Mock(return_value='dummy_abspath')):
325+
original_abspath = os.path.abspath
326+
327+
def mocked_abspath(path):
328+
if path == 'dummy_abspath':
329+
return 'dummy_abspath'
330+
elif isinstance(path, mock.Mock):
331+
return None
332+
else:
333+
return original_abspath(path)
334+
335+
with mock.patch('os.path.abspath', side_effect=mocked_abspath):
326336
twister_env = twisterlib.environment.TwisterEnv(options=options)
327337

328338
assert twister_env.generator_cmd == expected_env.generator_cmd
@@ -339,9 +349,17 @@ def test_twisterenv_discover():
339349
ninja=True
340350
)
341351

342-
abspath_mock = mock.Mock(return_value='dummy_abspath')
352+
original_abspath = os.path.abspath
343353

344-
with mock.patch('os.path.abspath', abspath_mock):
354+
def mocked_abspath(path):
355+
if path == 'dummy_abspath':
356+
return 'dummy_abspath'
357+
elif isinstance(path, mock.Mock):
358+
return None
359+
else:
360+
return original_abspath(path)
361+
362+
with mock.patch('os.path.abspath', side_effect=mocked_abspath):
345363
twister_env = twisterlib.environment.TwisterEnv(options=options)
346364

347365
mock_datetime = mock.Mock(
@@ -435,9 +453,17 @@ def mock_run(command, *args, **kwargs):
435453
ninja=True
436454
)
437455

438-
abspath_mock = mock.Mock(return_value='dummy_abspath')
456+
original_abspath = os.path.abspath
457+
458+
def mocked_abspath(path):
459+
if path == 'dummy_abspath':
460+
return 'dummy_abspath'
461+
elif isinstance(path, mock.Mock):
462+
return None
463+
else:
464+
return original_abspath(path)
439465

440-
with mock.patch('os.path.abspath', abspath_mock):
466+
with mock.patch('os.path.abspath', side_effect=mocked_abspath):
441467
twister_env = twisterlib.environment.TwisterEnv(options=options)
442468

443469
with mock.patch('subprocess.run', mock.Mock(side_effect=mock_run)):
@@ -555,9 +581,17 @@ def test_get_toolchain(caplog, script_result, exit_value, expected_log):
555581
ninja=True
556582
)
557583

558-
abspath_mock = mock.Mock(return_value='dummy_abspath')
584+
original_abspath = os.path.abspath
585+
586+
def mocked_abspath(path):
587+
if path == 'dummy_abspath':
588+
return 'dummy_abspath'
589+
elif isinstance(path, mock.Mock):
590+
return None
591+
else:
592+
return original_abspath(path)
559593

560-
with mock.patch('os.path.abspath', abspath_mock):
594+
with mock.patch('os.path.abspath', side_effect=mocked_abspath):
561595
twister_env = twisterlib.environment.TwisterEnv(options=options)
562596

563597
with mock.patch.object(

0 commit comments

Comments
 (0)