Skip to content

Commit e176a59

Browse files
committed
Add CI workflow to test Go code
On every push and pull request that affects relevant files, run the project's Go code tests.
1 parent 047122c commit e176a59

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

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

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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/v2#readme
6+
GO_VERSION: "1.16"
7+
8+
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/test-go-task.ya?ml"
14+
- "codecov.ya?ml"
15+
- "**/go.mod"
16+
- "**/go.sum"
17+
- "Taskfile.ya?ml"
18+
- "**.go"
19+
- "**/testdata/**"
20+
pull_request:
21+
paths:
22+
- ".github/workflows/test-go-task.ya?ml"
23+
- "codecov.ya?ml"
24+
- "**/go.mod"
25+
- "**/go.sum"
26+
- "Taskfile.ya?ml"
27+
- "**.go"
28+
- "**/testdata/**"
29+
workflow_dispatch:
30+
repository_dispatch:
31+
32+
jobs:
33+
run-determination:
34+
runs-on: ubuntu-latest
35+
outputs:
36+
result: ${{ steps.determination.outputs.result }}
37+
steps:
38+
- name: Determine if the rest of the workflow should run
39+
id: determination
40+
run: |
41+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
42+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
43+
if [[ \
44+
"${{ github.event_name }}" != "create" || \
45+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX \
46+
]]; then
47+
# Run the other jobs.
48+
RESULT="true"
49+
else
50+
# There is no need to run the other jobs.
51+
RESULT="false"
52+
fi
53+
54+
echo "::set-output name=result::$RESULT"
55+
56+
test:
57+
name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }})
58+
needs: run-determination
59+
if: needs.run-determination.outputs.result == 'true'
60+
61+
strategy:
62+
fail-fast: false
63+
64+
matrix:
65+
operating-system:
66+
- ubuntu-latest
67+
- windows-latest
68+
- macos-latest
69+
module:
70+
- path: ./
71+
codecov-flags: unit
72+
73+
runs-on: ${{ matrix.operating-system }}
74+
75+
steps:
76+
- name: Checkout repository
77+
uses: actions/checkout@v2
78+
79+
- name: Install Go
80+
uses: actions/setup-go@v2
81+
with:
82+
go-version: ${{ env.GO_VERSION }}
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+
env:
92+
GO_MODULE_PATH: ${{ matrix.module.path }}
93+
run: task go:test
94+
95+
- name: Send unit tests coverage to Codecov
96+
if: runner.os == 'Linux'
97+
uses: codecov/codecov-action@v2
98+
with:
99+
file: ${{ matrix.module.path }}coverage_unit.txt
100+
flags: ${{ matrix.module.codecov-flags }}
101+
fail_ci_if_error: ${{ github.repository == 'arduino/pluggable-discovery-protocol-handler' }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
dist/
22

3+
coverage_*.txt

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<!-- NOTE: update the pkg.go.dev badge URL on each major release -->
44

55
[![Go Reference](https://pkg.go.dev/badge/github.com/arduino/pluggable-discovery-protocol-handler.svg)](https://pkg.go.dev/github.com/arduino/pluggable-discovery-protocol-handler/v2)
6+
[![Test Go status](https://github.com/arduino/pluggable-discovery-protocol-handler/actions/workflows/test-go-task.yml/badge.svg)](https://github.com/arduino/pluggable-discovery-protocol-handler/actions/workflows/test-go-task.yml)
7+
[![Codecov](https://codecov.io/gh/arduino/pluggable-discovery-protocol-handler/branch/main/graph/badge.svg)](https://codecov.io/gh/arduino/pluggable-discovery-protocol-handler)
68

79
This project is a library to ease implementation of pluggable discoveries for the [Arduino CLI](https://github.com/arduino/arduino-cli)
810
following the [official specification](https://arduino.github.io/arduino-cli/latest/platform-specification/#pluggable-discovery).

Taskfile.yml

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ vars:
99
DUMMY_DISCOVERY_LDFLAGS: >
1010
-X github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args.Tag={{.DUMMY_DISCOVERY_VERSION}}
1111
-X github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args.Timestamp={{.DUMMY_DISCOVERY_TIMESTAMP}}
12+
# Path of the project's primary Go module:
13+
DEFAULT_GO_MODULE_PATH: ./
1214
DEFAULT_GO_PACKAGES:
13-
sh: echo $(go list ./... | tr '\n' ' ')
15+
sh: |
16+
echo $(cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"')
17+
# `-ldflags` flag to use for `go test` command
18+
TEST_LDFLAGS:
1419

1520
tasks:
1621
build-dummy-discovery:
@@ -20,6 +25,21 @@ tasks:
2025
cmds:
2126
- go build -o dist/{{.EXECUTABLE}} -v -ldflags '{{.DUMMY_DISCOVERY_LDFLAGS}}' ./dummy-discovery
2227

28+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-go-task/Taskfile.yml
29+
go:test:
30+
desc: Run unit tests
31+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
32+
cmds:
33+
- |
34+
go test \
35+
-v \
36+
-short \
37+
-run '{{default ".*" .GO_TEST_REGEX}}' \
38+
{{default "-timeout 10m -coverpkg=./... -covermode=atomic" .GO_TEST_FLAGS}} \
39+
-coverprofile=coverage_unit.txt \
40+
{{.TEST_LDFLAGS}} \
41+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
42+
2343
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-workflows-task/Taskfile.yml
2444
ci:validate:
2545
desc: Validate GitHub Actions workflows against their JSON schema

dummy-discovery/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dummy-discovery
2+
dummy-discovery.exe

0 commit comments

Comments
 (0)