# 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://developer.github.com/webhooks/event-payloads/ # https://github.com/actions/cache # 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 on: [push, pull_request] name: Run tests jobs: test_all: continue-on-error: true defaults: run: shell: bash strategy: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] # Do not forget to bump every 6 months! gover: ["1.16"] runs-on: "${{matrix.os}}" name: "go${{matrix.gover}}.x on ${{matrix.os}}" steps: - uses: actions/setup-go@v2 with: go-version: "^${{matrix.gover}}.0" # Checkout and print debugging information. - name: Turn off git core.autocrlf run: git config --global core.autocrlf false - uses: actions/checkout@v2 - name: 'go version' run: go version - name: 'go env' run: go env - name: "Debug" run: | echo HOME = $HOME echo GITHUB_WORKSPACE = $GITHUB_WORKSPACE echo PATH = $PATH echo "" echo $ ls -l $HOME/go/bin ls -la $HOME/go/bin - name: 'Cache: ~/go' uses: actions/cache@v2 with: path: ~/go key: "${{runner.os}}-gopkg-${{hashFiles('go.sum', '.github/workflows/*.yml')}}-v2" # Fetch the tools before checking out, so they don't modify go.mod/go.sum. - name: 'go get necessary tools' run: > cd ..; go get -u -v github.com/gordonklaus/ineffassign github.com/securego/gosec/cmd/gosec golang.org/x/lint/golint golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow honnef.co/go/tools/cmd/staticcheck - name: 'go get necessary tools (ubuntu)' if: always() && matrix.os == 'ubuntu-latest' run: > cd ..; go get -u -v github.com/client9/misspell/cmd/misspell github.com/google/addlicense # Now run proper checks. - name: 'Check: go vet' if: always() run: go vet ./... - name: 'Check: go vet shadow; shadowed variables' run: | SHADOW_TOOL="$(which shadow)" if [ -f "${SHADOW_TOOL}.exe" ]; then SHADOW_TOOL="${SHADOW_TOOL}.exe" fi go vet -vettool=$SHADOW_TOOL ./... - name: 'Check: golint' if: always() # TODO: Remove true if we make this check ever pass. run: golint -set_exit_status ./... || true - name: 'Check: inefficient variable assignment' if: always() run: ineffassign ./... - name: 'Check: staticcheck' if: always() run: staticcheck -checks inherit,-SA1019 ./... - name: 'Check: gosec (only G104)' run: gosec -include=G104 -fmt=golint -quiet ./... # 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 '*.sh' -prune -o -path ./.git -prune -o -type f -executable -print | grep -e . ; then echo 'Do not commit executables beside shell scripts' false fi - 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: addlicense; all sources have a license header (ubuntu)' if: always() && matrix.os == 'ubuntu-latest' run: addlicense -check . - 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 # Run tests last since it's potentially the slowest step. - name: 'Check: go test -cover' run: go test -timeout=40s -covermode=count -coverprofile coverage.txt ./... # Don't send code coverage if anything failed to reduce spam. - uses: codecov/codecov-action@v1 - name: 'Cleanup' run: rm coverage.txt # Don't run go test -race if anything failed, to speed up the results. - name: 'Check: go test -race' run: go test -timeout=40s -race ./... - name: 'Check: go test -bench .' run: go test -timeout=40s -bench . -benchtime=100ms -cpu=1 ./... - name: "Check: tree is clean" 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 - name: "Check: go generate doesn't modify files" run: | go generate ./... # TODO(maruel): Due to https://github.com/golang/go/issues/40276, ignore # go.mod/go.sum modifications. Remove once a new Go toolchain fixes # this. git checkout HEAD -- go.mod go.sum # Also test for untracked files. 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" 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 https://github.com/periph/cmd cd cmd go get periph.io/x/devices/v3@${GITHUB_SHA} go test -short ./... - name: 'Send comments' if: failure() && github.event_name == 'pull_request' run: | if [ -f _comments.txt ]; then URL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) echo "Sending $(cat _comments.txt|wc -l) lines of comments to ${URL}" PAYLOAD=$(echo '{}' | jq --arg body "$(cat _comments.txt)" '.body = $body') curl -sS --request POST \ --header "Authorization: Bearer ${{secrets.GITHUB_TOKEN}}" \ --header "Content-Type: application/json" \ --data "${PAYLOAD}" "${URL}" > /dev/null fi # Run on older Go versions while inside GOPATH, which tests different parts # of the code. go modules will be used on 1.13 and later but not before. test_short: env: GOPATH: ${{github.workspace}} GO111MODULE: auto continue-on-error: true strategy: fail-fast: false matrix: os: [ubuntu-latest] gover: ['1.13.15', '1.14.7'] runs-on: "${{matrix.os}}" name: "go${{matrix.gover}} on ${{matrix.os}} (quick)" steps: - uses: actions/setup-go@v2 with: go-version: "${{matrix.gover}}" - uses: actions/checkout@v2 with: path: src/periph.io/x/devices - name: 'Check: go get -d -t' working-directory: src/periph.io/x/devices run: go get -d -t ./... - name: 'Check: go test' working-directory: src/periph.io/x/devices run: go test -timeout=40s ./...