Skip to content

Commit ce8f250

Browse files
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 b2d61b7 commit ce8f250

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.19"
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+
steps:
45+
- name: Determine if the rest of the workflow should run
46+
id: determination
47+
run: |
48+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
49+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
50+
if [[
51+
"${{ github.event_name }}" != "create" ||
52+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
53+
]]; then
54+
# Run the other jobs.
55+
RESULT="true"
56+
else
57+
# There is no need to run the other jobs.
58+
RESULT="false"
59+
fi
60+
61+
echo "result=$RESULT" >> $GITHUB_OUTPUT
62+
63+
test:
64+
name: test (${{ matrix.module.path }})
65+
needs: run-determination
66+
if: needs.run-determination.outputs.result == 'true'
67+
68+
strategy:
69+
fail-fast: false
70+
71+
matrix:
72+
module:
73+
- path: ./
74+
codecov-flags: unit
75+
76+
runs-on: windows-latest
77+
78+
steps:
79+
- name: Checkout repository
80+
uses: actions/checkout@v3
81+
82+
- name: Install Go
83+
uses: actions/setup-go@v3
84+
with:
85+
go-version: ${{ env.GO_VERSION }}
86+
87+
- name: Install Task
88+
uses: arduino/setup-task@v1
89+
with:
90+
repo-token: ${{ secrets.GITHUB_TOKEN }}
91+
version: 3.x
92+
93+
- name: Run tests
94+
env:
95+
GO_MODULE_PATH: ${{ matrix.module.path }}
96+
run: task go:test
97+
98+
- name: Send unit tests coverage to Codecov
99+
if: runner.os == 'Linux'
100+
uses: codecov/codecov-action@v3
101+
with:
102+
file: ${{ matrix.module.path }}coverage_unit.txt
103+
flags: ${{ matrix.module.codecov-flags }}
104+
fail_ci_if_error: ${{ github.repository == 'arduino/go-win32-utils' }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/node_modules/
2+
go-win32-utils
3+
go-win32-utils.exe

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
[![Sync Labels status](https://github.com/arduino/go-win32-utils/actions/workflows/sync-labels.yml/badge.svg)](https://github.com/arduino/go-win32-utils/actions/workflows/sync-labels.yml)
33
[![Check Markdown status](https://github.com/arduino/go-win32-utils/actions/workflows/check-markdown-task.yml/badge.svg)](https://github.com/arduino/go-win32-utils/actions/workflows/check-markdown-task.yml)
44
[![Check Taskfiles status](https://github.com/arduino/go-win32-utils/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/go-win32-utils/actions/workflows/check-taskfiles.yml)
5+
[![Test Go status](https://github.com/arduino/go-win32-utils/actions/workflows/test-go-task.yml/badge.svg)](https://github.com/arduino/go-win32-utils/actions/workflows/test-go-task.yml)
6+
[![Codecov](https://codecov.io/gh/arduino/go-win32-utils/branch/main/graph/badge.svg)](https://codecov.io/gh/arduino/go-win32-utils)
57

68
This library contains some useful calls to win32 API that are not available on the standard golang library.
79

Taskfile.yml

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

4+
vars:
5+
# Path of the project's primary Go module:
6+
DEFAULT_GO_MODULE_PATH: ./
7+
DEFAULT_GO_PACKAGES:
8+
sh: |
9+
echo $(
10+
cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} &&
11+
go list ./... | tr '\n' ' ' ||
12+
echo '"ERROR: Unable to discover Go packages"'
13+
)
14+
# `-ldflags` flag to use for `go build` command
15+
LDFLAGS:
16+
# `-ldflags` flag to use for `go test` command
17+
TEST_LDFLAGS:
18+
419
tasks:
520
docs:generate:
621
desc: Create all generated documentation content
@@ -87,3 +102,18 @@ tasks:
87102
desc: Install dependencies managed by npm
88103
cmds:
89104
- npm install
105+
106+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-go-task/Taskfile.yml
107+
go:test:
108+
desc: Run unit tests
109+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
110+
cmds:
111+
- |
112+
go test \
113+
-v \
114+
-short \
115+
-run '{{default ".*" .GO_TEST_REGEX}}' \
116+
{{default "-timeout 10m -coverpkg=./... -covermode=atomic" .GO_TEST_FLAGS}} \
117+
-coverprofile=coverage_unit.txt \
118+
{{.TEST_LDFLAGS}} \
119+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

0 commit comments

Comments
 (0)