-
Notifications
You must be signed in to change notification settings - Fork 45
257 lines (251 loc) · 9.04 KB
/
test.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Copyright 2020 The Periph Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.
# References:
# https://github.com/actions/checkout
# https://github.com/actions/setup-go
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#using-the-github_token-in-a-workflow
# https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions/
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
# https://docs.github.com/en/rest/commits/comments#create-a-commit-comment
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
# https://docs.github.com/en/actions/learn-github-actions/contexts
on: [push, pull_request]
name: Run tests
jobs:
# Runs go test both with code coverage sent to codecov, race detector and
# benchmarks. At the end do a quick check to ensure the tests to not leave
# files in the tree.
test:
name: "test: ${{matrix.os}}"
runs-on: "${{matrix.os}}"
continue-on-error: true
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
env:
PYTHONDONTWRITEBYTECODE: x
steps:
- name: Turn off git core.autocrlf
if: matrix.os == 'windows-latest'
run: git config --global core.autocrlf false
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: 'go install necessary tools'
if: always()
run: |
go install github.com/maruel/pat/cmd/ba@latest
- name: 'Check: go test -cover'
if: always()
run: go test -timeout=120s -covermode=count -coverprofile coverage.txt -bench=. -benchtime=1x ./...
# Don't send code coverage if anything failed to reduce spam.
- uses: codecov/codecov-action@v4
with:
token: ${{secrets.CODECOV_TOKEN}}
- name: 'Cleanup'
if: always()
run: rm coverage.txt
- name: 'Check: go test -race'
run: go test -timeout=120s -race -bench=. -benchtime=1x ./...
- name: 'Check: benchmark 📈'
run: ba -against HEAD~1
- name: 'Check: go test -short (CGO_ENABLED=0)'
env:
CGO_ENABLED: 0
run: go test -timeout=120s -short -bench=. -benchtime=1x ./...
- name: 'Check: go test -short (32 bits)'
if: matrix.os != 'macos-latest'
env:
GOARCH: 386
run: go test -timeout=120s -short -bench=. -benchtime=1x ./...
- name: "Check: tree is clean"
if: always()
run: |
# Nothing should have changed in the tree up to that point and no
# unsuspected file was created.
TOUCHED=$(git status --porcelain --ignored)
if ! test -z "$TOUCHED"; then
echo "Oops, something touched these files, please cleanup:"
echo "$TOUCHED"
git diff
false
fi
# Run linters. This workflow can be merged with the test_all one if desired
# to cut on runtime, at the cost of latency. I dislike waiting for results
# so I prefer to run them in parallel.
lint:
name: "lint: ${{matrix.os}}"
runs-on: "${{matrix.os}}"
continue-on-error: true
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
# You may want to run only on linux to save on cost. Projects with
# OS-specific code benefits from explicitly linting on macOS and
# Windows.
os: [ubuntu-latest, macos-latest, windows-latest]
env:
PYTHONDONTWRITEBYTECODE: x
steps:
- name: Turn off git core.autocrlf
if: matrix.os == 'windows-latest'
run: git config --global core.autocrlf false
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: 'go install necessary tools'
if: always()
run: |
go install github.com/gordonklaus/ineffassign@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
- name: 'go install necessary tools (ubuntu)'
if: always() && matrix.os == 'ubuntu-latest'
run: |
go install github.com/client9/misspell/cmd/misspell@latest
go install github.com/google/addlicense@latest
- name: 'Check: go vet'
if: always()
run: go vet -unsafeptr=false ./...
- name: 'Check: go vet shadow; shadowed variables'
if: always()
run: |
SHADOW_TOOL="$(which shadow)"
if [ -f "${SHADOW_TOOL}.exe" ]; then
SHADOW_TOOL="${SHADOW_TOOL}.exe"
fi
go vet -vettool=$SHADOW_TOOL ./...
- name: 'Check: inefficient variable assignment'
if: always()
run: ineffassign ./...
- name: 'Check: staticcheck'
if: always()
# SA1019: Foo is deprecated
run: staticcheck -checks inherit,-SA1019 ./...
- name: 'Check: gosec'
if: always()
run: gosec -fmt=golint -quiet -exclude=G115 ./...
# The following checks are not dependent on the OS or go build tags. Only
# run them on ubuntu-latest since it's the fastest one.
- name: 'Check: no executable was committed (ubuntu)'
if: always() && matrix.os == 'ubuntu-latest'
run: |
if find . -path ./.git -prune -o -type f -executable -print | grep -e . ; then
echo 'Do not commit executables'
false
fi
- name: 'Check: addlicense; all sources have a license header (ubuntu)'
if: always() && matrix.os == 'ubuntu-latest'
run: addlicense -check .
- name: 'Check: gofmt; code is well formatted (ubuntu)'
if: always() && matrix.os == 'ubuntu-latest'
run: |
FILES=$(gofmt -s -l .)
if ! test -z "$FILES"; then
echo 'Please run `gofmt -s -w` on the following files:' >> _gofmt.txt
echo "" >> _gofmt.txt
for FILE in ${FILES}; do
echo "- ${FILE}" >> _gofmt.txt
done
cat _gofmt.txt
echo "## ⚠ gofmt Failed" >> ../_comments.txt
echo "" >> ../_comments.txt
cat _gofmt.txt >> ../_comments.txt
echo "" >> ../_comments.txt
false
fi
- name: "Check: misspelling; code doesn't contain misspelling (ubuntu)"
if: always() && matrix.os == 'ubuntu-latest'
run: |
ERR=$(misspell .)
if ! test -z "$ERR"; then
echo "$ERR"
echo "## ⚠ misspell Failed" >> ../_comments.txt
echo "" >> ../_comments.txt
echo "$ERR" >> ../_comments.txt
echo "" >> ../_comments.txt
false
fi
- name: 'Send comments'
if: failure()
run: |
if [ -f ../_comments.txt ]; then
URL="${{github.event.issue.pull_request.url}}"
if test -z "$URL"; then
URL="${{github.api_url}}/repos/${{github.repository}}/commits/${{github.sha}}/comments"
fi
echo "Sending $(cat ../_comments.txt|wc -l) lines of comments to ${URL}"
curl -sS --request POST \
--header "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" \
--header "Content-Type: application/json" \
--data "$(cat ../_comments.txt | jq -R --slurp '{body: .}')" \
"${URL}" > /dev/null
rm ../_comments.txt
fi
- name: "Check: go generate doesn't modify files"
if: always()
run: |
go generate ./...
# Also test for untracked files. go generate should not generate ignored
# files either.
TOUCHED=$(git status --porcelain --ignored)
if ! test -z "$TOUCHED"; then
echo "go generate created these files, please fix:"
echo "$TOUCHED"
false
fi
- name: "Check: go mod tidy doesn't modify files"
if: always()
run: |
go mod tidy
TOUCHED=$(git status --porcelain --ignored)
if ! test -z "$TOUCHED"; then
echo "go mod tidy was not clean, please update:"
git diff
false
fi
- name: 'Test on periph.io/x/cmd'
# Force an upgrade to test cmd with tip of tree devices.
run: |
cd ..
git clone --depth 1 https://github.com/periph/cmd
cd cmd
go mod edit -replace=periph.io/x/devices/v3=../devices
go get -t ./...
go test -short ./...
codeql:
name: "codeql: ${{matrix.os}}"
runs-on: "${{matrix.os}}"
continue-on-error: true
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
permissions:
security-events: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: go
- name: Autobuild
uses: github/codeql-action/autobuild@v3
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3