Skip to content

Commit a4ce10a

Browse files
thorsten-kleinpdgendt
authored andcommitted
added test for executing west/app/main.py directly
Test that the Python module search path is prepended with correct local module path if west/app/main.py is executed directly.
1 parent 879bb01 commit a4ce10a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/test_main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import runpy
2+
import sys
3+
from pathlib import Path
4+
5+
import pytest
16
from conftest import cmd, cmd_subprocess
27

38
import west.version
@@ -21,3 +26,25 @@ def test_main():
2126

2227
# output must be same in both cases
2328
assert output_subprocess.rstrip() == output_directly.rstrip()
29+
30+
31+
def test_module_run(tmp_path, monkeypatch):
32+
actual_path = ['initial-path']
33+
34+
# mock sys.argv and sys.path
35+
monkeypatch.setattr(sys, 'path', actual_path)
36+
monkeypatch.setattr(sys, 'argv', ['west', '--version'])
37+
38+
# ensure that west.app.main is freshly loaded
39+
sys.modules.pop('west.app.main', None)
40+
41+
# run west.app.main as module
42+
with pytest.raises(SystemExit) as exit_info:
43+
runpy.run_module('west.app.main', run_name='__main__')
44+
45+
# check that exit code is 0
46+
assert exit_info.value.code == 0
47+
48+
# check that that the sys.path was correctly inserted
49+
expected_path = Path(__file__).parents[1] / 'src'
50+
assert actual_path == [f'{expected_path}', 'initial-path']

0 commit comments

Comments
 (0)