scripts: runners: add type checking for west_commands

Use mypy to type check the runners package.

The test procedure is now annoying enough to replicate locally that
I'm going to wrap it in a script. Do this for both UNIX and Windows
environments by writing that script in Python.

Keep the GitHub workflow up to date so we now get mypy results in CI.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-08-21 15:47:22 -07:00 committed by Maureen Helm
parent 1bb39a6434
commit bd827056f6
5 changed files with 56 additions and 12 deletions

View file

@ -54,12 +54,13 @@ jobs:
${{ runner.os }}-pip-${{ matrix.python-version }}
- name: install pytest
run: |
pip3 install pytest west pyelftools canopen progress
pip3 install wheel
pip3 install pytest west pyelftools canopen progress mypy
- name: run pytest-win
if: runner.os == 'Windows'
run: |
cmd /C "set PYTHONPATH=.\scripts\west_commands && pytest ./scripts/west_commands/tests/"
python ./scripts/west_commands/run_tests.py
- name: run pytest-mac-linux
if: runner.os != 'Windows'
run: |
PYTHONPATH=./scripts/west_commands pytest ./scripts/west_commands/tests/
./scripts/west_commands/run_tests.py

View file

@ -18,6 +18,7 @@ coverage
# used for west-command testing
pytest
mypy
# used for mocking functions in pytest
mock>=4.0.1

View file

@ -1,22 +1,22 @@
This directory contains implementations for west commands which are
tightly coupled to the zephyr tree. Currently, those are the build,
flash, and debug commands.
tightly coupled to the zephyr tree. This includes the build, flash,
and debug commands.
Before adding more here, consider whether you might want to put new
extensions in upstream west. For example, any commands which operate
on the multi-repo need to be in upstream west, not here. Try to limit
what goes in here to just those files that change along with Zephyr
itself.
what goes in here to Zephyr-specific features.
When extending this code, please keep the unit tests (in tests/) up to
date. You can run the tests with this command from this directory:
date. The mypy static type checker is also run on the runners package.
$ PYTHONPATH=$PWD py.test
To run these tests locally on Windows, run:
Windows users will need to find the path to .west/west/src in their
Zephyr installation, then run something like this:
py -3 run_tests.py
> cmd /C "set PYTHONPATH=path\to\zephyr\scripts\west_commands && py.test"
On macOS and Linux:
./run_tests.py
Note that these tests are run as part of Zephyr's CI when submitting
an upstream pull request, and pull requests which break the tests

View file

@ -0,0 +1,2 @@
[mypy]
ignore_missing_imports=True

View file

@ -0,0 +1,40 @@
#!/usr/bin/env python3
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
# A convenience script provided for running tests on the runners
# package. Runs mypy and pytest. Any extra arguments in sys.argv are
# passed along to pytest.
#
# Using tox was considered, but rejected as overkill for now.
#
# We would have to configure tox to create the test virtualenv with
# all of zephyr's scripts/requirements.txt, which seems like too much
# effort for now. We just run in the same Python environment as the
# user for developer testing and trust CI to set that environment up
# properly for integration testing.
#
# If this file starts to reimplement too many features that are
# already available in tox, we can revisit this decision.
import os
import shlex
import subprocess
import sys
here = os.path.abspath(os.path.dirname(__file__))
mypy = [sys.executable, '-m', 'mypy', f'--config-file={here}/mypy.ini',
'--package', 'runners']
pytest = [sys.executable, '-m', 'pytest'] + sys.argv[1:]
print(f'Running mypy from {here}:\n\t' +
' '.join(shlex.quote(s) for s in mypy),
flush=True)
subprocess.run(mypy, check=True, cwd=here)
print(f'Running pytest from {here}:\n\t' +
' '.join(shlex.quote(s) for s in pytest),
flush=True)
subprocess.run(pytest, check=True, cwd=here)