Skip to content

Commit 7bf4782

Browse files
authored
Add CI workflow to test Go code (#8)
On every push and pull request that affects relevant files, run the project's Go code tests.
1 parent d05730b commit 7bf4782

File tree

5 files changed

+130
-10
lines changed

5 files changed

+130
-10
lines changed

.github/workflows/test-go-task.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-go-task.md
2+
name: Test Go
3+
4+
env:
5+
# See: https://github.com/actions/setup-go/tree/main#supported-version-syntax
6+
GO_VERSION: "1.20"
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-go-task.ya?ml"
14+
- ".github/.?codecov.ya?ml"
15+
- "dev/.?codecov.ya?ml"
16+
- ".?codecov.ya?ml"
17+
- "**/go.mod"
18+
- "**/go.sum"
19+
- "Taskfile.ya?ml"
20+
- "**.go"
21+
- "**/testdata/**"
22+
pull_request:
23+
paths:
24+
- ".github/workflows/test-go-task.ya?ml"
25+
- ".github/.?codecov.ya?ml"
26+
- "dev/.?codecov.ya?ml"
27+
- ".?codecov.ya?ml"
28+
- "**/go.mod"
29+
- "**/go.sum"
30+
- "Taskfile.ya?ml"
31+
- "**.go"
32+
- "**/testdata/**"
33+
schedule:
34+
# Run periodically to catch breakage caused by external changes.
35+
- cron: "0 11 * * WED"
36+
workflow_dispatch:
37+
repository_dispatch:
38+
39+
jobs:
40+
run-determination:
41+
runs-on: ubuntu-latest
42+
outputs:
43+
result: ${{ steps.determination.outputs.result }}
44+
permissions: {}
45+
steps:
46+
- name: Determine if the rest of the workflow should run
47+
id: determination
48+
run: |
49+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
50+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
51+
if [[
52+
"${{ github.event_name }}" != "create" ||
53+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
54+
]]; then
55+
# Run the other jobs.
56+
RESULT="true"
57+
else
58+
# There is no need to run the other jobs.
59+
RESULT="false"
60+
fi
61+
62+
echo "result=$RESULT" >> $GITHUB_OUTPUT
63+
64+
test:
65+
name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }})
66+
needs: run-determination
67+
if: needs.run-determination.outputs.result == 'true'
68+
permissions:
69+
contents: read
70+
71+
strategy:
72+
fail-fast: false
73+
74+
matrix:
75+
operating-system:
76+
- ubuntu-latest
77+
- windows-latest
78+
- macos-latest
79+
module:
80+
# TODO: add paths of all Go modules here
81+
- path: ./
82+
codecov-flags: unit
83+
84+
runs-on: ${{ matrix.operating-system }}
85+
86+
steps:
87+
- name: Checkout repository
88+
uses: actions/checkout@v3
89+
90+
- name: Install Go
91+
uses: actions/setup-go@v4
92+
with:
93+
go-version: ${{ env.GO_VERSION }}
94+
95+
- name: Install Task
96+
uses: arduino/setup-task@v1
97+
with:
98+
repo-token: ${{ secrets.GITHUB_TOKEN }}
99+
version: 3.x
100+
101+
- name: Run tests
102+
env:
103+
GO_MODULE_PATH: ${{ matrix.module.path }}
104+
run: task go:test
105+
106+
- name: Send unit tests coverage to Codecov
107+
if: runner.os == 'Linux'
108+
uses: codecov/codecov-action@v3
109+
with:
110+
file: ${{ matrix.module.path }}coverage_unit.txt
111+
flags: ${{ matrix.module.codecov-flags }}
112+
fail_ci_if_error: ${{ github.repository == 'arduino/uno-r4-wifi-fwuploader-plugin' }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bossac
2727
espflash
2828
fw
2929
uno-r4-wifi-fwuploader-plugin
30+
uno-r4-wifi-fwuploader-plugin.exe
3031
sketches/commands/build/arduino.renesas_uno.unor4wifi/commands.ino.elf
3132
sketches/commands/build/arduino.renesas_uno.unor4wifi/commands.ino.hex
3233
sketches/commands/build/arduino.renesas_uno.unor4wifi/commands.ino.map

DistTasks.yml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tasks:
3030
mkdir {{.PLATFORM_DIR}}
3131
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
3232
docker run -v `pwd`/..:/home/build -w /home/build \
33-
-e CGO_ENABLED=1 \
33+
-e CGO_ENABLED=0 \
3434
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
3535
--build-cmd "{{.BUILD_COMMAND}}" \
3636
-p "{{.BUILD_PLATFORM}}"
@@ -54,7 +54,7 @@ tasks:
5454
mkdir {{.PLATFORM_DIR}}
5555
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
5656
docker run -v `pwd`/..:/home/build -w /home/build \
57-
-e CGO_ENABLED=1 \
57+
-e CGO_ENABLED=0 \
5858
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
5959
--build-cmd "{{.BUILD_COMMAND}}" \
6060
-p "{{.BUILD_PLATFORM}}"
@@ -78,7 +78,7 @@ tasks:
7878
mkdir {{.PLATFORM_DIR}}
7979
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
8080
docker run -v `pwd`/..:/home/build -w /home/build \
81-
-e CGO_ENABLED=1 \
81+
-e CGO_ENABLED=0 \
8282
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
8383
--build-cmd "{{.BUILD_COMMAND}}" \
8484
-p "{{.BUILD_PLATFORM}}"
@@ -102,7 +102,7 @@ tasks:
102102
mkdir {{.PLATFORM_DIR}}
103103
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
104104
docker run -v `pwd`/..:/home/build -w /home/build \
105-
-e CGO_ENABLED=1 \
105+
-e CGO_ENABLED=0 \
106106
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
107107
--build-cmd "{{.BUILD_COMMAND}}" \
108108
-p "{{.BUILD_PLATFORM}}"
@@ -112,7 +112,9 @@ tasks:
112112
vars:
113113
PLATFORM_DIR: "{{.PROJECT_NAME}}_linux_amd64"
114114
BUILD_COMMAND: >
115-
go build -o {{.DIST_DIR}}/{{.PLATFORM_DIR}}/{{.PROJECT_NAME}} {{.LDFLAGS}}
115+
apt update &&
116+
apt install libudev-dev libusb-1.0-0-dev -y &&
117+
go build -tags libusb -o {{.DIST_DIR}}/{{.PLATFORM_DIR}}/{{.PROJECT_NAME}} {{.LDFLAGS}}
116118
BUILD_PLATFORM: "linux/amd64"
117119
CONTAINER_TAG: "{{.GO_VERSION}}-main"
118120
PACKAGE_PLATFORM: "Linux_64bit"
@@ -126,7 +128,7 @@ tasks:
126128
mkdir {{.PLATFORM_DIR}}
127129
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
128130
docker run -v `pwd`/..:/home/build -w /home/build \
129-
-e CGO_ENABLED=1 \
131+
-e CGO_ENABLED=0 \
130132
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
131133
--build-cmd "{{.BUILD_COMMAND}}" \
132134
-p "{{.BUILD_PLATFORM}}"
@@ -150,7 +152,7 @@ tasks:
150152
mkdir {{.PLATFORM_DIR}}
151153
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
152154
docker run -v `pwd`/..:/home/build -w /home/build \
153-
-e CGO_ENABLED=1 \
155+
-e CGO_ENABLED=0 \
154156
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
155157
--build-cmd "{{.BUILD_COMMAND}}" \
156158
-p "{{.BUILD_PLATFORM}}"
@@ -174,7 +176,7 @@ tasks:
174176
mkdir {{.PLATFORM_DIR}}
175177
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
176178
docker run -v `pwd`/..:/home/build -w /home/build \
177-
-e CGO_ENABLED=1 \
179+
-e CGO_ENABLED=0 \
178180
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
179181
--build-cmd "{{.BUILD_COMMAND}}" \
180182
-p "{{.BUILD_PLATFORM}}"
@@ -199,7 +201,7 @@ tasks:
199201
mkdir {{.PLATFORM_DIR}}
200202
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
201203
docker run -v `pwd`/..:/home/build -w /home/build \
202-
-e CGO_ENABLED=1 \
204+
-e CGO_ENABLED=0 \
203205
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
204206
--build-cmd "git config --global --add safe.directory /home/build && {{.BUILD_COMMAND}}" \
205207
-p "{{.BUILD_PLATFORM}}"
@@ -224,7 +226,7 @@ tasks:
224226
mkdir {{.PLATFORM_DIR}}
225227
cp ../LICENSE.txt {{.PLATFORM_DIR}}/
226228
docker run -v `pwd`/..:/home/build -w /home/build \
227-
-e CGO_ENABLED=1 \
229+
-e CGO_ENABLED=0 \
228230
{{.CONTAINER}}:{{.CONTAINER_TAG}} \
229231
--build-cmd "git config --global --add safe.directory /home/build && {{.BUILD_COMMAND}}" \
230232
-p "{{.BUILD_PLATFORM}}"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
[![Check Go Dependencies status](https://github.com/arduino/uno-r4-wifi-fwuploader-plugin/actions/workflows/check-go-dependencies-task.yml/badge.svg)](https://github.com/arduino/uno-r4-wifi-fwuploader-plugin/actions/workflows/check-go-dependencies-task.yml)
55
[![Check Go status](https://github.com/arduino/uno-r4-wifi-fwuploader-plugin/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/uno-r4-wifi-fwuploader-plugin/actions/workflows/check-go-task.yml)
66
[![Sync Labels status](https://github.com/arduino/uno-r4-wifi-fwuploader-plugin/actions/workflows/sync-labels.yml/badge.svg)](https://github.com/arduino/fwuploader-plugin-helper/actions/workflows/sync-labels.yml)
7+
[![Test Go status](https://github.com/arduino/uno-r4-wifi-fwuploader-plugin/actions/workflows/test-go-task.yml/badge.svg)](https://github.com/arduino/uno-r4-wifi-fwuploader-plugin/actions/workflows/test-go-task.yml)
8+
[![Codecov](https://codecov.io/gh/arduino/uno-r4-wifi-fwuploader-plugin/branch/main/graph/badge.svg)](https://codecov.io/gh/arduino/uno-r4-wifi-fwuploader-plugin)
79

810
Be sure to have `libudev-dev` installed
911

Taskfile.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,7 @@ vars:
150150
-X {{.CONFIGURATION_PACKAGE}}.commit={{ .COMMIT }}
151151
-X {{.CONFIGURATION_PACKAGE}}.date={{.TIMESTAMP}}
152152
'
153+
# `-ldflags` flag to use for `go test` command
154+
# TODO: define flag if required by the project, or leave empty if not needed.
155+
TEST_LDFLAGS:
153156

0 commit comments

Comments
 (0)