Skip to content

Commit d2e398d

Browse files
committed
Update infrastructure for running Python unit tests
Arduino tooling projects use a standardized infrastructure. A centralized collection of reusable infrastructure assets is maintained in a dedicated repository: https://github.com/arduino/tooling-project-assets Since the time this project's infrastructure was installed, some advancements have been made in the upstream "template" assets. The project's infrastructure is hereby brought up to date with the state of the art upstream assets. The significant changes: - Migration to the use of the Task task runner to allow contributors to easily run the same operations locally as is done by the CI system on GitHub.
1 parent 7e1fd1f commit d2e398d

File tree

6 files changed

+153
-102
lines changed

6 files changed

+153
-102
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-python-poetry-task.md
2+
name: Test Python
3+
4+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
5+
on:
6+
create:
7+
push:
8+
paths:
9+
- ".github/workflows/test-python-poetry-task.ya?ml"
10+
- ".github/.?codecov.ya?ml"
11+
- "dev/.?codecov.ya?ml"
12+
- ".?codecov.ya?ml"
13+
- "Taskfile.ya?ml"
14+
- ".python-version"
15+
- "poetry.lock"
16+
- "pyproject.toml"
17+
- "compilesketches/tests/**"
18+
- "**.py"
19+
pull_request:
20+
paths:
21+
- ".github/workflows/test-python-poetry-task.ya?ml"
22+
- ".github/.?codecov.ya?ml"
23+
- "dev/.?codecov.ya?ml"
24+
- ".?codecov.ya?ml"
25+
- "Taskfile.ya?ml"
26+
- ".python-version"
27+
- "poetry.lock"
28+
- "pyproject.toml"
29+
- "compilesketches/tests/**"
30+
- "**.py"
31+
schedule:
32+
# Run periodically to catch breakage caused by external changes.
33+
- cron: "0 12 * * WED"
34+
workflow_dispatch:
35+
repository_dispatch:
36+
37+
jobs:
38+
run-determination:
39+
runs-on: ubuntu-latest
40+
outputs:
41+
result: ${{ steps.determination.outputs.result }}
42+
steps:
43+
- name: Determine if the rest of the workflow should run
44+
id: determination
45+
run: |
46+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
47+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
48+
if [[
49+
"${{ github.event_name }}" != "create" ||
50+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
51+
]]; then
52+
# Run the other jobs.
53+
RESULT="true"
54+
else
55+
# There is no need to run the other jobs.
56+
RESULT="false"
57+
fi
58+
59+
echo "result=$RESULT" >> $GITHUB_OUTPUT
60+
61+
test:
62+
needs: run-determination
63+
if: needs.run-determination.outputs.result == 'true'
64+
runs-on: ubuntu-latest
65+
66+
env:
67+
COVERAGE_DATA_FILENAME: coverage.xml
68+
69+
steps:
70+
- name: Checkout repository
71+
uses: actions/checkout@v3
72+
73+
- name: Install Python
74+
uses: actions/setup-python@v4
75+
with:
76+
python-version-file: .python-version
77+
78+
- name: Install Poetry
79+
run: |
80+
pipx install \
81+
--python "$(which python)" \
82+
poetry
83+
84+
- name: Install Task
85+
uses: arduino/setup-task@v1
86+
with:
87+
repo-token: ${{ secrets.GITHUB_TOKEN }}
88+
version: 3.x
89+
90+
- name: Run tests
91+
uses: liskin/gh-problem-matcher-wrap@v2
92+
with:
93+
linters: pytest
94+
run: task python:test
95+
96+
- name: Display code coverage report
97+
run: task python:coverage-report
98+
99+
# codecov/codecov-action only makes the conversion if the `coverage` package is installed in the global runner
100+
# environment
101+
- name: Convert code coverage report to format required by Codecov
102+
run: |
103+
poetry run \
104+
coverage xml \
105+
-o "${{ github.workspace }}/${{ env.COVERAGE_DATA_FILENAME }}"
106+
107+
- name: Upload coverage report to Codecov
108+
uses: codecov/codecov-action@v3
109+
with:
110+
fail_ci_if_error: true
111+
file: ${{ env.COVERAGE_DATA_FILENAME }}

.github/workflows/test-python.yml

Lines changed: 0 additions & 94 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# `arduino/compile-sketches` action
22

33
[![Check Action Metadata status](https://github.com/arduino/compile-sketches/actions/workflows/check-action-metadata-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-action-metadata-task.yml)
4-
[![Tests](https://github.com/arduino/compile-sketches/workflows/Test%20Python%20code/badge.svg)](https://github.com/arduino/compile-sketches/actions?workflow=Test+Python+code)
54
[![Check Python status](https://github.com/arduino/compile-sketches/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-python-task.yml)
65
[![Spell Check status](https://github.com/arduino/compile-sketches/actions/workflows/spell-check-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/spell-check-task.yml)
76
[![Sync Labels status](https://github.com/arduino/compile-sketches/actions/workflows/sync-labels-npm.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/sync-labels-npm.yml)
7+
[![Test Python status](https://github.com/arduino/compile-sketches/actions/workflows/test-python-poetry-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/test-python-poetry-task.yml)
88
[![codecov](https://codecov.io/gh/arduino/compile-sketches/branch/main/graph/badge.svg?token=Uv6f1ebMZ4)](https://codecov.io/gh/arduino/compile-sketches)
99

1010
This action checks whether [Arduino](https://www.arduino.cc/) sketches compile and produces a report of data from the compilations.

Taskfile.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# See: https://taskfile.dev/#/usage
22
version: "3"
33

4+
vars:
5+
PYTHON_PROJECT_PATH: compilesketches
6+
47
tasks:
58
check:
69
desc: Check for problems with the project
710
deps:
811
- task: action:validate
912
- task: general:check-spelling
1013
- task: python:lint
14+
- task: python:test
1115

1216
fix:
1317
desc: Make automated corrections to the project's files
@@ -74,6 +78,17 @@ tasks:
7478
--no-root \
7579
{{if .POETRY_GROUPS}} --only {{.POETRY_GROUPS}} {{end}}
7680
81+
python:coverage-report:
82+
desc: Show code coverage report
83+
deps:
84+
- task: poetry:install-deps
85+
vars:
86+
POETRY_GROUPS: dev
87+
cmds:
88+
- |
89+
poetry run \
90+
coverage report
91+
7792
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
7893
python:format:
7994
desc: Format Python files
@@ -94,6 +109,22 @@ tasks:
94109
cmds:
95110
- poetry run flake8 --show-source
96111

112+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-python-poetry-task/Taskfile.yml
113+
python:test:
114+
desc: Run Python tests
115+
deps:
116+
- task: poetry:install-deps
117+
vars:
118+
POETRY_GROUPS: dev,main
119+
cmds:
120+
- |
121+
export PYTHONPATH="${PWD}/{{.PYTHON_PROJECT_PATH}}"
122+
poetry run \
123+
coverage run \
124+
--source="{{.PYTHON_PROJECT_PATH}}" \
125+
--module \
126+
pytest "{{.PYTHON_PROJECT_PATH}}/tests"
127+
97128
# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
98129
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
99130
utility:mktemp-file:

compilesketches/tests/pytest.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-python/pytest.ini
2+
[pytest]
3+
filterwarnings =
4+
error
5+
ignore::DeprecationWarning
6+
ignore::ResourceWarning
7+
8+
# --capture=no - disable per-test capture
9+
# --tb=long sets the length of the traceback in case of failures
10+
addopts = --capture=no --tb=long --verbose

pyproject.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ optional = true
3131
[tool.poetry.group.external.dependencies]
3232
pyserial = "3.5"
3333

34-
[tool.pytest.ini_options]
35-
filterwarnings = [
36-
"error",
37-
"ignore::DeprecationWarning",
38-
"ignore::ResourceWarning"
39-
]
40-
4134
[build-system]
4235
requires = ["poetry-core"]
4336
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)