Skip to content

Commit 2b2d372

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 6e5e2cc commit 2b2d372

File tree

6 files changed

+162
-81
lines changed

6 files changed

+162
-81
lines changed

.github/workflows/libraries_report-size-deltas.yml

-78
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-python-poetry-task.md
2+
name: Test Python
3+
4+
env:
5+
# See: https://github.com/actions/setup-python/tree/main#available-versions-of-python
6+
PYTHON_VERSION: "3.11.6"
7+
8+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/test-python-poetry-task.ya?ml"
14+
- ".github/.?codecov.ya?ml"
15+
- "dev/.?codecov.ya?ml"
16+
- ".?codecov.ya?ml"
17+
- "Taskfile.ya?ml"
18+
- ".python-version"
19+
- "poetry.lock"
20+
- "pyproject.toml"
21+
- "reportsizedeltas/tests/**"
22+
- "**.py"
23+
pull_request:
24+
paths:
25+
- ".github/workflows/test-python-poetry-task.ya?ml"
26+
- ".github/.?codecov.ya?ml"
27+
- "dev/.?codecov.ya?ml"
28+
- ".?codecov.ya?ml"
29+
- "Taskfile.ya?ml"
30+
- ".python-version"
31+
- "poetry.lock"
32+
- "pyproject.toml"
33+
- "reportsizedeltas/tests/**"
34+
- "**.py"
35+
schedule:
36+
# Run periodically to catch breakage caused by external changes.
37+
- cron: "0 12 * * WED"
38+
workflow_dispatch:
39+
repository_dispatch:
40+
41+
jobs:
42+
run-determination:
43+
runs-on: ubuntu-latest
44+
outputs:
45+
result: ${{ steps.determination.outputs.result }}
46+
permissions: {}
47+
steps:
48+
- name: Determine if the rest of the workflow should run
49+
id: determination
50+
run: |
51+
RELEASE_BRANCH_REGEX="^refs/heads/v[0-9]+$"
52+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
53+
if [[
54+
"${{ github.event_name }}" != "create" ||
55+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
56+
]]; then
57+
# Run the other jobs.
58+
RESULT="true"
59+
else
60+
# There is no need to run the other jobs.
61+
RESULT="false"
62+
fi
63+
64+
echo "result=$RESULT" >> $GITHUB_OUTPUT
65+
66+
test:
67+
needs: run-determination
68+
if: needs.run-determination.outputs.result == 'true'
69+
runs-on: ubuntu-latest
70+
permissions:
71+
contents: read
72+
73+
env:
74+
COVERAGE_DATA_FILENAME: coverage.xml
75+
76+
steps:
77+
- name: Checkout repository
78+
uses: actions/checkout@v4
79+
80+
- name: Install Python
81+
uses: actions/setup-python@v5
82+
with:
83+
python-version: ${{ env.PYTHON_VERSION }}
84+
85+
- name: Install Poetry
86+
run: pip install poetry
87+
88+
- name: Install Task
89+
uses: arduino/setup-task@v1
90+
with:
91+
repo-token: ${{ secrets.GITHUB_TOKEN }}
92+
version: 3.x
93+
94+
- name: Run tests
95+
uses: liskin/gh-problem-matcher-wrap@v3
96+
with:
97+
linters: pytest
98+
run: task python:test
99+
100+
- name: Display code coverage report
101+
run: task python:coverage-report
102+
103+
# codecov/codecov-action only makes the conversion if the `coverage` package is installed in the global runner
104+
# environment
105+
- name: Convert code coverage report to format required by Codecov
106+
run: |
107+
poetry run \
108+
coverage xml \
109+
-o "${{ github.workspace }}/${{ env.COVERAGE_DATA_FILENAME }}"
110+
111+
# A token is used to avoid intermittent spurious job failures caused by rate limiting.
112+
- name: Set up Codecov upload token
113+
run: |
114+
if [[ "${{ github.repository }}" == "arduino/compile-sketches" ]]; then
115+
# In order to avoid uploads of data from forks, only use the token for runs in the parent repo.
116+
# Token is intentionally exposed.
117+
# See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954
118+
CODECOV_TOKEN="3889efcb-4ed3-4d07-980a-99a3896bd9b8"
119+
else
120+
# codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input.
121+
CODECOV_TOKEN=""
122+
fi
123+
echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV"
124+
125+
- name: Upload coverage report to Codecov
126+
uses: codecov/codecov-action@v3
127+
with:
128+
fail_ci_if_error: true
129+
file: ${{ env.COVERAGE_DATA_FILENAME }}
130+
token: ${{ env.CODECOV_TOKEN }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/node_modules/
2+
__pycache__/
3+
.coverage

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
[![Check Poetry status](https://github.com/arduino/report-size-deltas/actions/workflows/check-poetry-task.yml/badge.svg)](https://github.com/arduino/report-size-deltas/actions/workflows/check-poetry-task.yml)
55
[![Check Python status](https://github.com/arduino/report-size-deltas/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/arduino/report-size-deltas/actions/workflows/check-python-task.yml)
66
[![Check Taskfiles status](https://github.com/arduino/report-size-deltas/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/report-size-deltas/actions/workflows/check-taskfiles.yml)
7-
[![Tests](https://github.com/arduino/report-size-deltas/workflows/libraries/report-size-deltas%20workflow/badge.svg)](https://github.com/arduino/report-size-deltas/actions?workflow=libraries/report-size-deltas+workflow)
87
[![Integration Tests](https://github.com/arduino/report-size-deltas/actions/workflows/test-integration.yml/badge.svg)](https://github.com/arduino/report-size-deltas/actions/workflows/test-integration.yml)
98
[![Spell Check status](https://github.com/arduino/report-size-deltas/actions/workflows/spell-check-task.yml/badge.svg)](https://github.com/arduino/report-size-deltas/actions/workflows/spell-check-task.yml)
109
[![Sync Labels status](https://github.com/arduino/report-size-deltas/actions/workflows/sync-labels-npm.yml/badge.svg)](https://github.com/arduino/report-size-deltas/actions/workflows/sync-labels-npm.yml)
10+
[![Test Python status](https://github.com/arduino/report-size-deltas/actions/workflows/test-python-poetry-task.yml/badge.svg)](https://github.com/arduino/report-size-deltas/actions/workflows/test-python-poetry-task.yml)
1111
[![codecov](https://codecov.io/gh/arduino/report-size-deltas/branch/master/graph/badge.svg)](https://codecov.io/gh/arduino/report-size-deltas)
1212

1313
This action comments on the pull request with a report on the resulting change in memory usage of the [Arduino](https://www.arduino.cc/) sketches compiled by the [`arduino/compile-sketches`](https://github.com/arduino/compile-sketches) action. This should be run from a [scheduled workflow](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#onschedule).

Taskfile.yml

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
version: "3"
33

44
vars:
5+
PYTHON_PROJECT_PATH: reportsizedeltas
56
# Last version of ajv-cli with support for the JSON schema "Draft 4" specification
67
SCHEMA_DRAFT_4_AJV_CLI_VERSION: 3.3.0
78

@@ -14,6 +15,7 @@ tasks:
1415
- task: npm:validate
1516
- task: poetry:validate
1617
- task: python:lint
18+
- task: python:test
1719

1820
fix:
1921
desc: Make automated corrections to the project's files
@@ -139,6 +141,15 @@ tasks:
139141
check \
140142
--lock
141143
144+
python:coverage-report:
145+
desc: Show code coverage report
146+
deps:
147+
- task: poetry:install-deps
148+
cmds:
149+
- |
150+
poetry run \
151+
coverage report
152+
142153
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
143154
python:format:
144155
desc: Format Python files
@@ -155,6 +166,20 @@ tasks:
155166
cmds:
156167
- poetry run flake8 --show-source
157168

169+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-python-poetry-task/Taskfile.yml
170+
python:test:
171+
desc: Run Python tests
172+
deps:
173+
- task: poetry:install-deps
174+
cmds:
175+
- |
176+
export PYTHONPATH="${PWD}/{{.PYTHON_PROJECT_PATH}}"
177+
poetry run \
178+
coverage run \
179+
--source="{{.PYTHON_PROJECT_PATH}}" \
180+
--module \
181+
pytest "{{.PYTHON_PROJECT_PATH}}/tests"
182+
158183
# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
159184
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
160185
utility:mktemp-file:

reportsizedeltas/tests/pytest.ini

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-python/pytest.ini
12
[pytest]
23
filterwarnings =
34
error
45
ignore::DeprecationWarning
56
ignore::ResourceWarning
67

7-
markers =
8-
slow: marks tests as slow (deselect with '-m "not slow"')
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

0 commit comments

Comments
 (0)