Skip to content

Commit c1d0f44

Browse files
committed
Add template workflow to run integration tests on Go projects
On every push and pull request that affects relevant files, run the pytest-based integration tests. test_all.py is sourced from https://github.com/arduino/arduino-lint/blob/5ef264939ff22a4a825df2e44acdd5033dd16e00/test/test_all.py
1 parent 18549f2 commit c1d0f44

File tree

8 files changed

+316
-0
lines changed

8 files changed

+316
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# See: https://taskfile.dev/#/usage
2+
version: "3"
3+
4+
vars:
5+
LDFLAGS:
6+
7+
tasks:
8+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/shared/go/Taskfile.yml
9+
go:build:
10+
desc: Build the Go code
11+
cmds:
12+
- go build -v {{.LDFLAGS}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# See: https://taskfile.dev/#/usage
2+
version: "3"
3+
4+
tasks:
5+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-go-integration-task/Taskfile.yml
6+
go:test-integration:
7+
desc: Run integration tests
8+
deps:
9+
- task: go:build
10+
- task: poetry:install-deps
11+
cmds:
12+
- poetry run pytest tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Source:
2+
# https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-integration/test_all.py
3+
# Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
4+
#
5+
# This software is released under the GNU General Public License version 3,
6+
# which covers the main part of Arduino Lint.
7+
# The terms of this license can be found at:
8+
# https: // www.gnu.org/licenses/gpl-3.0.en.html
9+
#
10+
# You can be released from the requirements of the above licenses by purchasing
11+
# a commercial license. Buying such a license is mandatory if you want to
12+
# modify or otherwise use the software for commercial activities involving the
13+
# Arduino software without disclosing the source code of your own applications.
14+
# To purchase a commercial license, send an email to [email protected].
15+
import pathlib
16+
import platform
17+
import typing
18+
19+
import invoke.context
20+
import pytest
21+
22+
test_data_path = pathlib.Path(__file__).resolve().parent.joinpath("testdata")
23+
24+
25+
@pytest.fixture(scope="function")
26+
def run_command(
27+
pytestconfig, working_dir
28+
) -> typing.Callable[..., invoke.runners.Result]:
29+
"""Provide a wrapper around invoke's `run` API so that every test will work in the same temporary folder.
30+
31+
Useful reference:
32+
http://docs.pyinvoke.org/en/1.4/api/runners.html#invoke.runners.Result
33+
"""
34+
35+
arduino_lint_path = pathlib.Path(pytestconfig.rootdir).parent / "arduino-lint"
36+
37+
def _run(
38+
cmd: list,
39+
custom_working_dir: typing.Optional[str] = None,
40+
custom_env: typing.Optional[dict] = None,
41+
) -> invoke.runners.Result:
42+
if cmd is None:
43+
cmd = []
44+
if not custom_working_dir:
45+
custom_working_dir = working_dir
46+
quoted_cmd = []
47+
for token in cmd:
48+
quoted_cmd.append(f'"{token}"')
49+
cli_full_line = '"{}" {}'.format(arduino_lint_path, " ".join(quoted_cmd))
50+
run_context = invoke.context.Context()
51+
# It might happen that we need to change directories between drives on Windows,
52+
# in that case the "/d" flag must be used otherwise directory wouldn't change
53+
cd_command = "cd"
54+
if platform.system() == "Windows":
55+
cd_command += " /d"
56+
# Context.cd() is not used since it doesn't work correctly on Windows.
57+
# It escapes spaces in the path using "\ " but it doesn't always work,
58+
# wrapping the path in quotation marks is the safest approach
59+
with run_context.prefix(f'{cd_command} "{custom_working_dir}"'):
60+
return run_context.run(
61+
command=cli_full_line,
62+
echo=False,
63+
hide=True,
64+
warn=True,
65+
env=custom_env,
66+
encoding="utf-8",
67+
)
68+
69+
return _run
70+
71+
72+
@pytest.fixture(scope="function")
73+
def working_dir(tmpdir_factory) -> str:
74+
"""Create a temporary folder for the test to run in. It will be created before running each test and deleted at the
75+
end. This way all the tests work in isolation.
76+
"""
77+
work_dir = tmpdir_factory.mktemp(basename="IntegrationTestWorkingDir")
78+
yield str(work_dir)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Source:
2+
# https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-integration/__init__.py
3+
# Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
4+
#
5+
# This software is released under the GNU General Public License version 3,
6+
# which covers the main part of Arduino Lint.
7+
# The terms of this license can be found at:
8+
# https: // www.gnu.org/licenses/gpl-3.0.en.html
9+
#
10+
# You can be released from the requirements of the above licenses by purchasing
11+
# a commercial license. Buying such a license is mandatory if you want to
12+
# modify or otherwise use the software for commercial activities involving the
13+
# Arduino software without disclosing the source code of your own applications.
14+
# To purchase a commercial license, send an email to [email protected].
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-go-integration-task.md
2+
name: Test Integration
3+
4+
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
5+
on:
6+
push:
7+
paths:
8+
- ".github/workflows/test-go-integration-task.ya?ml"
9+
- "Taskfile.ya?ml"
10+
- "**.go"
11+
- "go.mod"
12+
- "go.sum"
13+
- "poetry.lock"
14+
- "pyproject.toml"
15+
- "tests/**"
16+
pull_request:
17+
paths:
18+
- ".github/workflows/test-go-integration-task.ya?ml"
19+
- "Taskfile.ya?ml"
20+
- "**.go"
21+
- "go.mod"
22+
- "go.sum"
23+
- "poetry.lock"
24+
- "pyproject.toml"
25+
- "tests/**"
26+
workflow_dispatch:
27+
repository_dispatch:
28+
29+
jobs:
30+
test:
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v2
36+
37+
- name: Install Go
38+
uses: actions/setup-go@v2
39+
with:
40+
go-version: "1.14"
41+
42+
- name: Install Python
43+
uses: actions/setup-python@v2
44+
with:
45+
python-version: "3.9"
46+
47+
- name: Install Poetry
48+
run: pip install poetry
49+
50+
- name: Install Taskfile
51+
uses: arduino/setup-task@v1
52+
with:
53+
repo-token: ${{ secrets.GITHUB_TOKEN }}
54+
version: 3.x
55+
56+
- name: Run integration tests
57+
run: task go:test-integration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# "Test Integration" workflow (Go, Task)
2+
3+
Workflow file: [test-go-integration-task.yml](test-go-integration-task.yml)
4+
5+
Run Python integration tests for a [Go](https://golang.org/) module.
6+
7+
This is the version of the workflow for projects using the [Task](https://taskfile.dev/#/) task runner tool.
8+
9+
## Installation
10+
11+
The Python dependencies are managed by [Poetry](https://python-poetry.org/).
12+
13+
Install Poetry by following these instructions:<br />
14+
https://python-poetry.org/docs/#installation
15+
16+
If your project does not already use Poetry, you can initialize the [`pyproject.toml`](https://python-poetry.org/docs/pyproject/) file using these commands:
17+
18+
```
19+
poetry init --python="^3.9" --dev-dependency="pytest@^6.2.4" --dev-dependency="invoke@^1.5.0"
20+
poetry install
21+
```
22+
23+
If already using Poetry, add the tool using this command:
24+
25+
```
26+
poetry add --dev "pytest@^6.2.4" "invoke@^1.5.0"
27+
```
28+
29+
Make sure to commit the resulting `pyproject.toml` and `poetry.lock` files.
30+
31+
## Assets
32+
33+
- [`Taskfile.yml`](assets/test-go-integration-task/Taskfile.yml) - Test runner task.
34+
- Install to: repository root (or add the `go:test-integration` task into the existing `Taskfile.yml`)
35+
- [`Taskfile.yml`](assets/shared/go/Taskfile.yml) - Build task.
36+
- Merge the `go:build` task into the existing `Taskfile.yml`.
37+
- [`__init__.py`](assets/test-python/__init__.py) - Template for Python integration tests.
38+
- Install to: `/tests/`
39+
- [`test_all.py`](assets/test-integration/test_all.py) - Template for Python integration tests.
40+
- Install to: `/tests/`
41+
- [`pytest.ini`](assets/test-python/pytest.ini) - [pytest](https://pytest.org) configuration file.
42+
- Install to: `/tests/`
43+
44+
## Readme badge
45+
46+
Markdown badge:
47+
48+
```markdown
49+
[![Test Integration status](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/test-go-integration-task.yml/badge.svg)](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/test-go-integration-task.yml)
50+
```
51+
52+
Replace the `REPO_OWNER` and `REPO_NAME` placeholders in the URLs with the final repository owner and name ([example](https://raw.githubusercontent.com/arduino-libraries/ArduinoIoTCloud/master/README.md)).
53+
54+
---
55+
56+
Asciidoc badge:
57+
58+
```adoc
59+
image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/test-go-integration-task.yml/badge.svg["Test Integration status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/test-go-integration-task.yml"]
60+
```
61+
62+
Define the `{repository-owner}` and `{repository-name}` attributes and use them throughout the readme ([example](https://raw.githubusercontent.com/arduino-libraries/WiFiNINA/master/README.adoc)).
63+
64+
## Commit message
65+
66+
```
67+
Add CI workflow to run integration tests
68+
69+
On every push and pull request that affects relevant files, run the integration tests.
70+
```
71+
72+
## PR message
73+
74+
```markdown
75+
On every push and pull request that affects relevant files, run the integration tests.
76+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-go-integration-task.md
2+
name: Test Integration
3+
4+
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
5+
on:
6+
push:
7+
paths:
8+
- ".github/workflows/test-go-integration-task.ya?ml"
9+
- "Taskfile.ya?ml"
10+
- "**.go"
11+
- "go.mod"
12+
- "go.sum"
13+
- "poetry.lock"
14+
- "pyproject.toml"
15+
- "tests/**"
16+
pull_request:
17+
paths:
18+
- ".github/workflows/test-go-integration-task.ya?ml"
19+
- "Taskfile.ya?ml"
20+
- "**.go"
21+
- "go.mod"
22+
- "go.sum"
23+
- "poetry.lock"
24+
- "pyproject.toml"
25+
- "tests/**"
26+
workflow_dispatch:
27+
repository_dispatch:
28+
29+
jobs:
30+
test:
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v2
36+
37+
- name: Install Go
38+
uses: actions/setup-go@v2
39+
with:
40+
go-version: "1.14"
41+
42+
- name: Install Python
43+
uses: actions/setup-python@v2
44+
with:
45+
python-version: "3.9"
46+
47+
- name: Install Poetry
48+
run: pip install poetry
49+
50+
- name: Install Taskfile
51+
uses: arduino/setup-task@v1
52+
with:
53+
repo-token: ${{ secrets.GITHUB_TOKEN }}
54+
version: 3.x
55+
56+
- name: Run integration tests
57+
run: task go:test-integration

0 commit comments

Comments
 (0)