diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3fd37e6..49e12c3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,7 +1,7 @@ on: [pull_request] env: - GO_VERSION: '^1.21.2' + GO_VERSION: '^1.21' jobs: test: @@ -12,163 +12,15 @@ jobs: uses: actions/setup-go@v2 with: go-version: ${{ env.GO_VERSION }} - - - name: Prepare coverage - run: | - go install github.com/jandelgado/gcov2lcov@latest - name: Checkout uses: actions/checkout@v2 - - - name: Prepare coverage report - env: - CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }} - CC_TR_URL: "https://codeclimate.com/downloads/test-reporter" - CC_TR_BIN: "test-reporter-latest-linux-amd64" - run: | - mkdir -p coverage && \ - curl -L ${CC_TR_URL}/${CC_TR_BIN} > ./cc-test-reporter && \ - chmod +x ./cc-test-reporter && \ - ./cc-test-reporter before-build - name: Run tests - run: | - go test -v ./... -race -coverprofile=coverage/gcov.out -covermode=atomic && \ - gcov2lcov -infile=coverage/gcov.out -outfile=coverage/lcov.info - - - name: Push coverage env: CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }} run: | - ./cc-test-reporter after-build -t lcov - check-formatting: - runs-on: ubuntu-latest - steps: - - - name: Checkout - uses: actions/checkout@v2 - - - name: Check formatting - run: | - gofmt -s -e -d -l . | tee /tmp/gofmt.output && [ $(cat /tmp/gofmt.output | wc -l) -eq 0 ] - check-smells: - runs-on: ubuntu-latest - steps: - - - name: Checkout - uses: actions/checkout@v2 - - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Check code smells - run: | - go vet ./... - check-complexity: - runs-on: ubuntu-latest - steps: - - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Install gocyclo - run: | - go install github.com/fzipp/gocyclo/cmd/gocyclo@latest - - - name: Checkout - uses: actions/checkout@v2 - - - name: Check cyclomatic complexity - run: | - gocyclo -over 15 . - check-style: - runs-on: ubuntu-latest - steps: - - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Install golint - run: | - go install golang.org/x/lint/golint@latest - - - name: Checkout - uses: actions/checkout@v2 - - - name: Check Style - run: | - golint ./... - check-ineffectual-assignments: - runs-on: ubuntu-latest - steps: - - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Install ineffassign - run: | - go install github.com/gordonklaus/ineffassign@latest - - - name: Checkout - uses: actions/checkout@v2 - - - name: Download dependencies to local - run : | - go mod download - - - name: Check ineffectual assignments - run: | - ineffassign ./... - check-spelling: - runs-on: ubuntu-latest - steps: - - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Install spellchecker - run: | - go install github.com/client9/misspell/cmd/misspell@latest - - - name: Checkout - uses: actions/checkout@v2 - - - name: Check spelling - run: | - misspell -error . - staticcheck: - runs-on: ubuntu-latest - steps: - - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Install staticcheck - run: | - go install honnef.co/go/tools/cmd/staticcheck@latest - - - name: Checkout - uses: actions/checkout@v2 - - - name: Download dependencies to local - run : | - go mod download - - - name: Run staticcheck - run: | - staticcheck ./... + scripts/check.sh all check-license: runs-on: ubuntu-latest steps: diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad0a524 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/coverage/ +/cc-test-reporter diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 0000000..3190c77 --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,118 @@ +#!/bin/env -S bash -l + +set -eu + +is_codeclimate_setup() { + set +u + if [ -z "${CC_TEST_REPORTER_ID}" ]; then + set -u + return 1 + fi + set -u + return 0 +} + +codeclimate_precheck() { + # This function requires CC_TEST_REPORTER_ID env var, containing + # CodeClimate Token, to be present in the environment + if is_codeclimate_setup; then + echo "*** CodeClimate is setup" + CC_TEST_REPORTER_URL=https://codeclimate.com/downloads/test-reporter/ + CC_TEST_REPORTER_BIN=test-reporter-latest-linux-amd64 + curl -L ${CC_TEST_REPORTER_URL}/${CC_TEST_REPORTER_BIN} > ./cc-test-reporter + chmod +x ./cc-test-reporter + ./cc-test-reporter before-build + else + echo "*** CodeClimate is not setup" + fi +} + +convert_coverage_report_gcov2lcov() { + go install github.com/jandelgado/gcov2lcov@latest + gcov2lcov -infile=${1} -outfile=${2} +} + +tests_check() { + echo "*** Run unit tests" + EXTRA_TEST_PARAMS="" + if is_codeclimate_setup; then + mkdir -p coverage + EXTRA_TEST_PARAMS="${EXTRA_TEST_PARAMS}-coverprofile=coverage/gcov.out " + fi + go test -v ./... -race -covermode=atomic ${EXTRA_TEST_PARAMS} + if is_codeclimate_setup; then + convert_coverage_report_gcov2lcov coverage/gcov.out coverage/lcov.info + ./cc-test-reporter after-build -t lcov + fi +} + +complexity_check() { + echo "*** Complexity check" + go install github.com/fzipp/gocyclo/cmd/gocyclo@latest + gocyclo -avg -top 5 -over 15 . +} + +format_check() { + echo "*** Format check" + gofmt -s -e -d -l . | tee /tmp/gofmt.output && [ $(cat /tmp/gofmt.output | wc -l) -eq 0 ] +} + +inefficiencies_check() { + echo "*** Inefficiencies check" + go install github.com/gordonklaus/ineffassign@latest + go mod tidy + ineffassign ./... +} + +smells_check() { + echo "*** Smells check" + go mod tidy + go vet ./... +} + +spelling_check() { + echo "*** Spelling check" + go install github.com/client9/misspell/cmd/misspell@latest + misspell -error . +} + +static_check() { + echo "*** Static check" + go install honnef.co/go/tools/cmd/staticcheck@latest + go mod download + staticcheck ./... +} + +style_check() { + echo "*** Style check" + go install golang.org/x/lint/golint@latest + golint ./... +} + +find_functions() { + HOOK=${1:-check} + + declare -F | awk '{print $3}' | grep -E "_${HOOK}$" | sed -e 's/_${HOOK}$//' +} + +try() { + if find_functions "check" | grep -w ${1} >/dev/null; then + ${1} && echo "=== OK!" || (echo "=== NOK!" && return -1) + else + echo No ${1} available + return 255 + fi +} + +if [ "${1}" == "all" ]; then + failure=0 + set +e + codeclimate_precheck + for check in $(find_functions "check"); do + try ${check} || failure=1 + done + set -e + exit ${failure} +else + try ${1} +fi