33# that can be found in the LICENSE file.
44
55# References:
6- # https://developer.github.com/webhooks/event-payloads/
7- # https://github.com/actions/cache
86# https://github.com/actions/checkout
97# https://github.com/actions/setup-go
108# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#using-the-github_token-in-a-workflow
119# https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions/
1210# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
11+ # https://docs.github.com/en/rest/commits/comments#create-a-commit-comment
12+ # https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
13+ # https://docs.github.com/en/actions/learn-github-actions/contexts
1314
1415on : [push, pull_request]
1516name : Run tests
1617jobs :
17- test_all :
18+ # Runs go test both with code coverage sent to codecov, race detector and
19+ # benchmarks. At the end do a quick check to ensure the tests to not leave
20+ # files in the tree.
21+ test :
22+ name : " test: go${{matrix.gover}}.x/${{matrix.os}}"
23+ runs-on : " ${{matrix.os}}"
1824 continue-on-error : true
1925 defaults :
2026 run :
@@ -24,18 +30,89 @@ jobs:
2430 matrix :
2531 os : [ubuntu-latest, macos-latest, windows-latest]
2632 # Do not forget to bump every 6 months!
27- gover : ["1.17 "]
28- runs-on : " ${{matrix.os}} "
29- name : " go${{matrix.gover}}.x on ${{matrix.os}} "
33+ gover : ["1.19 "]
34+ env :
35+ PYTHONDONTWRITEBYTECODE : x
3036 steps :
37+ - name : Turn off git core.autocrlf
38+ if : matrix.os == 'windows-latest'
39+ run : git config --global core.autocrlf false
40+ - uses : actions/checkout@v3
41+ with :
42+ fetch-depth : 2
3143 - uses : actions/setup-go@v3
3244 with :
3345 go-version : " ~${{matrix.gover}}.0"
46+ cache : true
47+ - name : ' go install necessary tools'
48+ if : always()
49+ run : |
50+ go install github.com/maruel/pat/cmd/ba@latest
51+ - name : ' Check: go test -cover'
52+ if : always()
53+ run : go test -timeout=120s -covermode=count -coverprofile coverage.txt -bench=. -benchtime=1x ./...
54+ # Don't send code coverage if anything failed to reduce spam.
55+ - uses : codecov/codecov-action@v2
56+ - name : ' Cleanup'
57+ if : always()
58+ run : rm coverage.txt
59+ - name : ' Check: go test -race'
60+ run : go test -timeout=120s -race -bench=. -benchtime=1x ./...
61+ - name : ' Check: benchmark 📈'
62+ run : ba -against HEAD~1
63+ - name : ' Check: go test -short (CGO_ENABLED=0)'
64+ env :
65+ CGO_ENABLED : 0
66+ run : go test -timeout=120s -short -bench=. -benchtime=1x ./...
67+ - name : ' Check: go test -short (32 bits)'
68+ if : matrix.os != 'macos-latest'
69+ env :
70+ GOARCH : 386
71+ run : go test -timeout=120s -short -bench=. -benchtime=1x ./...
72+ - name : " Check: tree is clean"
73+ if : always()
74+ run : |
75+ # Nothing should have changed in the tree up to that point and no
76+ # unsuspected file was created.
77+ TOUCHED=$(git status --porcelain --ignored)
78+ if ! test -z "$TOUCHED"; then
79+ echo "Oops, something touched these files, please cleanup:"
80+ echo "$TOUCHED"
81+ git diff
82+ false
83+ fi
3484
35- # Checkout and print debugging information.
85+
86+ # Run linters. This workflow can be merged with the test_all one if desired
87+ # to cut on runtime, at the cost of latency. I dislike waiting for results
88+ # so I prefer to run them in parallel.
89+ lint :
90+ name : " lint: go${{matrix.gover}}.x/${{matrix.os}}"
91+ runs-on : " ${{matrix.os}}"
92+ continue-on-error : true
93+ defaults :
94+ run :
95+ shell : bash
96+ strategy :
97+ fail-fast : false
98+ matrix :
99+ # You may want to run only on linux to save on cost. Projects with
100+ # OS-specific code benefits from explicitly linting on macOS and
101+ # Windows.
102+ os : [ubuntu-latest, macos-latest, windows-latest]
103+ # Do not forget to bump every 6 months!
104+ gover : ["1.19"]
105+ env :
106+ PYTHONDONTWRITEBYTECODE : x
107+ steps :
36108 - name : Turn off git core.autocrlf
109+ if : matrix.os == 'windows-latest'
37110 run : git config --global core.autocrlf false
38- - uses : actions/checkout@v2
111+ - uses : actions/checkout@v3
112+ - uses : actions/setup-go@v3
113+ with :
114+ go-version : " ~${{matrix.gover}}.0"
115+ cache : true
39116 - name : " Debug"
40117 run : |
41118 echo HOME = $HOME
@@ -44,31 +121,23 @@ jobs:
44121 echo ""
45122 echo $ ls -l $HOME/go/bin
46123 ls -la $HOME/go/bin
47-
48- - name : ' Cache: ~/go'
49- uses : actions/cache@v2
50- with :
51- path : ~/go
52- key : " ${{runner.os}}-gopkg-${{hashFiles('go.sum', '.github/workflows/*.yml')}}"
53-
54- # Fetch the tools before checking out, so they don't modify go.mod/go.sum.
55124 - name : ' go install necessary tools'
125+ if : always()
56126 run : |
57- go install -v github.com/gordonklaus/ineffassign@latest
58- go install -v github.com/securego/gosec/cmd/gosec@latest
59- go install -v golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
60- go install -v honnef.co/go/tools/cmd/staticcheck@latest
127+ go install github.com/gordonklaus/ineffassign@latest
128+ go install github.com/securego/gosec/v2 /cmd/gosec@latest
129+ go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
130+ go install honnef.co/go/tools/cmd/staticcheck@latest
61131 - name : ' go install necessary tools (ubuntu)'
62132 if : always() && matrix.os == 'ubuntu-latest'
63133 run : |
64- go install -v github.com/client9/misspell/cmd/misspell@latest
65- go install -v github.com/google/addlicense@latest
66-
67- # Now run proper checks.
134+ go install github.com/client9/misspell/cmd/misspell@latest
135+ go install github.com/google/addlicense@latest
68136 - name : ' Check: go vet'
69137 if : always()
70- run : go vet ./...
138+ run : go vet -unsafeptr=false ./...
71139 - name : ' Check: go vet shadow; shadowed variables'
140+ if : always()
72141 run : |
73142 SHADOW_TOOL="$(which shadow)"
74143 if [ -f "${SHADOW_TOOL}.exe" ]; then
@@ -80,19 +149,23 @@ jobs:
80149 run : ineffassign ./...
81150 - name : ' Check: staticcheck'
82151 if : always()
152+ # SA1019: Foo is deprecated
83153 run : staticcheck -checks inherit,-SA1019 ./...
84- - name : ' Check: gosec (only G104) '
85- run : gosec -include=G104 -fmt=golint -quiet ./...
86-
154+ - name : ' Check: gosec'
155+ if : always()
156+ run : gosec -fmt=golint -quiet ./...
87157 # The following checks are not dependent on the OS or go build tags. Only
88158 # run them on ubuntu-latest since it's the fastest one.
89159 - name : ' Check: no executable was committed (ubuntu)'
90160 if : always() && matrix.os == 'ubuntu-latest'
91161 run : |
92- if find . -path '*.sh' -prune -o -path ./.git -prune -o -type f -executable -print | grep -e . ; then
93- echo 'Do not commit executables beside shell scripts '
162+ if find . -path ./.git -prune -o -type f -executable -print | grep -e . ; then
163+ echo 'Do not commit executables'
94164 false
95165 fi
166+ - name : ' Check: addlicense; all sources have a license header (ubuntu)'
167+ if : always() && matrix.os == 'ubuntu-latest'
168+ run : addlicense -check .
96169 - name : ' Check: gofmt; code is well formatted (ubuntu)'
97170 if : always() && matrix.os == 'ubuntu-latest'
98171 run : |
@@ -104,67 +177,54 @@ jobs:
104177 echo "- ${FILE}" >> _gofmt.txt
105178 done
106179 cat _gofmt.txt
107- echo "## ⚠ gofmt Failed" >> _comments.txt
108- echo "" >> _comments.txt
109- cat _gofmt.txt >> _comments.txt
110- echo "" >> _comments.txt
180+ echo "## ⚠ gofmt Failed" >> ../ _comments.txt
181+ echo "" >> ../ _comments.txt
182+ cat _gofmt.txt >> ../ _comments.txt
183+ echo "" >> ../ _comments.txt
111184 false
112185 fi
113- - name : ' Check: addlicense; all sources have a license header (ubuntu)'
114- if : always() && matrix.os == 'ubuntu-latest'
115- run : addlicense -check .
116186 - name : " Check: misspelling; code doesn't contain misspelling (ubuntu)"
117187 if : always() && matrix.os == 'ubuntu-latest'
118188 run : |
119189 ERR=$(misspell .)
120190 if ! test -z "$ERR"; then
121191 echo "$ERR"
122- echo "## ⚠ misspell Failed" >> _comments.txt
123- echo "" >> _comments.txt
124- echo "$ERR" >> _comments.txt
125- echo "" >> _comments.txt
192+ echo "## ⚠ misspell Failed" >> ../ _comments.txt
193+ echo "" >> ../ _comments.txt
194+ echo "$ERR" >> ../ _comments.txt
195+ echo "" >> ../ _comments.txt
126196 false
127197 fi
128-
129- # Run tests last since it's potentially the slowest step.
130- - name : ' Check: go test -cover'
131- run : go test -timeout=40s -covermode=count -coverprofile coverage.txt ./...
132- # Don't send code coverage if anything failed to reduce spam.
133- - uses : codecov/codecov-action@v2
134- - name : ' Cleanup'
135- run : rm coverage.txt
136- # Don't run go test -race if anything failed, to speed up the results.
137- - name : ' Check: go test -race'
138- run : go test -timeout=60s -race ./...
139- - name : ' Check: go test -bench .'
140- run : go test -timeout=40s -bench . -benchtime=100ms -cpu=1 ./...
141- - name : ' Check: CGO_ENABLED=0 go test -short'
142- run : CGO_ENABLED=0 go test -timeout=40s -short ./...
143-
144- - name : " Check: tree is clean"
198+ - name : ' Send comments'
199+ if : failure()
145200 run : |
146- # Nothing should have changed in the tree up to that point and no
147- # unsuspected file was created.
148- TOUCHED=$(git status --porcelain --ignored)
149- if ! test -z "$TOUCHED"; then
150- echo "Oops, something touched these files, please cleanup:"
151- echo "$TOUCHED"
152- git diff
153- false
201+ if [ -f ../_comments.txt ]; then
202+ URL="${{github.event.issue.pull_request.url}}"
203+ if test -z "$URL"; then
204+ URL="${{github.api_url}}/repos/${{github.repository}}/commits/${{github.sha}}/comments"
205+ fi
206+ echo "Sending $(cat ../_comments.txt|wc -l) lines of comments to ${URL}"
207+ curl -sS --request POST \
208+ --header "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" \
209+ --header "Content-Type: application/json" \
210+ --data "$(cat ../_comments.txt | jq -R --slurp '{body: .}')" \
211+ "${URL}" > /dev/null
212+ rm ../_comments.txt
154213 fi
155-
156214 - name : " Check: go generate doesn't modify files"
215+ if : always()
157216 run : |
158217 go generate ./...
159- # Also test for untracked files.
218+ # Also test for untracked files. go generate should not generate ignored
219+ # files either.
160220 TOUCHED=$(git status --porcelain --ignored)
161221 if ! test -z "$TOUCHED"; then
162222 echo "go generate created these files, please fix:"
163223 echo "$TOUCHED"
164224 false
165225 fi
166-
167226 - name : " Check: go mod tidy doesn't modify files"
227+ if : always()
168228 run : |
169229 go mod tidy
170230 TOUCHED=$(git status --porcelain --ignored)
@@ -173,7 +233,6 @@ jobs:
173233 git diff
174234 false
175235 fi
176-
177236 - name : ' Test on periph.io/x/cmd'
178237 # Force an upgrade to test cmd with tip of tree devices.
179238 run : |
@@ -184,32 +243,60 @@ jobs:
184243 go get -t ./...
185244 go test -short ./...
186245
187- - name : ' Send comments'
188- if : failure() && github.event_name == 'pull_request'
189- run : |
190- if [ -f _comments.txt ]; then
191- URL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
192- echo "Sending $(cat _comments.txt|wc -l) lines of comments to ${URL}"
193- PAYLOAD=$(echo '{}' | jq --arg body "$(cat _comments.txt)" '.body = $body')
194- curl -sS --request POST \
195- --header "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" \
196- --header "Content-Type: application/json" \
197- --data "${PAYLOAD}" "${URL}" > /dev/null
198- fi
199246
200- test_short :
247+ # Ensure tests pass on oldest supported Go version.
248+ old :
249+ name : " test: go${{matrix.gover}}/${{matrix.os}}"
250+ runs-on : " ${{matrix.os}}"
201251 continue-on-error : true
252+ defaults :
253+ run :
254+ shell : bash
202255 strategy :
203256 fail-fast : false
204257 matrix :
205258 os : [ubuntu-latest]
206- gover : ['1.14.15']
207- runs-on : " ${{matrix.os}}"
208- name : " go${{matrix.gover}} on ${{matrix.os}} (quick)"
259+ # https://github.com/golang/go/issues/55078
260+ # golang.org/x/sys/unix broke on Go versions before 1.17. Not worth
261+ # fixing.
262+ gover : ['1.17.13']
263+ env :
264+ PYTHONDONTWRITEBYTECODE : x
209265 steps :
210- - uses : actions/setup-go@v2
266+ - name : Turn off git core.autocrlf
267+ if : matrix.os == 'windows-latest'
268+ run : git config --global core.autocrlf false
269+ - uses : actions/checkout@v3
270+ - uses : actions/setup-go@v3
211271 with :
212- go-version : " ${{matrix.gover}}"
213- - uses : actions/checkout@v2
272+ go-version : " =${{matrix.gover}}"
214273 - name : ' Check: go test'
215- run : go test -timeout=40s ./...
274+ run : go test -timeout=120s -bench=. -benchtime=1x ./...
275+
276+
277+ codeql :
278+ name : " codeql: go${{matrix.gover}}.x/${{matrix.os}}"
279+ runs-on : " ${{matrix.os}}"
280+ continue-on-error : true
281+ strategy :
282+ fail-fast : false
283+ matrix :
284+ os : [ubuntu-latest]
285+ # Do not forget to bump every 6 months!
286+ gover : ["1.19"]
287+ permissions :
288+ security-events : write
289+ steps :
290+ - uses : actions/checkout@v3
291+ - uses : actions/setup-go@v3
292+ with :
293+ go-version : " ~${{matrix.gover}}.0"
294+ cache : true
295+ - name : Initialize CodeQL
296+ uses : github/codeql-action/init@v2
297+ with :
298+ languages : go
299+ - name : Autobuild
300+ uses : github/codeql-action/autobuild@v2
301+ - name : Perform CodeQL Analysis
302+ uses : github/codeql-action/analyze@v2
0 commit comments