From 27110b7b62c0c065472d923de67fc5acf28283c5 Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Sat, 23 Aug 2025 13:04:04 +0200 Subject: [PATCH 1/2] Add infrastructure for pytest and mypy Make mypy run with proper type stubs for micropython RP2. Add basic structure for pytest testing. Signed-off-by: Matthias Blankertz --- .gitignore | 3 +++ DEVELOP.md | 11 +++++++++++ software/mypy.ini | 9 +++++++++ software/pytest.ini | 3 +++ software/src/main.py | 2 +- software/src/utils/buttons.py | 2 +- software/tests/mocks/machine.py | 2 ++ software/tests/mocks/micropython.py | 2 ++ software/tests/mocks/rp2_sd.py | 5 +++++ software/tests/requirements.txt | 1 + software/tests/test_dummy.py | 3 +++ software/tests/utils_test/__init__.py | 0 12 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 DEVELOP.md create mode 100644 software/mypy.ini create mode 100644 software/pytest.ini create mode 100644 software/tests/mocks/machine.py create mode 100644 software/tests/mocks/micropython.py create mode 100644 software/tests/mocks/rp2_sd.py create mode 100644 software/tests/requirements.txt create mode 100644 software/tests/test_dummy.py create mode 100644 software/tests/utils_test/__init__.py diff --git a/.gitignore b/.gitignore index c05765c..a7360b5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ hardware/tonberry-pico/tonberry-pico-backups/ *.kicad_sch-bak *.kicad_sch.lck software/build +software/typings compile_commands.json .dir-locals.el .cache +\#*# +__pycache__ diff --git a/DEVELOP.md b/DEVELOP.md new file mode 100644 index 0000000..6b60080 --- /dev/null +++ b/DEVELOP.md @@ -0,0 +1,11 @@ +# Developer notes + +## How to setup python environment for mypy and pytest + +```bash +cd software +python -m venv test-venv +. test-venv/bin/activate +pip install -r tests/requirements.txt +pip install -U micropython-rp2-pico_w-stubs --target typings +``` diff --git a/software/mypy.ini b/software/mypy.ini new file mode 100644 index 0000000..0744a67 --- /dev/null +++ b/software/mypy.ini @@ -0,0 +1,9 @@ +[mypy] +platform = linux +mypy_path = $MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/typings +custom_typeshed_dir = $MYPY_CONFIG_FILE_DIR/typings +follow_imports = silent +exclude = "typings[\\/].*" +follow_imports_for_stubs = true +no_site_packages = true +check_untyped_defs = true \ No newline at end of file diff --git a/software/pytest.ini b/software/pytest.ini new file mode 100644 index 0000000..d3458fa --- /dev/null +++ b/software/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +pythonpath = tests/mocks src +testpaths = tests \ No newline at end of file diff --git a/software/src/main.py b/software/src/main.py index 98ac1aa..c5862cf 100644 --- a/software/src/main.py +++ b/software/src/main.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: MIT # Copyright (c) 2024-2025 Matthias Blankertz -import aiorepl +import aiorepl # type: ignore import asyncio import machine import micropython diff --git a/software/src/utils/buttons.py b/software/src/utils/buttons.py index 12109fa..140ffc6 100644 --- a/software/src/utils/buttons.py +++ b/software/src/utils/buttons.py @@ -17,7 +17,7 @@ if TYPE_CHECKING: class Buttons: - def __init__(self, cb: ButtonCallback, pin_volup=17, pin_voldown=19, pin_next=18): + def __init__(self, cb: "ButtonCallback", pin_volup=17, pin_voldown=19, pin_next=18): self.VOLUP = micropython.const(1) self.VOLDOWN = micropython.const(2) self.NEXT = micropython.const(3) diff --git a/software/tests/mocks/machine.py b/software/tests/mocks/machine.py new file mode 100644 index 0000000..7c14b67 --- /dev/null +++ b/software/tests/mocks/machine.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Matthias Blankertz diff --git a/software/tests/mocks/micropython.py b/software/tests/mocks/micropython.py new file mode 100644 index 0000000..7c14b67 --- /dev/null +++ b/software/tests/mocks/micropython.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Matthias Blankertz diff --git a/software/tests/mocks/rp2_sd.py b/software/tests/mocks/rp2_sd.py new file mode 100644 index 0000000..e1498d3 --- /dev/null +++ b/software/tests/mocks/rp2_sd.py @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Matthias Blankertz + +class SDCard(): + pass diff --git a/software/tests/requirements.txt b/software/tests/requirements.txt new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/software/tests/requirements.txt @@ -0,0 +1 @@ +pytest diff --git a/software/tests/test_dummy.py b/software/tests/test_dummy.py new file mode 100644 index 0000000..c4c0d4c --- /dev/null +++ b/software/tests/test_dummy.py @@ -0,0 +1,3 @@ +def test_dummy(): + # This is just a dummy test to make pytest not fail because no tests are defined + pass diff --git a/software/tests/utils_test/__init__.py b/software/tests/utils_test/__init__.py new file mode 100644 index 0000000..e69de29 -- 2.39.5 From 95b39247368613281792c41f72835ccfdce3e3c7 Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Wed, 20 Aug 2025 20:32:20 +0200 Subject: [PATCH 2/2] ci: flake8 on all python folders; Run pytest in CI Signed-off-by: Matthias Blankertz --- .gitea/workflows/check-format.yaml | 7 +++++-- .gitea/workflows/unit-tests-python.yaml | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/unit-tests-python.yaml diff --git a/.gitea/workflows/check-format.yaml b/.gitea/workflows/check-format.yaml index d3fb2e0..b72091a 100644 --- a/.gitea/workflows/check-format.yaml +++ b/.gitea/workflows/check-format.yaml @@ -25,8 +25,11 @@ jobs: path: git - name: Check python run: | - cd git/software/src && - find . -iname '*.py' -exec ../../../flake-venv/bin/flake8 {} + + cd git/software && ( + find src -iname '*.py' -exec ../../flake-venv/bin/flake8 {} + + find tests -iname '*.py' -exec ../../flake-venv/bin/flake8 {} + + find modules -iname '*.py' -exec ../../flake-venv/bin/flake8 {} + + ) Check-Bash-Shellcheck: runs-on: ubuntu-22.04-full steps: diff --git a/.gitea/workflows/unit-tests-python.yaml b/.gitea/workflows/unit-tests-python.yaml new file mode 100644 index 0000000..2b7ad2b --- /dev/null +++ b/.gitea/workflows/unit-tests-python.yaml @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Matthias Blankertz +--- +name: Run pytests +"on": + push: + +jobs: + Check-Pytest: + runs-on: ubuntu-22.04-full + steps: + - name: Check out repository code + uses: actions/checkout@v4 + with: + path: git + - name: Get dependencies + run: | + python -m venv test-venv + test-venv/bin/pip install -r git/software/tests/requirements.txt + - name: Run pytest + run: | + . test-venv/bin/activate && + cd git/software && + pytest -- 2.39.5