Skip to content

Commit 131b1c3

Browse files
allow running pytest directly
1 parent ba70229 commit 131b1c3

4 files changed

Lines changed: 59 additions & 8 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,6 @@ env = {PYTHONIOENCODING = "utf-8"}
115115
lint = "ruff check ."
116116
types = "mypy --package west"
117117
all = ["test", "lint", "types"]
118+
119+
[tool.pytest.ini_options]
120+
pythonpath = ["src"]

src/west/app/main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
import colorama
3232

33+
if __name__ == "__main__":
34+
# Prepend the project root to sys.path so that running this script
35+
# directly uses the local 'west' modules instead of any installed version.
36+
project_root = Path(__file__).resolve().parents[2]
37+
sys.path.insert(0, os.fspath(project_root))
38+
3339
import west.configuration
3440
from west import log
3541
from west.app.config import Config

tests/conftest.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,24 @@
1313

1414
import pytest
1515

16+
import west.version
1617
from west import configuration as config
18+
from west.app import main
19+
20+
# Expected PYTHONPATH pointing to the current source directory
21+
PYTHONPATH = os.fspath(Path(west.version.__file__).resolve().parents[1])
22+
23+
# West's main Python script, runnable as a command-line executable
24+
WEST_MAIN = main.__file__
25+
26+
# installed west executable (only set if it is correct version)
27+
WEST_EXE = shutil.which('west')
28+
if WEST_EXE:
29+
# ensure that installed west exe has expected version
30+
expected = west.version.__version__
31+
actual = subprocess.check_output([WEST_EXE,'--version'], text=True)
32+
if expected not in actual:
33+
WEST_EXE = None
1734

1835
GIT = shutil.which('git')
1936

@@ -324,17 +341,21 @@ def cmd(cmd, cwd=None, stderr=None, env=None):
324341
#
325342
# This helper relies on the test environment to ensure that the
326343
# 'west' executable is a bootstrapper installed from the current
327-
# west source code.
344+
# west source code. If installed 'west' executable has correct version,
345+
# it is preferred. Otherwise, fall back to running the local Python module.
328346
#
329347
# stdout from cmd is captured and returned. The command is run in
330348
# a python subprocess so that program-level setup and teardown
331349
# happen fresh.
332350

333351
# If you have quoting issues: do NOT quote. It's not portable.
334352
# Instead, pass `cmd` as a list.
335-
cmd = ['west'] + (cmd.split() if isinstance(cmd, str) else cmd)
336353

354+
west_exe = WEST_EXE or WEST_MAIN
355+
356+
cmd = [west_exe] + (cmd.split() if isinstance(cmd, str) else cmd)
337357
print('running:', cmd)
358+
338359
if env:
339360
print('with non-default environment:')
340361
for k in env:
@@ -349,7 +370,7 @@ def cmd(cmd, cwd=None, stderr=None, env=None):
349370
try:
350371
return check_output(cmd, cwd=cwd, stderr=stderr, env=env)
351372
except subprocess.CalledProcessError:
352-
print('cmd: west:', shutil.which('west'), file=sys.stderr)
373+
print('cmd: west:', west_exe, file=sys.stderr)
353374
raise
354375

355376

tests/test_main.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import os
12
import subprocess
23
import sys
34

5+
from conftest import PYTHONPATH, WEST_EXE, cmd
6+
47
import west.version
58

69

@@ -11,8 +14,26 @@ def test_main():
1114
# sane (i.e. the actual version number is printed instead of
1215
# simply an error message to stderr).
1316

14-
output_as_module = subprocess.check_output([sys.executable, '-m', 'west',
15-
'--version']).decode()
16-
output_directly = subprocess.check_output(['west', '--version']).decode()
17-
assert west.version.__version__ in output_as_module
18-
assert output_as_module == output_directly
17+
expected_version = west.version.__version__
18+
19+
# call west executable directly
20+
output_directly = cmd(['--version'])
21+
assert expected_version in output_directly
22+
23+
# Note: if a wrong west version is installed, local PYTHONPATH needs to be
24+
# prepended so that the called process uses correct west python modules
25+
env = os.environ.copy()
26+
if not WEST_EXE:
27+
val = os.getenv('PYTHONPATH')
28+
env['PYTHONPATH'] = f'{PYTHONPATH}{os.pathsep}{val}' if val else PYTHONPATH
29+
30+
# execute west as a python module
31+
output_as_module = subprocess.check_output(
32+
[sys.executable, '-m', 'west','--version'],
33+
env=env,
34+
text=True
35+
)
36+
assert expected_version in output_as_module
37+
38+
# output must be same in both cases
39+
assert output_as_module.rstrip() == output_directly.rstrip()

0 commit comments

Comments
 (0)